<blockquote>
<p>I have some colors in RGB in [0,1] and want to find a way to rate
their similarity, as percieved by a human.</p>
</blockquote>
<p>This is a huge subject, and loosely is found under the banner of <em>colour appearance models</em>. Why is it not strictly a more simple formation is due to the psychophysical nature of colour in that colour does not exist beyond the human organism.</p>
<blockquote>
<p>My first idea is to treat the RGB colors as XYZ points and calculate
their distance.</p>
<p>Another idea I have is to treat the RGB values as a histogram and use
dot product to get a similarity value between them, where a larger
value is better.</p>
<p>I know however, that not all of the color channels have the same
percieved brightness so maybe i ought to weight the color channels
differently for both cases?</p>
</blockquote>
<p>Best advice is, much like cryptography, do not roll your own; you are likely to arrive at a sub-optimal system that will, in the best of cases, hit walls already hit by other researchers in the field. If you base your work on existing models and research, you might find it to be more accurate for your needs[1].</p>
<p>One could point at the historical developments around CAMs, but it is easier here to suggest that you research the IPT colour encoding model and its cylindrical equivalent that models colourfulness and hue as an angle. The evolutions in the IPT model overcome most of the issues of the earlier Lab model, and simplifies some of the work involved in CIECAM02.</p>
<blockquote>
<p>Another challenge to this may be that different displays will display
the same color values differently. Not sure if that's relevant in this
case.</p>
</blockquote>
<p>IPT, and every RGB colour space for that matter, are anchored in the 1931 CIE research. As such, these sorts of issues are solved at a lower level.</p>
<p>[1] This expanded answer is due to Mr. Wolfe's comment below in an attempt to explain why rolling your own solution might be a sub-optimal approach.</p>

CC BY-SA 3.0
2017-01-15T19:34:55.247Z
Discouraging people from experimenting in graphics and putting it on par with shipping hand crafted crypto algorithms is ridiculous.
Alan Wolfe
CC BY-SA 3.0
2017-01-15T20:51:54.340Z
@AlanWolfe Given that there are quite a few extremely brilliant PhD types out there that have already spent countless hours and effort solving the problems in the original question, I find your context of ridiculous ridiculous. Not to discourage one from filling their boots and attempting to reinvent the wheel.
troy_s
CC BY-SA 3.0
2017-01-15T20:56:23.097Z
You should hear the simple hacks recommended by active graphics researchers. Such as "dot product RGB, it really does work amazingly well" from Peter Shirley.
Alan Wolfe
CC BY-SA 3.0
2017-01-15T20:58:05.607Z
@AlanWolfe I am sure there are some. Sadly the use of IPT and JCh for example, are almost trivial and yield remarkably good results. I edited the post and comment as I realized it read as pure snark. It wasn't intended as such. Dot product is also colour space dependent sadly, and it too fails.
troy_s
CC BY-SA 3.0
2017-01-15T21:02:01.033Z
Apologies if my response was also snark. Thanks for the info Troy!
Alan Wolfe
CC BY-SA 3.0
2017-01-15T21:03:17.003Z
@AlanWolfe I would be interested in the context of what you are trying to solve. There is a tremendous amount of top shelf research going on at the moment, specifically relating to game programming and motion picture work. I might be able to point to a more specific bit of code.
troy_s
CC BY-SA 3.0
2017-01-15T23:04:42.363Z
I've moved on but there's application of wave function collapse for procedural image and content creation. It works in part by exact matching pixels so works best with pixel art. I was looking at seeing it be able to do softer matching for use with more realistic images, or for less strict procedural content rules. Check out this link for the basic thing: https://github.com/mxgmn/WaveFunctionCollapse
Alan Wolfe
CC BY-SA 3.0
2017-01-15T23:25:11.960Z
@AlanWolfe Wow. I have already seen your work. It is pretty incredible. Would be remarkable to see you extend it.
troy_s
CC BY-SA 3.0
2017-01-15T23:26:58.830Z
It's not my work but I was trying to extend it. I totally agree, it's cool stuff!  Off topic but here's my unrelated work hehe. http://blog.demofox.org/2016/02/22/gpu-texture-sampler-bezier-curve-evaluation/
Alan Wolfe
CC BY-SA 3.0
2017-03-13T21:33:16.357Z
@AlanWolfe The research you linked is incredible! Thanks for posting.
Dan
CC BY-SA 3.0
2017-01-15T16:50:15.950Z
2017-01-15T20:55:49.967Z
2020-06-18T08:32:12.800Z
troy_s
<p>If complex metric is acceptable I would suggest to look at perceptual based approach described <a href="http://pdiff.sourceforge.net/" rel="nofollow noreferrer">here</a>. The metric is designed to pick perceptual difference of two images. There are two main tests for that : luminance based and color based. First one allows to answer the question how important luminance change is by estimating a non uniform threshold factor based on sensitivity to contrast changes depending on spatial frequencies of the image. The second one is based on euclidean distance in CIE LAB color space, but slightly modified to make color difference less important when luminance is in mesopic and scotopic ranges. A list of papers related to that metric can be found <a href="https://gist.github.com/reinsteam/9e6b3c05109b8bf121ec0f47252a2e1a" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 3.0
2016-11-28T13:15:20.570Z
Welcome to Computer Graphics SE! In general, link-only answers are strongly discouraged on SE, because they might become useless should those links ever go down. Please include a short summary of their content, so that people can still figure out what exactly you're actually suggesting without having to rely on the links.
Martin Ender
CC BY-SA 3.0
2016-11-28T12:52:08.143Z
2016-11-28T16:00:25.253Z
2016-11-28T16:00:25.253Z
reinsteam
<p>I have some colors in RGB in [0,1] and want to find a way to rate their similarity, as perceived by a human.</p>

