Keys →
Functions ↓
function colorContrastBW ( $col, $mode=3 ) {#-k color#- given a hex background-color $col, what is best fg color to use - white or black?#-p $col - req - Hex RGB color (Must be in the form #hhhhhh)#-p $mode - opt - 0..3 algorithm chosen for computing the color. Default 3.#-p - ret - B&W contrast color, format #hhhhhh#-c credit: http://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color#-c credit#3: http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color#-d 2/8/21 Fixed#-d 2/16/21 Updated $mode as optional algorithm. Was using 1, but 3 seems better. $r = 255 - hexdec ( substr( $col,1,2) ); $g = 255 - hexdec ( substr( $col,3,2) ); $b = 255 - hexdec ( substr( $col,5,2) );# Counting the perceptive luminance - human eye favors green color... switch ($mode) {# Luminance (standard, objective)case 0: $a = 1 - (0.2126*$r + 0.7152*$g + 0.0722*$b)/255; break;# Luminance (perceived approach 1)case 1: $a = 1 - ( 0.299*$r + 0.587*$g + 0.114*$b)/255; break;# Luminance (perceived approach 3)case 2: $a = 1 - sqrt( 0.241*$r*$r + 0.691*$g*$g + 0.068*$b*$b )/255; break;# Luminance (perceived approach 4)default:case 3: $a = 1 - sqrt( 0.299*$r*$r + 0.587*$g*$g + 0.114*$b*$b )/255; break; } if ($a < 0.5) $d = 'FF'; // dark colors - white font else $d = '00'; // bright colors - dark font return shift('#'.right('0'.dechex($d),2).right('0'.dechex($d),2).right('0'.dechex($d),2)); }?>