Loading ...
[Plus récent] [Plus ancien] [Meilleur rang] [Mauvais rang]

PCR Postit

PHP

::

Génération d'un dégradé

[Trackback]
Date : 2007-10-01@15:54:45
Rang : 0

Cette fonction retourne un tableau contenant $stepNumber couleurs nécessaire 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' (length=6)
1 => string 'CC2F00' (length=6)
2 => string '995E00' (length=6)
3 => string '668D00' (length=6)
4 => string '33BC00' (length=6)
5 => string '00EB00' (length=6)
6 => string '00EE00' (length=6)