javascript - Is this possible to detect a colour is a light or dark colour? -
consider these 2 pink square:
and this:
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
$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; }
Comments
Post a Comment