ConFoo Montreal 2026: Call for Papers

Voting

: three plus three?
(Example: nine)

The Note You're Voting On

tyberis
20 years ago
2 functions to convert from HSV colorspace (hue/saturation/brightness) to RGB (red/green/blue) colorspace and back.
<?php
// $c = array($red, $green, $blue)
// $red=[0..1], $green=[0..1], $blue=[0..1];
function rgb2hsv($c) {
list(
$r,$g,$b)=$c;
$v=max($r,$g,$b);
$t=min($r,$g,$b);
$s=($v==0)?0:($v-$t)/$v;
if (
$s==0)
$h=-1;
else {
$a=$v-$t;
$cr=($v-$r)/$a;
$cg=($v-$g)/$a;
$cb=($v-$b)/$a;
$h=($r==$v)?$cb-$cg:(($g==$v)?2+$cr-$cb:(($b==$v)?$h=4+$cg-$cr:0));
$h=60*$h;
$h=($h<0)?$h+360:$h;
}
return array(
$h,$s,$v);
}

// $c = array($hue, $saturation, $brightness)
// $hue=[0..360], $saturation=[0..1], $brightness=[0..1]
function hsv2rgb($c) {
list(
$h,$s,$v)=$c;
if (
$s==0)
return array(
$v,$v,$v);
else {
$h=($h%=360)/60;
$i=floor($h);
$f=$h-$i;
$q[0]=$q[1]=$v*(1-$s);
$q[2]=$v*(1-$s*(1-$f));
$q[3]=$q[4]=$v;
$q[5]=$v*(1-$s*$f);
//return(array($q[($i+4)%5],$q[($i+2)%5],$q[$i%5]));
return(array($q[($i+4)%6],$q[($i+2)%6],$q[$i%6])); //[1]
}
}
?>

[1] - EDITOR NOTE: THIS IS A FIX FROM "hc at hob(removethis)soft dot net".

<< Back to user notes page

To Top