<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tejesh Reddy</title>
    <description>The latest articles on DEV Community by Tejesh Reddy (@tejeshreddy).</description>
    <link>https://dev.to/tejeshreddy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F166404%2F7deaceb7-b603-4bd2-9258-8484132bf8c1.jpeg</url>
      <title>DEV Community: Tejesh Reddy</title>
      <link>https://dev.to/tejeshreddy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tejeshreddy"/>
    <language>en</language>
    <item>
      <title>Difference Between 2 Colours Using Python</title>
      <dc:creator>Tejesh Reddy</dc:creator>
      <pubDate>Sat, 12 Sep 2020 13:13:49 +0000</pubDate>
      <link>https://dev.to/tejeshreddy/color-difference-between-2-colours-using-python-182b</link>
      <guid>https://dev.to/tejeshreddy/color-difference-between-2-colours-using-python-182b</guid>
      <description>&lt;p&gt;Recently, I needed to compare a number of colours to get the 2 most identical colours. This problem at the facade looks like a cakewalk, even for beginners, but is a bit involved.&lt;br&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A naive implementation is to calculate to Euclidean distance (as shown below) between the RGB values of the 2 colours.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;d=sqrt((x1−x2)^2 + (y1−y2)^2 + (z1−z2)^2)&lt;/code&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;The 2 colours that have the lowest Euclidean Distance are then selected. But, there is a serious flaw in this assumption. Although RGB values are a convenient way to represent colours in computers, we humans perceive colours in a different way from how colours are represented in the RGB colour space. What looks like identical colours to us, might give us a Euclidean distance that is greater than the Euclidean distance of colours that look different to us when comparing in RGB colour space.&lt;/p&gt;



&lt;h2&gt;
  
  
  So, what to do now?
&lt;/h2&gt;

&lt;p&gt;Delta-E distance metric comes to our rescue. It uses the CIE Lab colour space which approximates how we perceive colours, although it's also not fully accurate. Once we represent the colour in the CIE colour space, we can calculate the Delta-E distance metric using Euclidean Distance. Ever since its release in 1976, it has been modified 2 times to cope with shortcomings of the previous versions. The diagram below shows that by shifting along the x and y-axis we get different shades of the same colour.&lt;br&gt;
&lt;em&gt;CIE76 -&amp;gt; CIE94 -&amp;gt; CIE2000&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fhanzratech.in%2Ffigures%2Fcolor-space.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fhanzratech.in%2Ffigures%2Fcolor-space.gif" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;For, this tutorial we will use CIE2000. If you are interested in the actual mathematics, refer to the Wikipedia page. It’s is pretty easy to write your own code to compare the 2 colours, but we’ll not try to reinvent the wheel. Python provides a high utility package &lt;code&gt;colormath&lt;/code&gt; to convert between different colorspaces, delta E comparison, density to spectral operation etc.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;colormath&lt;/code&gt;, we just don’t need to put in much effort to find the Delta-E metric.&lt;/p&gt;

&lt;p&gt;The module &lt;code&gt;colormath&lt;/code&gt; can be installed using pip -&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;python-pip
&lt;span class="nb"&gt;sudo &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;colormath


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let’s write the actual python code to find the difference between 2 colours.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;colormath.color_objects&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sRGBColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LabColor&lt;/span&gt;&lt;br&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;colormath.color_conversions&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;convert_color&lt;/span&gt;&lt;br&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;colormath.color_diff&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;delta_e_cie2000&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;# Red Color&lt;br&gt;
&lt;/span&gt;&lt;span class="n"&gt;color1_rgb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sRGBColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;# Blue Color&lt;br&gt;
&lt;/span&gt;&lt;span class="n"&gt;color2_rgb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sRGBColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;# Convert from RGB to Lab Color Space&lt;br&gt;
&lt;/span&gt;&lt;span class="n"&gt;color1_lab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;convert_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color1_rgb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LabColor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;# Convert from RGB to Lab Color Space&lt;br&gt;
&lt;/span&gt;&lt;span class="n"&gt;color2_lab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;convert_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color2_rgb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LabColor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;# Find the color difference&lt;br&gt;
&lt;/span&gt;&lt;span class="n"&gt;delta_e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;delta_e_cie2000&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color1_lab&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color2_lab&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The difference between the 2 color = &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delta_e&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  References&lt;br&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/9018016/how-to-compare-two-colors-for-similarity-difference" rel="noopener noreferrer"&gt;Stack Overflow Thread&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/colormath/" rel="noopener noreferrer"&gt;PyPi colormath Package&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>datascience</category>
      <category>python</category>
    </item>
  </channel>
</rss>
