Is there a way to calculate how far apart two colors are?

Discussion in 'General Science & Technology' started by A C Kerr, Jun 12, 2018.

  1. A C Kerr Registered Member

    Messages:
    8
    Suppose I have a shade of blue with these values for the RGB attributes: 143 179 221. And suppose I have several other shades of blue, each with their RGB settings. I would like a way to calculate how close they are to the target shade. Is there an accepted way to do that? The RGB values for the other shades are in the table below.

    Code:
     R      G      B   Sum(Diff) Sum(Sqrs)
    143    179    221       0      0
    130    166    205     -42     24
    167    188    214     +26     27
    139    159    197     -48     31
    172    186    245     +60     38
    
    I tried two methods. One, Sum(Diff), was to simply add up the differences. That is a poor choice. If R & G are each off by +100 and B is off by -200, the Sum(Diff) will be zero.

    My next method was to add up the square of the differences, Sum(Sqrs). This doesn't have the problem of Sum(Diff), but I'm not sure if it is a good metric.

    Any suggestions?

    Thanks
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. iceaura Valued Senior Member

    Messages:
    26,870
    If your idea is to match human perception of some small variation, note that most human perception (any sense except smell) will be logarithmic - so not only taking the square root of the arithmetic mean of the sum of the squares (rms, https://en.wikipedia.org/wiki/Root_mean_square), but taking some kind of logarithm of that, might be worth trying.

    If this is for an AI learning program to match human visual perception in general - a bot that sees as humans see - you will need to include specific information about the position on the frequency spectrum. Small differences toward the red end will not register the same as small differences in the middle or toward blue (the R, B, and G will need separate transformation before total sum or rms calculation).

    None of that is "the accepted way" of doing anything, afaik.
     
    Last edited: Jun 12, 2018
    A C Kerr likes this.
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. iceaura Valued Senior Member

    Messages:
    26,870
    Overlooked: the adjustments you need to match human perception - if that's what you are going for - may be built into those scale values for the RBG code. Somebody may have done the work for you, and an rms calculation is all you need. Just a thought - -
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. A C Kerr Registered Member

    Messages:
    8
    Yes, my objective is to try to approximate how a human (with good vision) would rank order the shades of a color.

    After I implemented an RMS metric, I noticed that it is smaller that the Sum Squares in every case, but they are in the same order. So I took the ratio. The RMS metric is exactly 57.735% of the Sum Squares metric. That makes sense since the RMS is the square root of the sum of the squares of the three differences divided by 3, whereas the Sum Squares is that same thing, but without dividing by 3. So the ration is 1/sqrt(3) = .57735....

    Naw, I'm not nearly smart enough to do that.

    Please Register or Log in to view the hidden image!



    Isn't that what the RGB values are? A color with RGB values of 000 127 255 would have 0% red, 50% green, and 100% blue.

    I took the (000 127 255) color and (a) added 30 points to R (30), (b) added 3o points to G (157), (c) subtracted 3o points from G (97), and (d) subtracted 30 points from B (225). Of the 4 results, I could just barely see the difference in the R & G values (near the end of the spectrum), but both changes in G were easily visible. So, as you say, it appears that changes in the middle are perceived as greater than those at the ends for the same absolute change.

    I think that this means that any logarithms will need to be two-tailed.

    Yes, because they will be at different points on the spectrum (closer to one end or the other and closer or farther from the middle).

    Please Register or Log in to view the hidden image!



    For my purposes,
     
  8. gebobs Registered Member

    Messages:
    36
    I work in textiles and often have to compare shades usually to prove that the process hasn't changed from lot to lot. I use a Hunter Labs spectrophotometer and prefer to use the L*a*b* color space system rather than RGB.

    Please Register or Log in to view the hidden image!



    At any rate, I think you are on the right track with the sum of squares. We do much the same. We measure a sample and it gives us the ΔL, Δa, and Δb from a known standard. We convert that to a single metric, ΔE, which is sqrt(ΔL^2+Δa^2+Δb^2). Basically a Pythagorean calculation in three dimensions.
     
    A C Kerr likes this.
  9. DaveC426913 Valued Senior Member

    Messages:
    9,184
    For starters, when you are trying to figure out how far apart two things are, drop the +/- sign. Your goal is to measure magnitude not direction. Magnitudesare unsigned values.

    If blue is off by -200 that means it is a really off by 200.
     
  10. A C Kerr Registered Member

    Messages:
    8
    Interesting. You are primarily interested in whether ΔE is close to zero, right? I care about that, but also about where several samples line up relative to each other relative to the standard.

    And what do you think about icearua's point about the three scales not being linear?
     
  11. DaveC426913 Valued Senior Member

    Messages:
    9,184
    That is correct.

    Just Google for images of RGB sensitivity in humans. The curves are all over the place like roller coasters.
     
  12. gmilam Valued Senior Member

    Messages:
    2,984
    Not to mention additive and subtractive primaries. Red, Green and Blue works for light. However with pigments your primary colors are Magenta, Cyan and Yellow.
     
  13. DaveC426913 Valued Senior Member

    Messages:
    9,184
    and Black

    Please Register or Log in to view the hidden image!

     
  14. A C Kerr Registered Member

    Messages:
    8
    For starters, my goal may not be what you think it is.

    Please Register or Log in to view the hidden image!



    If you are referring to the Sum(Diff) column in my table, I kept the signs intentionally. These are colors, so the direction indicates whether the difference is a darker or lighter color, something I care about.

    As a quick experiment, I set the target color to 127 127 127, a medium shade of grey. I then generated all of the 12 combinations adding or subtracting 50 from two of the values and 100 from the other. In the 6 examples where the 50s are the opposite sign from the 100, the colors are all about the same darkness. In the 3 where the 50s and the 100 are all positive, the colors are distinctly lighter. And in the 3 where the 50s and the 100 are all negative, the colors are so dark that I had to change the color of the text to yellow to be able to read it.

    I think that's a little like saying that if I am programming my self-driving car to go to the edge of the Grand Canyon and I find that it is missing the mark by 100 yards, I don't care whether it's +100 or -100. It's just "100". Right?

    Please Register or Log in to view the hidden image!

     
  15. gebobs Registered Member

    Messages:
    36
    Usually we are looking to minimize variation but there are times we want to discern which shade is bluer, or greener, or whiter. In those cases, the raw Lab values are used e.g. L=10 is lighter than L=30 and b=-180 is more blue than b=-150.

    Lab color space was designed to approximate human vision in linear 3D. On the other hand, RGB models the output of physical devices such as your monitor.

    As for human perception of color, it's variable as I'm sure you know. No two people have the same cones, numbers of them, and alignments. Personally,
    I have red-green protanomaly. Reds don't stand out to me and I am over sensitive to greens, yellows, and oranges. I have particular trouble discerning green, red, and brown.
     
    A C Kerr likes this.
  16. Kittamaru Never cruel nor cowardly... Staff Member

    Messages:
    13,866
    I am shade deficient myself - certain shades that other people see as obviously and evidently different appear extremely similar to me, especially in the darker end of things. It isn't until they are side by side that I can be certain of what is what.

    I can't imagine being, say, Red/Green blind or the like. Must be a giant pain in the ass.
     
  17. A C Kerr Registered Member

    Messages:
    8
    I think I have better than average ability to discern colors and shades. I just have absolutely no idea of what goes with what. I am never allowed to dress myself if we are going out with friends.
     
    gebobs likes this.
  18. A C Kerr Registered Member

    Messages:
    8
    Especially at traffic lights.

    Please Register or Log in to view the hidden image!

    Please Register or Log in to view the hidden image!



    When I was in New Mexico a long time ago, I noticed that the traffic lights are horizontal. So if you are red/green blind and dyslexic, ...

    "If life gives you melons, you may be dyslexic"
     
  19. gebobs Registered Member

    Messages:
    36
    Sounds like you have some degree of color blindness. You should take a test. You can do it online.

    Color blindness rarely means colors are completely indiscernible.
     
  20. DaveC426913 Valued Senior Member

    Messages:
    9,184
    You told us what your goal is when you described how you were trying to solve it.

    Magnitude does not have a direction. i.e. the distance between two things is an absolute value (no +/- sign)

    To wit:

    No.
    If A is +100 and B is -100, then the distance between them is not zero - the distance is 200.

    Programmatically: var dist = ABS(A) + ABS(B);

    You will have some work to expand that to 3 variables, but the principle is the same.
     
    Last edited: Jun 13, 2018
  21. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,829
    Colors of light differ in wavelength & frequency. These values indicate differences between two colors.

    The difference in either seems usable for specifying how far apart two colors are.

    Perhaps we should be using difference between or some other term instead of how far apart when referring to colors. How far apart implies spacial distance.
     
  22. DaveC426913 Valued Senior Member

    Messages:
    9,184
    The OP did specify that he's interested in the colour space, specifically as it relates to perception.

    I mean you might be on to something, but that would have to come from the OP redefining what suits his purpose.
     
  23. A C Kerr Registered Member

    Messages:
    8
    Wavelength and frequency are different forms of the same thing.
    Code:
    λν=c
    where λ is wavelength, ν is frequency, and c is the speed of light.

    I think you are being too literal with "far apart". If two people are "far apart" or even "miles apart" in their political views, is that a spacial distance?

    In any case, how would using "difference between" instead of "how far apart" change how we would approach the problem of determining which of two shades of blue are closer to some reference shade of blue? Does this lead us to a new column in my original table that will rank the colors represented by the RGB values in rows 2-5 on how "close" they are in either distance or farness from the reference color in row 1?
     

Share This Page