Compare RGB colors in c#

28,389

Solution 1

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:

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'.

Solution 2

There's an open-source .net library that lets you do this easily: https://github.com/hvalidi/ColorMine

The most common method for comparing colors is CIE76:

var a = new Rgb { R = 149, G = 13, B = 12 }
var b = new Rgb { R = 255, G = 13, B = 12 }

var deltaE = a.Compare(b,new Cie1976Comparison());

Solution 3

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

difference = (GrayColor1 - GrayColor2) * 100.0 / 255.0

with difference ranging from 0-100.

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

Solution 4

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))
        );
    }

Solution 5

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.

Share:
28,389

Related videos on Youtube

SaphuA
Author by

SaphuA

Updated on July 09, 2022

Comments

  • SaphuA
    SaphuA almost 2 years

    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.

    • Klaus Byskov Pedersen
      Klaus Byskov Pedersen over 13 years
      What is your definition of alike?
    • Daniel Mošmondor
      Daniel Mošmondor over 13 years
      you should really tell us what do you want to accomplish here? you are diff-ing according to the human eye perception, or something else is in order?
  • SaphuA
    SaphuA over 13 years
    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)
  • Hannes Ovrén
    Hannes Ovrén over 13 years
    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.
  • Pieter van Ginkel
    Pieter van Ginkel over 13 years
    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 :).
  • honibis
    honibis over 13 years
    Yep thats right. But its basic and it works in the real world. Edited the answer with some more info.
  • SaphuA
    SaphuA over 13 years
    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
    SaphuA over 13 years
    I don't agree with whoever downvoted this answer. While not as accurate as ColorEyes's answer this was still usefull to me.
  • Brad
    Brad over 13 years
    Weight L heavier than the others for sure. Our eyes are far more sensitive to changes in brightness than changes in color.
  • Hannes Ovrén
    Hannes Ovrén almost 13 years
    @SaphuA: It might be "correct" in some cases, but it is still very wrong. For a quick example, the colors 0x7f0000 and 0x007f00 will yield (approximately) the same results as comparing 0xb2b2b2 and 0xffffff. In other words: A bright red and bright green are considered to be just as close as white and a quite light grey. That is not likely a result you want.
  • rsbarro
    rsbarro almost 12 years
    This site has some useful conversion algorithms (see easyrgb.com/index.php?X=MATH and easyrgb.com/index.php?X=DELT).
  • hruske
    hruske over 8 years
    Note that in Lab color space CIE 1976 is just euclidean distance between points, so DE = sqrt((L2-L1)^2 + (a2-a1)^2 + (b2-b1)^2).
  • Rev
    Rev about 4 years
    To have a result ranging from 0-100, the divider needs to be 255 not 256. I will fix the answer.
  • Devolus
    Devolus over 3 years
    The range can not be 0-100 because if you have RGB = (0,0,0) and compare it against another color, the result will always be negative. So the actual range wold be -100 - 100, unless some info in the text is missing.