gpt4 book ai didi

PHP HSV转RGB公式理解

转载 作者:IT王子 更新时间:2023-10-29 00:17:22 25 4
gpt4 key购买 nike

我可以使用以下代码将 RGB 值转换为 HSV...

 $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 中将 HSV RGB 转换???

下面是维基百科上的,但我看不懂,

我猜这很明显

alt text

最佳答案

这是针对 [0,1] 范围内的 HSV 值(并给出 [0,1] 范围内的 RGB 值,而不是 {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);
}

关于PHP HSV转RGB公式理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3597417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com