<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ROCKIMG</title>
    <description>The latest articles on DEV Community by ROCKIMG (@rockimg).</description>
    <link>https://dev.to/rockimg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4032961%2Fa16f03ea-f566-4e8e-9264-477842ae5d9d.jpg</url>
      <title>DEV Community: ROCKIMG</title>
      <link>https://dev.to/rockimg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rockimg"/>
    <language>en</language>
    <item>
      <title>Brightness, Contrast, and Saturation Change Different Numbers</title>
      <dc:creator>ROCKIMG</dc:creator>
      <pubDate>Sun, 01 Nov 2026 14:48:41 +0000</pubDate>
      <link>https://dev.to/rockimg/brightness-contrast-and-saturation-change-different-numbers-fjp</link>
      <guid>https://dev.to/rockimg/brightness-contrast-and-saturation-change-different-numbers-fjp</guid>
      <description>&lt;p&gt;A photo can be too dark, too flat, or too gray, and those are three different complaints. Brightness, contrast, and saturation each answer one of them. Turning the wrong one up is why a "brighter" edit sometimes looks milky, and why a "more colorful" edit sometimes just looks harsher.&lt;/p&gt;

&lt;p&gt;The three controls below are the ordinary per-pixel versions used in a browser canvas: an amount in percent, &lt;code&gt;factor = amount / 100&lt;/code&gt;, and each red, green, and blue value kept inside 0–255. At 100% every formula is the identity. Nothing else in the file is consulted. There is no blur, no tone curve, and no separate luminance plane unless the formula builds one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each word means
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Brightness&lt;/strong&gt; is how much light the picture is carrying. A dark doorway and a sunlit wall can have the same colors. Brightness is the difference between those two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contrast&lt;/strong&gt; is how far the light parts and the dark parts sit from each other. A foggy afternoon can be bright enough and still look flat, because the brightest cloud and the darkest tree are close together. Contrast is that gap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Saturation&lt;/strong&gt; is how far a color sits from gray. A navy shirt and a pale blue shirt can be the same lightness. Saturation is what separates them. A pixel with equal red, green, and blue is gray at every saturation setting.&lt;/p&gt;

&lt;p&gt;The three are independent questions. A dim photo can be highly saturated. A bright photo can be gray. A high-contrast photo can still have weak color. The formulas make that concrete, because each one refuses to touch the quantity the others are about, until a channel hits the 0 or 255 rail and the refusal breaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Brightness multiplies every channel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same gain is applied to red, green, and blue. At 0% the pixel is black. At 200% every channel is doubled. At 50% every channel is halved, which is darker, not a fade-to-white.&lt;/p&gt;

&lt;p&gt;Channel ratios stay put for as long as no channel clips. A pixel at &lt;code&gt;(120, 80, 40)&lt;/code&gt; and the same pixel at 150%, &lt;code&gt;(180, 120, 60)&lt;/code&gt;, are the same hue. The distance of each channel from the pixel's gray also scales with the gain, so chroma does rise, but only because the whole pixel got larger, not because color was pulled away from gray on purpose.&lt;/p&gt;

&lt;p&gt;Clipping is the part people notice. Once a channel reaches 255 it stops, and the other two may still be climbing. That pixel does change hue. On the coastal photo used later, 150% brightness pushed about half of the pixels onto the 0 or 255 rail. Mean luminance moved from 127 to 174. The clouds lost their gray shading and became flat white. The footpath and the shadows under the cars lifted, which is the actual brightness change. The grass looking a little stronger is a side effect of the multiply, plus clipping in the sky.&lt;/p&gt;

&lt;p&gt;This is not "add white." Adding white would move every channel toward 255 and pull the pixel toward gray. A gain keeps the ratios until the ceiling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contrast stretches distance from middle gray
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;128 is the midpoint of an 8-bit channel, not the average of the photo. Each channel moves away from that midpoint when the factor is above 1, and toward it when the factor is below 1. At 0% every channel becomes 128, so the picture is a flat mid-gray. At 200% the distance from 128 is doubled.&lt;/p&gt;

&lt;p&gt;A pixel lighter than middle gray gets lighter. A pixel darker than middle gray gets darker. On this photo the mean luminance barely moved, from 127.1 to 125.6, because the pivot sits near the middle of the encoding and the scene was already centered there. The hedge, the pine, and the cars went darker. The clouds went whiter. That is the contrast change: the same scene, a wider gap between its light and dark parts.&lt;/p&gt;

