<p>In order to replace a color with another color, you need some sort of <a href="https://en.wikipedia.org/wiki/Metric_(mathematics)" rel="nofollow noreferrer">distance metric</a> between the colors and a function for calculating the <a href="https://en.wikipedia.org/wiki/Blend_modes" rel="nofollow noreferrer">blending</a> based on that distance. </p>

<p>Finding a distance between 2 colors can be tricky. RGB is a bad color space to measure the distance between colors for perceptual uses. HSV or YCbCr are better. </p>

<p>Once you have the colors in a color space you like, you need to decide how to calculate the distance. I generally calculate the hue distance for starters. If the hue of the color I'm considering replacing is within +/- some threshold (possibly set by the user), then I check to see if the saturation and value are within a certain range, too. </p>

<p>Once I've found a color that's within the ranges specified, I calculate a blending amount. Usually this will be 100% if the color in the image exactly matches the color I want to replace, and will fall off to 0 at the thresholds of hue, saturation and brightness. </p>

<p>You then need to calculate the color you're going to use as a replacement color. It's useful to offset the hue, saturation and value (or Y, Cb, and Cr components) by the same amount as the color in the image differs from the replacement color.</p>

<p>So now you have the original color in the image, the color to replace it with, and a blending amount. You still need to figure out which blend mode you want to use. I would start with what Photoshop calls "Normal" or "Over". That would be a linear mix of the two. If you have colors A and B with a mix amount of 25%, it would be calculated like this:</p>

<p>newColor = A * .25 + B * (1.0 - .25);</p>

<p>Here are some relevant answers I gave to similar questions on Stack Overflow:</p>

<p><a href="https://stackoverflow.com/questions/9234724/how-to-change-hue-of-a-texture-with-glsl/9234854#9234854">How to Change the Hue of a Texture with GLSL</a></p>

<p><a href="https://stackoverflow.com/questions/9064556/better-image-coloring-logic-algorithm/9075340#9075340">Better Image Coloring Logic/Algorithm</a></p>

CC BY-SA 3.0
2016-04-22T05:32:42.897Z
2016-04-22T05:32:42.897Z
2017-05-23T12:39:19.850Z
user1118321
<p>Assuming you are looking to transform a particular component (r, g or b) of any color, this can be easily done by simple vector/matrix multiplication.</p>

<p>The introduction on this page (Chapter 6.1) - from the most excellent immersivemath.com site - illustrates this perfectly:
<a href="http://immersivemath.com/ila/ch06_matrices/ch06.html" rel="nofollow noreferrer">Chapter 6: The Matrix (Immersive Linear Algebra) | immersivemath.com</a></p>

<p>Think of it as the "Channel Mixer" command in PhotoShop.</p>

<p><strong>edit</strong></p>

<p>What I described above is one of many ways of changing a given color. The PS Color Replacement Tool simply applies that to any pixel that matches certain conditions defined by the user (source color, destination color, tolerance, etc...). The nifty thing they did is put it in the form of a brush tool, allowing the user artistic control over where to apply the color transform.</p>

<p>Basically it boils down to this: if a pixel under the brush matches the conditions set by the user the color transform is applied. If not, leave the pixel as is.</p>

CC BY-SA 3.0
2016-04-19T03:36:26.743Z
What I am trying to do is Color Replacement Tool in Photoshop. I donot know what Channel Mixer does. I am trying to replicate the Color Replacement Tool.
Sandiip
CC BY-SA 3.0
2016-04-18T14:39:31.007Z
2018-04-15T10:01:56.963Z
2018-04-15T10:01:56.963Z
Erik
<p>Suppose i have a image and I want to convert the portion of image that is red to blue(or any other color). One way to do this is extract red part of image using HSV or other color values and convert red hsv value to corresponding blue. Is there any other methods(better) for this. What sort of algorithms do Photoshop color replacement tool use?</p>

