php渐变,在PHP中生成RGB渐变色的算法

在下面的图片你可以看到一段代码的输出我写信给使用RGB和HSV分裂相等大小的步骤两种颜色之间的对比转换:

UluLN.png

我发现使用过渡HSV受色相影响,取决于颜色之间的距离。如果选择具有相同色调的两种颜色,可以发现HSV过渡比RGB更清晰,因为您只是在玩饱和度和数值(黑色),并且没有像RGB那样添加颜色。

// Configuration.

$nc = 6; // Number of colors.

$w = 300; // Width of divs.

$a = 50; // Height of divs.

// Colors

/* In RGB */

$c1 = array(rand(0,255),rand(0,255),rand(0,255));

$c2 = array(rand(0,255),rand(0,255),rand(0,255));

//$c1 = array(128,175,27); // Color 1: Whit these colors is not change.

//$c2 = array(255,255,140); // Color 2: Whit these colors is not change.

// $c1 = array(0,0,0); // Color 1: White.

// $c2 = array(255,255,255); // Color 2: Black.

/* In HSV */

$h3 = array(rand(0,360),rand(0,100),rand(0,100));

$h4 = array(rand(0,360),rand(0,100),rand(0,100));

//$h3 = array(145,50,50); // Color 3: To see the influence of Hue.

//$h4 = array(145,0,100); // Color 4: To see the influence of Hue.

// HTML

$html .= '

';

// RGB to RGB split

$c = graduateRGB($c1,$c2,$nc);

$html .= customHTML($w,$a,$c,'RGB->RGBs');

// RGB to HSV split

$h1 = RGBtoHSV($c1);

$h2 = RGBtoHSV($c2);

$h = graduateHSV($h1,$h2,$nc);

$html .= customHTML($w,$a,$h,'RGB->HSVs');

// HSV to HSV split

$h = graduateHSV($h3,$h4,$nc);

$html .= customHTML($w,$a,$h,'HSV->HSVs');

// HSV to RGB split

$c3 = HSVtoRGB($h3);

$c4 = HSVtoRGB($h4);

$c = graduateRGB($c3,$c4,$nc);

$html .= customHTML($w,$a,$c,'HSV->RGBs');

// Output

$html .= '

';

echo $html;

/* FUNCIONES DE GRADUACIÓN */

// Dados dos colores RGB (0-255,0-255,0-255) y un número de colores deseados, regresa un array con todos los colores de la gradación.

function graduateRGB($c1,$c2,$nc){

$c = array();

$dc = array(($c2[0]-$c1[0])/($nc-1),($c2[1]-$c1[1])/($nc-1),($c2[2]-$c1[2])/($nc-1));

for ($i=0;$i

$c[$i][0]= round($c1[0]+$dc[0]*$i);

$c[$i][1]= round($c1[1]+$dc[1]*$i);

$c[$i][2]= round($c1[2]+$dc[2]*$i);

}

return $c;

}

// Dados dos colores HSV (0-360,0-100,0-100) y un número de colores deseados, regresa un array con todos los colores de la gradación en RGB. (Hay un detalle con esta función y es que la transición se podría hacer por el lado contrario del círculo cromático)

function graduateHSV($h1,$h2,$nc){

$h = array();

$dh = array(($h2[0]-$h1[0])/($nc-1),($h2[1]-$h1[1])/($nc-1),($h2[2]-$h1[2])/($nc-1));

for ($i=0;$i

$h[$i][0]= $h1[0]+$dh[0]*$i;

$h[$i][1]= $h1[1]+$dh[1]*$i;

$h[$i][2]= $h1[2]+$dh[2]*$i;

$h[$i] = HSVtoRGB($h[$i]);

}

return $h;

}

/* FUNCIONES DE CONVERSIÓN. */

// Convierte a HSV (0-360,0-100,0-100) colores en RGB (0-255,0-255,0-255).

function RGBtoHSV(array $rgb) {

$f = 0.00000001; // Factor de corrección para evitar la división por cero.

list($R,$G,$B) = $rgb;

$R = $R==0?$f:$R/255;

$G = $G==0?$f:$G/255;

$B = $B==0?$f:$B/255;

$V = max($R,$G,$B);

$X = min($R,$G,$B);

$S = ($V-$X)/$V;

$V_X = $V-$X==0?$f:$V-$X;

$r = ($V-$R)/($V_X);

$g = ($V-$G)/($V_X);

$b = ($V-$B)/($V_X);

if ($R == $V)

$H = $G==$X?(5+$b):(1-$g);

elseif ($G == $V)

$H = $B==$X?(1+$r):(3-$b);

else

$H = $R==$X?(3+$g):(5-$r);

$H /= 6;

$H = round($H*360);

$S = round($S*100);

$V = round($V*100);

return array($H, $S, $V);

}

// Convierte a RGB (0-255,0-255,0-255) colores en HSV (0-360,0-100,0-100).

function HSVtoRGB(array $hsv) {

list($H,$S,$V) = $hsv;

$H = $H/360;

$S = $S/100;

$V = $V/100;

//1

$H *= 6;

//2

$I = floor($H);

$F = $H - $I;

//3

$M = $V * (1 - $S);

$N = $V * (1 - $S * $F);

$K = $V * (1 - $S * (1 - $F));

//4

switch ($I) {

case 0:

list($R,$G,$B) = array($V,$K,$M);

break;

case 1:

list($R,$G,$B) = array($N,$V,$M);

break;

case 2:

list($R,$G,$B) = array($M,$V,$K);

break;

case 3:

list($R,$G,$B) = array($M,$N,$V);

break;

case 4:

list($R,$G,$B) = array($K,$M,$V);

break;

case 5:

case 6: //for when $H=1 is given

list($R,$G,$B) = array($V,$M,$N);

break;

}

$R = round($R*255);

$G = round($G*255);

$B = round($B*255);

return array($R, $G, $B);

}

// Función con un HTML de muestra para la visualización de colores, podría ser cualquier otro.

function customHTML($w,$a,$c,$header){

$html = '

'.$header.'

';

foreach ($c as $color){

$html .= '

RGB '.$color[0].','.$color[1].','.$color[2].'
';

}

$html .= '

';

return $html;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值