&lt;p&gt;Gaps between channels stretch too, because each channel is scaled around 128 on its own. A colored pixel therefore looks more saturated even though this formula never computes a gray value. Measured chroma rose from 64.8 to 87.9. About 43% of pixels landed on a rail. A gray pixel, equal channels, stays gray, because all three channels move together. Contrast can imitate a saturation boost on colored pixels, and it still will not colorize a gray one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Saturation scales distance from gray
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.2126&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.7152&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.0722&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;factor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;gray&lt;/code&gt; is Rec. 709 luminance: green counts for most of it, then red, then blue. That matches the contribution of those channels to perceived lightness. The gray part of the pixel is written back unchanged. Only the remainder, &lt;code&gt;channel - gray&lt;/code&gt;, is multiplied.&lt;/p&gt;

&lt;p&gt;At 0% the remainder is discarded and the pixel is &lt;code&gt;(gray, gray, gray)&lt;/code&gt;. Lightness is the same, color is gone. At 200% the remainder is doubled, so the pixel moves twice as far from gray in the same direction. Hue does not rotate. A blue pixel stays on the blue side of gray.&lt;/p&gt;

&lt;p&gt;On this photo the mean luminance stayed 127.1, the same number as the original, because the gray axis is held still and clipping was rare. Chroma rose from 64.8 to 97.0. About 4% of pixels hit a rail, mostly where a channel was already near the edge and the extra chroma pushed it over. The ocean and the lawn get stronger. The brightness of the road and the clouds stays close to the source. That is the signature of a saturation edit: color moves, lightness does not, until clipping forces a channel to stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  One photo, three edits at 150%
&lt;/h2&gt;

&lt;p&gt;The source is one coastal street, 760×428. Each edit is the full frame at 150% (&lt;code&gt;factor = 1.5&lt;/code&gt;), then a JPEG. The files were made with ROCKIMG's &lt;a href="https://www.rockimg.com/adjust-image-brightness.html" rel="noopener noreferrer"&gt;Adjust Image Brightness&lt;/a&gt;, &lt;a href="https://www.rockimg.com/adjust-image-contrast.html" rel="noopener noreferrer"&gt;Adjust Image Contrast&lt;/a&gt;, and &lt;a href="https://www.rockimg.com/adjust-image-saturation.html" rel="noopener noreferrer"&gt;Adjust Image Saturation&lt;/a&gt;. The arithmetic is the same one shown above.&lt;/p&gt;

&lt;p&gt;Original:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjzq7u9ucasvptdo6prbq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjzq7u9ucasvptdo6prbq.jpg" alt="Coastal street before any adjustment" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Brightness at 150%. The path, the walls, and the shaded grass are lighter. Cloud detail is gone where the channels hit 255.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1pvvhxi8gvmb48uojspx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1pvvhxi8gvmb48uojspx.jpg" alt="Same coastal street with brightness at 150 percent" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Contrast at 150%. Overall lightness is close to the original. Shadows under the trees and cars are deeper, and the clouds are harder white. The color looks stronger because channel gaps grew, not because saturation was the control.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqjf8ex67zt91eclxbcs5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqjf8ex67zt91eclxbcs5.jpg" alt="Same coastal street with contrast at 150 percent" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Saturation at 150%. The sea and the grass carry more color. The road and the clouds keep nearly the same lightness as the original, which is what holding luminance still looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbz0ktjon6bizzeaohcpl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbz0ktjon6bizzeaohcpl.jpg" alt="Same coastal street with saturation at 150 percent" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Edit at 150%&lt;/th&gt;
&lt;th&gt;Mean luminance&lt;/th&gt;
&lt;th&gt;Mean chroma&lt;/th&gt;
&lt;th&gt;Pixels pushed onto 0 or 255&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Original&lt;/td&gt;
&lt;td&gt;127.1&lt;/td&gt;
&lt;td&gt;64.8&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Brightness&lt;/td&gt;
&lt;td&gt;174.3&lt;/td&gt;
&lt;td&gt;73.9&lt;/td&gt;
&lt;td&gt;about 50%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Contrast&lt;/td&gt;
&lt;td&gt;125.6&lt;/td&gt;
&lt;td&gt;87.9&lt;/td&gt;
&lt;td&gt;about 43%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Saturation&lt;/td&gt;
&lt;td&gt;127.1&lt;/td&gt;
&lt;td&gt;97.0&lt;/td&gt;
&lt;td&gt;about 4%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Chroma here is the average of &lt;code&gt;|R − gray| + |G − gray| + |B − gray|&lt;/code&gt; with the same Rec. 709 gray as the saturation formula. It is a distance from gray, not a score of whether the picture looks good.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the three knobs will not do
&lt;/h2&gt;