CC BY-SA 3.0
2016-04-18T09:33:19.480Z
Are you looking to replace regions of pure red with pure blue? Or are you looking to change the red component of each colour to blue (so an orange region becomes cyan)? Somewhere in between these two approaches, you could identify regions of the image that are "sufficiently red" and only apply the change to those regions. If you edit the question to describe more specifically what you require, we will be better able to suggest algorithms.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-04-18T09:40:28.387Z
I want to change pure red with pure blue maintaing the lightning and shades of color
Sandiip
CC BY-SA 3.0
2016-04-18T11:13:05.583Z
This sounds like you want to change more than just one precise colour. In this [picture of an apple](https://upload.wikimedia.org/wikipedia/commons/2/20/Civni-Rubens_apple.jpg) would you want all of the reddish parts to become bluish? Or would you want only the very reddest parts to become blue, and the rest of the reddish parts to remain reddish?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-04-18T11:33:21.457Z
i want to all red to become blue? for both wouldn't the algorithm differ only in the threshold taken for red
Sandiip
CC BY-SA 3.0
2016-04-18T13:31:29.027Z
Do you have an example before and after image?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-04-19T03:37:48.830Z
What i am trying to do is done in this tool http://explorug.net/coloranything/   I want to know what are the best methods to achieve this. This is same as PhotoShop's Color Replacement Tool.
Sandiip
<color><color-science><color-management>
Change particular color in an image
Sandiip
# Change particular color in an image

(Asked by: username_0 on 2016-04-18)

Suppose i have a image and I want to convert the portion of image that is red to blue(or any other color). One way to do this is extract red part of image using HSV or other color values and convert red hsv value to corresponding blue. Is there any other methods(better) for this. What sort of algorithms do Photoshop color replacement tool use?

**Tags:** color, color-science, color-management

> **Comments:**
> **username_1**: Are you looking to replace regions of pure red with pure blue? Or are you looking to change the red component of each colour to blue (so an orange region becomes cyan)? Somewhere in between these two approaches, you could identify regions of the image that are "sufficiently red" and only apply the change to those regions. If you edit the question to describe more specifically what you require, we will be better able to suggest algorithms.  
> **username_0**: I want to change pure red with pure blue maintaing the lightning and shades of color  
> **username_1**: This sounds like you want to change more than just one precise colour. In this [picture of an apple](https://upload.wikimedia.org/wikipedia/commons/2/20/Civni-Rubens_apple.jpg) would you want all of the reddish parts to become bluish? Or would you want only the very reddest parts to become blue, and the rest of the reddish parts to remain reddish?  
> **username_0**: i want to all red to become blue? for both wouldn't the algorithm differ only in the threshold taken for red  
> **username_1**: Do you have an example before and after image?  
> **username_0**: What i am trying to do is done in this tool http://explorug.net/coloranything/   I want to know what are the best methods to achieve this. This is same as PhotoShop's Color Replacement Tool.  

---

## 2 answers

### Answer by username_2 on 2016-04-22. Score: 4

In order to replace a color with another color, you need some sort of [distance metric](https://en.wikipedia.org/wiki/Metric_(mathematics)) between the colors and a function for calculating the [blending](https://en.wikipedia.org/wiki/Blend_modes) based on that distance.

Finding a distance between 2 colors can be tricky. RGB is a bad color space to measure the distance between colors for perceptual uses. HSV or YCbCr are better.

Once you have the colors in a color space you like, you need to decide how to calculate the distance. I generally calculate the hue distance for starters. If the hue of the color I'm considering replacing is within +/- some threshold (possibly set by the user), then I check to see if the saturation and value are within a certain range, too.

Once I've found a color that's within the ranges specified, I calculate a blending amount. Usually this will be 100% if the color in the image exactly matches the color I want to replace, and will fall off to 0 at the thresholds of hue, saturation and brightness.

You then need to calculate the color you're going to use as a replacement color. It's useful to offset the hue, saturation and value (or Y, Cb, and Cr components) by the same amount as the color in the image differs from the replacement color.

So now you have the original color in the image, the color to replace it with, and a blending amount. You still need to figure out which blend mode you want to use. I would start with what Photoshop calls "Normal" or "Over". That would be a linear mix of the two. If you have colors A and B with a mix amount of 25%, it would be calculated like this:

newColor = A \* .25 + B \* (1.0 - .25);

Here are some relevant answers I gave to similar questions on Stack Overflow:

[How to Change the Hue of a Texture with GLSL](https://stackoverflow.com/questions/9234724/how-to-change-hue-of-a-texture-with-glsl/9234854#9234854)

[Better Image Coloring Logic/Algorithm](https://stackoverflow.com/questions/9064556/better-image-coloring-logic-algorithm/9075340#9075340)


---

### Answer by username_3 on 2016-04-18. Score: 2

Assuming you are looking to transform a particular component (r, g or b) of any color, this can be easily done by simple vector/matrix multiplication.

The introduction on this page (Chapter 6.1) - from the most excellent immersivemath.com site - illustrates this perfectly:
[Chapter 6: The Matrix (Immersive Linear Algebra) | immersivemath.com](http://immersivemath.com/ila/ch06_matrices/ch06.html)

Think of it as the "Channel Mixer" command in PhotoShop.

**edit**

What I described above is one of many ways of changing a given color. The PS Color Replacement Tool simply applies that to any pixel that matches certain conditions defined by the user (source color, destination color, tolerance, etc...). The nifty thing they did is put it in the form of a brush tool, allowing the user artistic control over where to apply the color transform.

Basically it boils down to this: if a pixel under the brush matches the conditions set by the user the color transform is applied. If not, leave the pixel as is.

> **Comments:**
> **username_0**: What I am trying to do is Color Replacement Tool in Photoshop. I donot know what Channel Mixer does. I am trying to replicate the Color Replacement Tool.  

---

