halostatue / color

Color tools for Ruby.
Other
133 stars 41 forks source link

difference between color X and Y is reported as not equal to difference between colors Y and X #22

Closed matkoniecz closed 9 years ago

matkoniecz commented 9 years ago

I would expect

require 'color'

lab_a = Color::RGB::DarkSalmon.to_lab
lab_b = Color::RGB::Blue.to_lab
rgb = Color::RGB::Blue
puts rgb.delta_e94(lab_a, lab_b)
puts rgb.delta_e94(lab_b, lab_a)

to print the same value twice (as distance between colour is the same in both cases as colours are the same). Bur result is

85.6075926403573 54.11289673323268

I found one suspicious fragment of code -

s_C = 1 + k_1 * c_1 s_H = 1 + k_2 * c_1

in https://github.com/halostatue/color/blob/master/lib/color/rgb.rb#L432 that seems to break symmetry between the first and the second color.

But changing second c_1 to c_2 is not fixing lack of symmetry (though in general difference is reduced) - result is

62.44677425056029 79.73246450623749

halostatue commented 9 years ago

I don’t know with absolute certainty, but I think that this is (unfortunately) the way that the math works out. Most colour math is an approximation, not something that guarantees transitivity.

For colour 2.x, I’m going to be moving as much as possible to matrix math, but I haven’t had a chance to get as familiar with the math involved as I should be before embarking on a rewrite. This may not make things any better.

grv87 commented 9 years ago

See the name of the algorithm and the reference mentioned in the comment: https://en.wikipedia.org/wiki/Color_difference#CIE94. I've checked implementation and found no differences with Wikipedia.

It's not a bug, but a known property of Delta-E 94 algorithm. See e.g. http://mathematica.stackexchange.com/questions/86730/is-colordistance-symmetric. I believe that quote from here about CMC algorithm can be applied to CIE 94 as well:

the formula is not symmetric, but depends on a standard and a test colour.
Switching standard and test colors changes the difference.
matkoniecz commented 9 years ago

Thanks! Sorry for spurious bug report.