2
0
forked from Wavyzz/dolibarr

FIX comment color and create colorIsLight function

This commit is contained in:
arnaud
2017-09-13 10:43:44 +02:00
parent adc78296aa
commit 4b6553d360
4 changed files with 50 additions and 27 deletions

View File

@@ -2145,6 +2145,40 @@ function colorStringToArray($stringcolor,$colorifnotfound=array(88,88,88))
return array(hexdec($reg[1]),hexdec($reg[2]),hexdec($reg[3]));
}
/**
* Return true if the color is light
*
* @param string $stringcolor String with hex (FFFFFF) or comma RGB ('255,255,255')
* @return int -1 : Error with argument passed |0 : color is dark | 1 : color is light
*/
function colorIsLight($stringcolor)
{
$res = -1;
if (!empty($stringcolor))
{
$res = 0;
$tmp=explode(',', $stringcolor);
if (count($tmp) > 1) // This is a comma RGB ('255','255','255')
{
$r = $tmp[0];
$g = $tmp[1];
$b = $tmp[2];
}
else
{
$hexr=$stringcolor[0].$stringcolor[1];
$hexg=$stringcolor[2].$stringcolor[3];
$hexb=$stringcolor[4].$stringcolor[5];
$r = hexdec($hexr);
$g = hexdec($hexg);
$b = hexdec($hexb);
}
$bright = (max($r, $g, $b) + min($r, $g, $b)) / 510.0; // HSL algorithm
if ($bright > 0.6) $res = 1;
}
return $res;
}
/**
* Applies the Cartesian product algorithm to an array
* Source: http://stackoverflow.com/a/15973172