&lt;p&gt;Raising brightness will not recover a flat picture. It lifts darks and lights together, and the lights run out of room first.&lt;/p&gt;

&lt;p&gt;Raising contrast will not reliably add color. It widens the gap around middle gray. Colored pixels often look richer afterward, and gray pixels stay gray.&lt;/p&gt;

&lt;p&gt;Raising saturation will not fix a dark exposure. Lightness is the term the formula is built to leave alone.&lt;/p&gt;

&lt;p&gt;Past the clip, none of the three is reversible. A channel that became 0 or 255 has lost the amount it was supposed to move, and the hue of that pixel is no longer the hue of the source. 150% on this frame is already enough to show that on brightness and contrast. Saturation still has headroom, which is why its luminance number matches the original and the other two do not.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>design</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Recolor One Region of an Image Without Flattening the Shading</title>
      <dc:creator>ROCKIMG</dc:creator>
      <pubDate>Mon, 19 Oct 2026 12:48:34 +0000</pubDate>
      <link>https://dev.to/rockimg/recolor-one-region-of-an-image-without-flattening-the-shading-5751</link>
      <guid>https://dev.to/rockimg/recolor-one-region-of-an-image-without-flattening-the-shading-5751</guid>
      <description>&lt;p&gt;A global hue slider is the wrong tool when only one part of a picture should change. The blue of an icon, the outline of a logo, or the flat background behind a character are regions. Everything else has to stay where it is, including the light and dark pixels inside the region you did touch.&lt;/p&gt;

&lt;p&gt;This post walks through a selection that starts from one pixel, and a recolor that replaces the color without turning the area into a single flat fill. The implementation is the &lt;a href="https://www.npmjs.com/package/recolor-region" rel="noopener noreferrer"&gt;&lt;code&gt;recolor-region&lt;/code&gt;&lt;/a&gt; package: RGBA bytes in, RGBA bytes out, no DOM and no dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  What counts as the region
&lt;/h2&gt;

&lt;p&gt;Start at the pixel the user clicked. That pixel is the seed. Its red, green, and blue values are the reference color.&lt;/p&gt;

&lt;p&gt;From there, walk to neighboring pixels. A neighbor joins the selection only when all of these are true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is inside the image.&lt;/li&gt;
&lt;li&gt;Its alpha is at least 16. Nearly transparent pixels are holes, not paint.&lt;/li&gt;
&lt;li&gt;Its alpha is within 96 of the seed alpha, so a soft shadow does not swallow the opaque shape next to it.&lt;/li&gt;
&lt;li&gt;The squared RGB distance to the seed stays inside the tolerance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distance test is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pixelR&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;seedR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pixelG&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;seedG&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pixelB&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;seedB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxDist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tolerance&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;tolerance&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dr&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;dr&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;dg&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;dg&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;maxDist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// too far from the clicked color&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tolerance&lt;/code&gt; is an integer from 0 to 255. The default is 28. Because the limit is &lt;code&gt;tolerance² * 3&lt;/code&gt;, the cutoff is the Euclidean distance &lt;code&gt;tolerance * √3&lt;/code&gt;. At 28 that is about 48.5, which is enough for a flat icon with anti-aliased edges and tight enough that a red logo sitting on blue does not leak into the blue.&lt;/p&gt;

&lt;p&gt;The walk is 8-connected: each pixel considers all eight neighbors, not only the four that share an edge. Corners of a diagonal stroke stay attached. Pixels are marked seen when they enter the queue, so each pixel is visited once. The queue is a typed array with a head and a tail index, which avoids the cost of &lt;code&gt;shift()&lt;/code&gt; on a large image.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fringe pass
&lt;/h2&gt;

&lt;p&gt;Anti-aliasing paints a one-pixel halo that is halfway between the shape and whatever is behind it. A strict tolerance often stops just inside that halo, and the recolor leaves a bright or dark rim.&lt;/p&gt;

