views:

660

answers:

3

Hi, I got array of some (about 200) colours in RGB format. I want to write program that taking any RGB colour and trying to match colour from my array that is most "similar".

Of course I need better definition for "similar". Unfortunately I don't have any. It want it to act like a parson asked the same question.

I want to show some infos about matching accuracy. For example black-white -100% and for the same colour but little different hue -4%.

So maybe I should use neural network? But I think it must be easer way.

+12  A: 

Convert all of the colors to the CIE Lab color space and compute the distance in that space

deltaE = sqrt(deltaL^2 + deltaA^2 + deltaB^2)

Colors with the lowest deltaE are the most perceptually similar to each other.

hobbs
Thank You, that is exactly what I need.
Maciek Sawicki
Keep in mind you don't need to do the sqrt - sqrt is an increasing function, therefore this step is superfluous.
Rooke
You're right, if you're doing nothing more than sorting, the square of the distance is as good as the distance itself. If you want to compare "how different', then leave it in.
hobbs
CIE Lab is used in exactly this manner to do nearest-color calculations in all the major color management systems, such as the ones from Apple, Microsoft, and Adobe. It's a very interesting topic.
Bob Murphy
+3  A: 

No, you do not need neural networks here! Simply consider an HSL color value a vector and define a weighted modulus function for the vector like this:

modulus = sqrt(a*H1*H1 + b*S1*S1 + c*L1*L1);

where a,b,c are weights you should decide based on your visual definition of what
creates a bigger difference in perceived color - a 1% change in Hue or a 1%
change in Saturation

I would suggest you use a = b = 0.5 and c = 1

Finally, find out the range your modulus would take and define similar colors to be those which have their moduli very close to each other (say 5%)

Crimson
That's a good simple alternative. The conversion from RGB to HSL is a lot simpler than the conversion from RGB to Lab. :)
hobbs
Ok, so I will try it first.
Maciek Sawicki
Crimson, can you check the math on your modulus there? I don't think it's right. You want something more like `a * (H1 - H2)**2 + `..., yeah?
hobbs
@hobbs - it would be better to calculate both moduli and then compare them rather than just compute the modulus of the difference vector
Crimson
But you don't want to multiply the hues, etc. of the two different colors, do you?
hobbs
+1  A: 

I'd also point out the least squares method, just as something slightly simpler. That is, you take the difference of a number, square it, then sum all these squared differences.

Rooke