<p>I have two ideas in mind, but I am sure there are other options as well, but am unsure which is best, or if perhaps there is no best, but only trade offs.</p>

<p>My first idea is to treat the RGB colors as XYZ points and calculate their distance.</p>

<p>Another idea I have is to treat the RGB values as a histogram and use dot product to get a similarity value between them, where a larger value is better.</p>

<p>I know however, that not all of the color channels have the same perceived brightness so maybe I ought to weight the color channels differently for both cases?</p>

<p>I'm also thinking I perhaps would need to do sRGB correction on the color values (such as, sqrt each color channel).</p>

<p>I also know other color spaces exist, so maybe one of those would be better at giving a similarity value.</p>

<p>Another challenge to this may be that different displays will display the same color values differently.  Not sure if that's relevant in this case.</p>

<p>Anyone able to provide some help/direction?</p>

CC BY-SA 4.0
2021-06-20T07:10:45.423Z
Convert color to preceptually uniform color spaces and then compute difference, like in - [Color Difference Calculator by Bruce Justin Lindbloom](http://Color%20Difference%20Calculator) - [in R using pev_hex_distance](https://ijlyttle.github.io/paleval/reference/pev_hex_distance.html)
Adam
CC BY-SA 3.0
2016-11-28T02:37:02.827Z
Have you taken a look at [this](https://en.wikipedia.org/wiki/Color_difference)? RGB isn't a great color space for doing comparisons related to human perception.
aces
CC BY-SA 3.0
2016-11-28T02:48:28.220Z
Good info thanks! I was looking at cielab but that article says that isn't the best.  I'm doing work with RGB source data unfortunately so have to figure out how to convert from RGB to something better, but the challenge seems to be that RGB is device dependant, while eg cielab are not.  Fortunately, a lesser approximation is good enough for my needs, if device independence isn't really feasible with RGB source data.
Alan Wolfe
CC BY-SA 3.0
2016-11-28T12:53:42.593Z
Check out Bruce Lindbloom's site, especiall the various DeltaE metrics: http://www.brucelindbloom.com/
David Kuri
CC BY-SA 3.0
2017-01-17T14:53:41.950Z
There is a whole Python package for color science that includes some transformations: http://colour-science.org/ .
KAE
<color>
What is a good color similarity metric?
Alan Wolfe
# What is a good color similarity metric?

(Asked by: username_0 on 2016-11-27)

I have some colors in RGB in [0,1] and want to find a way to rate their similarity, as perceived by a human.

I have two ideas in mind, but I am sure there are other options as well, but am unsure which is best, or if perhaps there is no best, but only trade offs.

My first idea is to treat the RGB colors as XYZ points and calculate their distance.

Another idea I have is to treat the RGB values as a histogram and use dot product to get a similarity value between them, where a larger value is better.

I know however, that not all of the color channels have the same perceived brightness so maybe I ought to weight the color channels differently for both cases?

I'm also thinking I perhaps would need to do sRGB correction on the color values (such as, sqrt each color channel).

I also know other color spaces exist, so maybe one of those would be better at giving a similarity value.

Another challenge to this may be that different displays will display the same color values differently. Not sure if that's relevant in this case.

Anyone able to provide some help/direction?

**Tags:** color

> **Comments:**
> **username_2**: Have you taken a look at [this](https://en.wikipedia.org/wiki/Color_difference)? RGB isn't a great color space for doing comparisons related to human perception.  
> **username_0**: Good info thanks! I was looking at cielab but that article says that isn't the best.  I'm doing work with RGB source data unfortunately so have to figure out how to convert from RGB to something better, but the challenge seems to be that RGB is device dependant, while eg cielab are not.  Fortunately, a lesser approximation is good enough for my needs, if device independence isn't really feasible with RGB source data.  
> **username_3**: Check out Bruce Lindbloom's site, especiall the various DeltaE metrics: http://www.brucelindbloom.com/  
> **username_4**: There is a whole Python package for color science that includes some transformations: http://colour-science.org/ .  
> **username_1**: Convert color to preceptually uniform color spaces and then compute difference, like in - [Color Difference Calculator by Bruce Justin Lindbloom](http://Color%20Difference%20Calculator) - [in R using pev_hex_distance](https://ijlyttle.github.io/paleval/reference/pev_hex_distance.html)  

---

## 2 answers

### Answer by username_5 on 2017-01-15. Score: 3 | Accepted answer

> I have some colors in RGB in [0,1] and want to find a way to rate
> their similarity, as percieved by a human.

This is a huge subject, and loosely is found under the banner of *colour appearance models*. Why is it not strictly a more simple formation is due to the psychophysical nature of colour in that colour does not exist beyond the human organism.

> My first idea is to treat the RGB colors as XYZ points and calculate
> their distance.
>
> Another idea I have is to treat the RGB values as a histogram and use
> dot product to get a similarity value between them, where a larger
> value is better.
>
> I know however, that not all of the color channels have the same
> percieved brightness so maybe i ought to weight the color channels
> differently for both cases?

Best advice is, much like cryptography, do not roll your own; you are likely to arrive at a sub-optimal system that will, in the best of cases, hit walls already hit by other researchers in the field. If you base your work on existing models and research, you might find it to be more accurate for your needs[1].

One could point at the historical developments around CAMs, but it is easier here to suggest that you research the IPT colour encoding model and its cylindrical equivalent that models colourfulness and hue as an angle. The evolutions in the IPT model overcome most of the issues of the earlier Lab model, and simplifies some of the work involved in CIECAM02.

> Another challenge to this may be that different displays will display
> the same color values differently. Not sure if that's relevant in this
> case.

IPT, and every RGB colour space for that matter, are anchored in the 1931 CIE research. As such, these sorts of issues are solved at a lower level.

[1] This expanded answer is due to Mr. username_0's comment below in an attempt to explain why rolling your own solution might be a sub-optimal approach.

> **Comments:**
> **username_0**: Discouraging people from experimenting in graphics and putting it on par with shipping hand crafted crypto algorithms is ridiculous.  
> **username_5**: @username_0 Given that there are quite a few extremely brilliant PhD types out there that have already spent countless hours and effort solving the problems in the original question, I find your context of ridiculous ridiculous. Not to discourage one from filling their boots and attempting to reinvent the wheel.  
> **username_0**: You should hear the simple hacks recommended by active graphics researchers. Such as "dot product RGB, it really does work amazingly well" from Peter Shirley.  
> **username_5**: @username_0 I am sure there are some. Sadly the use of IPT and JCh for example, are almost trivial and yield remarkably good results. I edited the post and comment as I realized it read as pure snark. It wasn't intended as such. Dot product is also colour space dependent sadly, and it too fails.  
> **username_0**: Apologies if my response was also snark. Thanks for the info Troy!  
> **username_5**: @username_0 I would be interested in the context of what you are trying to solve. There is a tremendous amount of top shelf research going on at the moment, specifically relating to game programming and motion picture work. I might be able to point to a more specific bit of code.  
> **username_0**: I've moved on but there's application of wave function collapse for procedural image and content creation. It works in part by exact matching pixels so works best with pixel art. I was looking at seeing it be able to do softer matching for use with more realistic images, or for less strict procedural content rules. Check out this link for the basic thing: https://github.com/mxgmn/WaveFunctionCollapse  
> **username_5**: @username_0 Wow. I have already seen your work. It is pretty incredible. Would be remarkable to see you extend it.  
> **username_0**: It's not my work but I was trying to extend it. I totally agree, it's cool stuff!  Off topic but here's my unrelated work hehe. http://blog.demofox.org/2016/02/22/gpu-texture-sampler-bezier-curve-evaluation/  
> **username_6**: @username_0 The research you linked is incredible! Thanks for posting.  

---

### Answer by username_7 on 2016-11-28. Score: 4

If complex metric is acceptable I would suggest to look at perceptual based approach described [here](http://pdiff.sourceforge.net/). The metric is designed to pick perceptual difference of two images. There are two main tests for that : luminance based and color based. First one allows to answer the question how important luminance change is by estimating a non uniform threshold factor based on sensitivity to contrast changes depending on spatial frequencies of the image. The second one is based on euclidean distance in CIE LAB color space, but slightly modified to make color difference less important when luminance is in mesopic and scotopic ranges. A list of papers related to that metric can be found [here](https://gist.github.com/username_7/9e6b3c05109b8bf121ec0f47252a2e1a).

> **Comments:**
> **username_8**: Welcome to Computer Graphics SE! In general, link-only answers are strongly discouraged on SE, because they might become useless should those links ever go down. Please include a short summary of their content, so that people can still figure out what exactly you're actually suggesting without having to rely on the links.  

---

