change font color if background is dark or light in php

Solutions on MaxInterview for change font color if background is dark or light in php by the best coders in the world

showing results for - "change font color if background is dark or light in php"
Lottie
02 Nov 2018
1get_brightness($hex) { 
2  // returns brightness value from 0 to 255 
3  // strip off any leading # 
4  $hex = str_replace('#', '', $hex); 
5  $c_r = hexdec(substr($hex, 0, 2)); 
6  $c_g = hexdec(substr($hex, 2, 2)); 
7  $c_b = hexdec(substr($hex, 4, 2)); 
8  
9  return (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000;
10}
11
12$color = "#******"; 
13if ($get_brightness($color) > 130) { // will have to experiment with this number 
14  echo '<font style="color:black;">Black</font>'; 
15} else {  
16  echo '<font style="color:white;">White</font>'; 
17}