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?
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:
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
Think of it as the "Channel Mixer" command in PhotoShop.
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.