Created
May 15, 2014 11:38
-
-
Save SimonJThompson/a7d9f0092516480c4642 to your computer and use it in GitHub Desktop.
PHP function, returns either black or white hex value to contrast with a given hex colour.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Original code from http://www.webmasterworld.com/forum88/9769.htm, tweaked to accomodate for shorthand hex (#fff) | |
*/ | |
function get_contrasting_colour($hex) { | |
$hex = str_replace('#', '', $hex); | |
$l=2; | |
if(strlen($hex==3)){$l=1;} | |
$c_r = hexdec(substr($hex, 0, $l)); | |
$c_g = hexdec(substr($hex, 2, $l)); | |
$c_b = hexdec(substr($hex, 4, $l)); | |
$b = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; | |
if($b>130){ | |
return '#000'; | |
}else{ | |
return '#fff'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment