Warning: Invalid argument supplied for foreach() in /home/jihswbiz/public_html/t6.gwilt.org/refDox.php on line 212
Docs-colorContrastBW

 

ReLoad

Keys


Functions

arraybrowsercolorcookiedatetimeDBdebugdeviceDSTequiverrorflagsfontformatgeo
holidayhtmlisjsonmathparsePHPprintsearchsortstringtabletexttracetype
VOTDweatherxml

Function  colorContrastBW   Lines 432-470 (38 lines)Updated: 2021-02-16 file  utils.php   Last mod: Fri 2021-05-14 21:45:07

function colorContrastBW $col$mode=)
        {
#-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 - (0.2126*$r 0.7152*$g 0.0722*$b)/255;
                break;
#       Luminance (perceived approach 1)
case 1:         $a - ( 0.299*$r +  0.587*$g +  0.114*$b)/255;
                break;
#       Luminance (perceived approach 3)
case 2:         $a sqrt0.241*$r*$r 0.691*$g*$g 0.068*$b*$b )/255;
                break;
#       Luminance (perceived approach 4)
default:
case 
3:         $a sqrt0.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));
        }

?>