11

Consider these two pink square:

enter image description here

And this:

enter image description here

As you may know, one is lighter and one is darker or more sharp. The problem is, I can tell it by human eyes, but is this possible to use a system way or programme way to detect this information? At least, is this possible to have a value that tell me that colour is more like white or that colour is less like white? (Assume I got the RGB code of that colour.) Thanks.

Community
  • 1
  • 1
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • I've added few tags to your question, but if you are looking to detect the color comparison apart from the language I tagged, than please add the relevant tag of that language – Mr. Alien Mar 24 '14 at 07:51
  • [`Relative luminance follows the photometric definition of luminance, but with the values normalized to 1 or 100 for a reference white.`](https://en.wikipedia.org/wiki/Luminance_(relative)) – Some Guy Mar 24 '14 at 08:08
  • 1
    Yes it's possible, but to get the right answer you need to provide more information. Are we talking about reading an image file? if so, is the image is made of only one color? What language are we talking about? – Gil Mar 24 '14 at 08:10
  • I just want to compare two pixel, and...I think it can be apply to all language, so, no special language is mentioned. – DNB5brims Mar 24 '14 at 09:28
  • In JavaScript, you can refer to http://stackoverflow.com/questions/12043187/how-to-check-if-hex-color-is-too-black/32442062#32442062 – skalee Sep 07 '15 at 16:21

2 Answers2

15

Below is the Python code to determine light or dark color. The formulation is based on the HSP value. HSP (Highly Sensitive Poo) equation is from http://alienryderflex.com/hsp.html used to determine whether the color is light or dark.

import math
def isLightOrDark(rgbColor=[0,128,255]):
    [r,g,b]=rgbColor
    hsp = math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b))
    if (hsp>127.5):
        return 'light'
    else:
        return 'dark'
Kardi Teknomo
  • 1,375
  • 16
  • 24
  • 1
    Just a minor optimization: instead of taking the square root in the hsp calculation, you can just compare the non-square-rooted result to the squared value of the current constant: `127.5 * 127.5` = `16256.25` or `16256`, depending on how precise you want to be. – Chris Jul 21 '21 at 16:07
  • @Chris indeed you can remove the square root from both sides of the equation. The `127.5` results of `sqrt((1*255^2) / 4)` – mindlid May 19 '22 at 19:28
5

Since you haven't specified any particular language/script to detect the darker/lighter hex, I would like to contribute a PHP solution to this

Demo

$color_one = "FAE7E6"; //State the hex without #
$color_two = "EE7AB7";

function conversion($hex) {
    $r = hexdec(substr($hex,0,2)); //Converting to rgb
    $g = hexdec(substr($hex,2,2));
    $b = hexdec(substr($hex,4,2));

    return $r + $g + $b; //Adding up the rgb values
}

echo (conversion($color_one) > conversion($color_two)) ? 'Color 1 Is Lighter' : 'Color 1 Is Darker';
//Comparing the two converted rgb, the greater one is darker

As pointed out by @Some Guy, I have modified my function for yielding a better/accurate result... (Added Luminance)

function conversion($hex) {
    $r = 0.2126*hexdec(substr($hex,0,2)); //Converting to rgb and multiplying luminance
    $g = 0.7152*hexdec(substr($hex,2,2));
    $b = 0.0722*hexdec(substr($hex,4,2));

    return $r + $g + $b;
}

Demo 2

Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278