&lt;p&gt;After the flood fill, a second pass looks only at pixels that touch the selection and are not yet selected. Those pixels may be a little farther from the seed: the allowed distance uses &lt;code&gt;tolerance + 18&lt;/code&gt; instead of &lt;code&gt;tolerance&lt;/code&gt;. Pixels that are not next to the selection are ignored, so the looser limit cannot jump across the picture and grab a similar color somewhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repainting without a flat fill
&lt;/h2&gt;

&lt;p&gt;Once the mask exists, the new color is not written as a constant RGB. A logo highlight is the same hue as the shadow under it, just lighter. If every selected pixel becomes &lt;code&gt;#E53935&lt;/code&gt;, the highlight disappears.&lt;/p&gt;

&lt;p&gt;The package estimates the region's base brightness first. Selected pixels are grouped into buckets of 16 levels per channel (a right shift of 4). The most common bucket is averaged, and that average is the sampled color. Using the dominant bucket, rather than every selected pixel, keeps a few fringe pixels from dragging the base toward the background.&lt;/p&gt;

&lt;p&gt;Relative luminance is the usual Rec. 709 weighting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;lum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.2126&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.7152&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.0722&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each selected pixel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keepShading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;lum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;outR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;outG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;outB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alpha is copied unchanged. &lt;code&gt;keepShading: false&lt;/code&gt; skips the scale and writes the target color as a flat fill, which is what you want for a solid plus icon and the wrong choice for a shaded mark.&lt;/p&gt;

&lt;p&gt;The clamp from 0.55 to 1.4 is deliberate. A near-black edge pixel would otherwise map to nearly black and look unpainted. A specular pixel would blow out past the target and clip in a harsh way. The window keeps the shading readable while the hue still reads as the new color.&lt;/p&gt;

&lt;p&gt;This is not a full hue rotation. Pixels inside the region lose their original hue and move toward one target. That is the point when the job is "make this logo red." It is the wrong model when the region itself contains several intentional hues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calling it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;recolor-region
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;recolorRegion&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;recolor-region&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recolorRegion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pixels&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#E53935&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;tolerance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;keepShading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pixels&lt;/code&gt; is any array of bytes with length &lt;code&gt;width * height * 4&lt;/code&gt;. &lt;code&gt;result.data&lt;/code&gt; is a new &lt;code&gt;Uint8ClampedArray&lt;/code&gt;. The input buffer is not modified. &lt;code&gt;result.mask&lt;/code&gt; is a &lt;code&gt;Uint8Array&lt;/code&gt; of 0 and 1, &lt;code&gt;result.selected&lt;/code&gt; is the count, and &lt;code&gt;result.sampled&lt;/code&gt; is the &lt;code&gt;{ r, g, b, hex }&lt;/code&gt; base color used for the shading scale.&lt;/p&gt;

&lt;p&gt;On a canvas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;willReadFrequently&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getImageData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recolorRegion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clickX&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clickY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#2563EB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putImageData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ImageData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;selectRegion(pixels, width, height, x, y, tolerance)&lt;/code&gt; returns only the mask, if you want to preview or edit the selection before painting. You can also pass that mask back in as &lt;code&gt;options.mask&lt;/code&gt; and skip the click.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it breaks
&lt;/h2&gt;

&lt;p&gt;Flood fill follows connectivity and color distance. It does not know what an object is.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A JPEG sky is a gradient plus compression noise. One click will not grab the whole sky, and a high tolerance will leak into the subject.&lt;/li&gt;
&lt;li&gt;Two shapes of the same color that do not touch are two regions. Shift-click, or call &lt;code&gt;selectRegion&lt;/code&gt; again and merge the masks yourself.&lt;/li&gt;
&lt;li&gt;The fringe pass only repairs a thin rim. A wide soft shadow still needs a brush, or a higher tolerance plus a careful seed.&lt;/li&gt;
&lt;li&gt;Very large images are just more pixels. The algorithm is linear in the number of pixels, but &lt;code&gt;getImageData&lt;/code&gt; on a multi-megapixel photo is the slow part. Downscale before editing if the long edge is above something like 1600 pixels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The useful test is a flat icon and a shaded logo on a transparent background. Click the color you mean to change, leave tolerance near 28, and keep shading on. If the rim is wrong, nudge tolerance before you reach for a brush.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
