Cette fonction retourne un tableau contenant $stepNumber couleurs nécessaires au dégradé de $startColor vers $endColor.
function gradient($startColor,$endColor, $stepNumber)
{
$colors = array($startColor);
$sColor = str_split($startColor,2);
$eColor = str_split($endColor,2);
for($i = 0 ;$i< 3 ;$i++)
{
$diff [$i] = (hexdec($sColor[$i])-hexdec($eColor[$i]))/($stepNumber-2);
}
for ($i = 1;$i<$stepNumber-1;$i++)
{
$c = str_split($colors[$i-1],2);
$colors[$i] = sprintf('%02X',max(0,min(255,(hexdec($c[0])-$diff[0])))).
sprintf('%02X',max(0,min(255,(hexdec($c[1])-$diff[1])))).
sprintf('%02X',max(0,min(255,(hexdec($c[2])-$diff[2]))));
}
$colors[$i] = $endColor;
return $colors;
}
Exemple :
$colors = gradient('FF0000','00EE00',7);
Affiche :
0 => string 'FF0000'
1 => string 'CC2F00'
2 => string '995E00'
3 => string '668D00'
4 => string '33BC00'
5 => string '00EB00'
6 => string '00EE00'
Image : Alcino