Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Computing distances between RGB colours, in a way that's meaningful to the eye, isn't as easy a just taking the Euclidian distance between the two RGB vectors.</p> <p>There is an interesting article about this here: <a href="http://www.compuphase.com/cmetric.htm" rel="nofollow noreferrer">http://www.compuphase.com/cmetric.htm</a></p> <p>The example implementation in C is this:</p> <pre><code>typedef struct { unsigned char r, g, b; } RGB; double ColourDistance(RGB e1, RGB e2) { long rmean = ( (long)e1.r + (long)e2.r ) / 2; long r = (long)e1.r - (long)e2.r; long g = (long)e1.g - (long)e2.g; long b = (long)e1.b - (long)e2.b; return sqrt((((512+rmean)*r*r)&gt;&gt;8) + 4*g*g + (((767-rmean)*b*b)&gt;&gt;8)); } </code></pre> <p>It shouldn't be too difficult to port to Python.</p> <p><strong>EDIT:</strong></p> <p>Alternatively, as suggested in <a href="https://stackoverflow.com/a/4171566/372643">this answer</a>, you could use <a href="http://en.wikipedia.org/wiki/HSL_and_HSV" rel="nofollow noreferrer">HLS and HSV</a>. The <a href="http://docs.python.org/library/colorsys.html" rel="nofollow noreferrer"><code>colorsys</code></a> module seems to have functions to make the conversion from RGB. Its documentation also links to these pages, which are worth reading to understand why RGB Euclidian distance doesn't really work:</p> <ul> <li><a href="http://www.poynton.com/ColorFAQ.html" rel="nofollow noreferrer">http://www.poynton.com/ColorFAQ.html</a></li> <li><a href="http://www.cambridgeincolour.com/tutorials/color-space-conversion.htm" rel="nofollow noreferrer">http://www.cambridgeincolour.com/tutorials/color-space-conversion.htm</a></li> </ul> <p><strong>EDIT 2:</strong></p> <p>According to <a href="https://stackoverflow.com/a/3565191/372643">this answer</a>, this library should be useful: <a href="http://code.google.com/p/python-colormath/" rel="nofollow noreferrer">http://code.google.com/p/python-colormath/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload