Here is a quick utility function for PHP to calculating the RGB hex for a given HSB color vector. It is based, in part, this article by Chris Jackson:
function hsbColor($h, $s, $b)
{
if(0 > $h || 360 < $h) { throw new Exception('Out of Range');}
if(0 > $s || 1 < $s) { throw new Exception('Out of Range');}
if(0 > $b || 1 < $b) { throw new Exception('Out of Range');}
if($s == 0) {
return sprintf("#%'02x%'02x%'02x", $b*255, $b*255, $b*255);
}
$hi = floor($h/60) % 6;
$f = ($h/60) - floor($h/60);
$p = 255 * $b * (1-$s);
$q = 255 * $b * (1 - $f * $s);
$t = 255 * $b * (1 - (1 - $f) * $s);
$b = (int)($b * 255);
switch($hi) {
case 0:
return sprintf("#%'02x%'02x%'02x", $b, $t, $p);
break;
case 1:
return sprintf("#%'02x%'02x%'02x", $q, $b, $p);
break;
case 2:
return sprintf("#%'02x%'02x%'02x", $p, $b, $t);
break;
case 3:
return sprintf("#%'02x%'02x%'02x", $p, $q, $b);
break;
case 4:
return sprintf("#%'02x%'02x%'02x", $t, $p, $b);
break;
case 5:
return sprintf("#%'02x%'02x%'02x", $b, $p, $q);
break;
}
}
Like
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.