My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 117 attachment: text color function - php.txt (1.8 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* colorTone determines if it is dark or light
*
* @param color is a string of a hex color
* @return #FFFFFF if color is a dark color. #000000 if the color is a light color.
*/
function colorTone($color){
$rgbColors = rgb2hex2rgb($color);
$c = array( 255 - $rgbColors['red'], 255 - $rgbColors['green'], 255 - $rgbColors['blue'] );
$d = 0;

// Counting the perceptive luminance - human eye favors green color...
(float)$a = 1 - ( 0.299 * $rgbColors['red'] + 0.587 * $rgbColors['green'] + 0.114 * $rgbColors['blue'])/255;

if ($a < 0.5){
$d = 0; // bright colors - dark font
}else{
$d = 255; // dark colors - white font
}

return rgb2hex2rgb("$d,$d,$d");
}
function rgb2hex2rgb($c){
if(!$c) return false;
$c = trim($c);
$out = false;
if(eregi("^[0-9ABCDEFabcdef\#]+$", $c)){
$c = str_replace('#','', $c);
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);

if($l){
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1*$l));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1*$l,1*$l));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2*$l,1*$l));
}else $out = false;

}elseif (eregi("^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$", $c)){
$spr = str_replace(array(',',' ','.'), ':', $c);
$e = split(":",$spr);
if(count($e) != 3) return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));

for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];

$out = strtoupper($out);
}else $out = false;

return $out;
}
Powered by Google Project Hosting