javascript - Is this possible to detect a colour is a light or dark colour? -


consider these 2 pink square:

enter image description here

and this:

enter image description here

as may know, 1 lighter , 1 darker or more sharp. problem is, can tell human eyes, possible use system way or programme way detect information? @ least, possible have value tell me colour more white or colour less white? (assume got rgb code of colour.) thanks.

since haven't specified particular language/script detect darker/lighter hex, contribute php solution this

demo

$color_one = "fae7e6"; //state hex without # $color_two = "ee7ab7";  function conversion($hex) {     $r = hexdec(substr($hex,0,2)); //converting rgb     $g = hexdec(substr($hex,2,2));     $b = hexdec(substr($hex,4,2));      return $r + $g + $b; //adding rgb values }  echo (conversion($color_one) > conversion($color_two)) ? 'color 1 lighter' : 'color 1 darker'; //comparing 2 converted rgb, greater 1 darker 

as pointed out @some guy, have modified function yielding better/accurate result... (added luminance)

function conversion($hex) {     $r = 0.2126*hexdec(substr($hex,0,2)); //converting rgb , multiplying luminance     $g = 0.7152*hexdec(substr($hex,2,2));     $b = 0.0722*hexdec(substr($hex,4,2));      return $r + $g + $b; } 

demo 2


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -