PHP HSV转RGB公式理解
我可以使用以下代码将 RGB 值转换为 HSV...
I can convert RGB values to HSV with the following code...
$r = $r/255;
$g = $g/255;
$b = $b/255;
$h = 0;
$s = 0;
$v = 0;
$min = min(min($r, $g),$b);
$max = max(max($r, $g),$b);
$r = $max-$min;
$v = $max;
if($r == 0){
$h = 0;
$s = 0;
}
else {
$s = $r / $max;
$hr = ((($max - $r) / 6) + ($r / 2)) / $r;
$hg = ((($max - $g) / 6) + ($r / 2)) / $r;
$hb = ((($max - $b) / 6) + ($r / 2)) / $r;
if ($r == $max) $h = $hb - $hg;
else if($g == $max) $h = (1/3) + $hr - $hb;
else if ($b == $max) $h = (2/3) + $hg - $hr;
if ($h < 0)$h += 1;
if ($h > 1)$h -= 1;
}
但是你如何在 PHP???
以下是维基百科,但我不明白,
The following is on wikipedia but I don't understand it,
我猜这很明显
推荐答案
这是针对 [0,1]
范围内的 HSV 值(并给出 范围内的 RGB 值[0,1]
,而不是 {0, 1, ..., 255}
:
This is for the the HSV values in the range [0,1]
(and giving RGB values in the range [0,1]
, instead of {0, 1, ..., 255}
:
function HSVtoRGB(array $hsv) {
list($H,$S,$V) = $hsv;
//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;
}
return array($R, $G, $B);
}
相关文章