views:

124

answers:

5

Hello,

I'm trying to find a way to compare two colors to find out how much they are alike. I can't seem to find any resources about the subject so I'm hoping to get some pointers here.

Idealy, I would like to get a score that tells how much they are alike. For example, 0 to 100, where 100 would be equal and 0 would be totally different.

Thanks!

Edit:

Getting to know a bit more about colors from the answers I understand my question was a bit vague. I will try to explain what I needed this for.

I have pixeldata (location and color) of an application window at 800x600 size so I can find out if a certain window is open or not by checking every x-interval.

However, this method fails as soon as the application is resized (the contents are scaled, not moved). I can calculate where the pixels move, but because of rounding and antialising the color can be slightly different.

Pieter's solution was good enough for me in this case, although all other responses were extremely helpfull as well, so I just upvoted everyone. I do think that ColorEye's answer is the most accurate when looking at this from a professional way, so I marked it as the answer.

+3  A: 

Colour perception depends on many factors and similarity can be measured in many ways. Just comparing how similar the R, G and B components are generally gives results humans won't agree with.

There's some general material on colour comparisons in wikipedia, and on working with natural colour spaces in C# in this question.

Pontus Gagge
+1  A: 

Something like this:

    public static int CompareColors(Color a, Color b)
    {
        return 100 * (int)(
            1.0 - ((double)(
                Math.Abs(a.R - b.R) +
                Math.Abs(a.G - b.G) +
                Math.Abs(a.B - b.B)
            ) / (256.0 * 3))
        );
    }
Pieter
Thanks, I think I can work from this for now, although I will need to see how accurate this is. There are a few mistakes in your code though (256 should be 255 and casting the result to an int isn't very smart either :D)
SaphuA
While mathematically sound this is not a good idea since it does not take into account how colors are perceived. You can easily find pairs of colors that are similar but will yield a low score, and colors that are dissimilar but will yield a high score.
kigurai
Yes you're right. You can go all out with this, but this is a quick and dirty way to get a proximate difference. I think the "correct" comparison methods would take a few pages :).
Pieter
I don't agree with whoever downvoted this answer. While not as accurate as ColorEyes's answer this was still usefull to me.
SaphuA
Well, thank you very much :).
Pieter
+3  A: 

Colors have different weights affecting human eye. So convert the colors to grayscale using their calculated weights:

Gray Color = .11 * B + .59 * G + .30 * R

And your difference will be (GrayColor1 - GrayColor2)*100.0/256.0

This is actually commonly used and very simple approach thats used calculating image differences in image procesing.

-edit this is the very simple and still usable formula - even in commercial applications. If you want to go deep you should check out the color difference methods called: CIE1976, CIE1994, CIE2000 and CMC Here you can find some more detailed info: http://en.wikipedia.org/wiki/Color_difference

honibis
That only compares luminosity without any regard for hue.
Hans Passant
Yep thats right. But its basic and it works in the real world. Edited the answer with some more info.
honibis
+4  A: 

Converting the RGB color to the HSL color space often produces good results. Check wikipedia for the conversion formula. It is up to you to assign weights to the differences in H, the color, S, how 'deep' the color is and L, how bright it is.

Hans Passant
Weight L heavier than the others for sure. Our eyes are far more sensitive to changes in brightness than changes in color.
Brad
+4  A: 

What you are looking for is called Delta-E.

http://www.colorwiki.com/wiki/Delta_E:_The_Color_Difference

It is the distance between two colors in LAB color space. It is said that the human eye cannot distinguish colors below 1 DeltaE (I find that my eyes can find differences in colors below 1 DeltaE, each person is different.)

There are 4 formulas for 'color difference'.

  • Delta E (CIE 1976)
  • Delta E (CIE 1994)
  • Delta E (CIE 2000)
  • Delta E (CMC)

Check the math link on this site:

  • http://www.brucelindbloom.com/

So the proper answer is to convert your RGB to LAB using the formula given, then use DeltaE 1976 to determine the 'difference' in your colors. A result of 0 would indicate identical colors. Any value higher than 0 could be judged by the rule 'A delta e of 1 or less is indistinguishable by most people'.

ColorEyes
Thanks for your reply, I was afraid it would be something as complicated as this. I will mark your answer since it is the most accurate, although will probably go for a solution much like Pieter's.
SaphuA