PDA

View Full Version : PHP RGB<->HSV functions


petefs
07-12-2004, 06:53 PM
had to write this yesterday, and hell... nobody else has posted here yet : )

dunno if it could be optimized. I wasn't able to turn anything up in a search, so here ya go if anybody wants it:


<?php
function RGB_TO_HSV ($R, $G, $B) // RGB Values:Number 0-255
{ // HSV Results:Number 0-1
$HSL = array();

$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);

$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;

$V = $var_Max;

if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
$S = $del_Max / $var_Max;

$del_R = ( ( ( $max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;

if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;

if (H<0) $H++;
if (H>1) $H--;
}

$HSL['H'] = $H;
$HSL['S'] = $S;
$HSL['V'] = $V;

return $HSL;
}

function HSV_TO_RGB ($H, $S, $V) // HSV Values:Number 0-1
{ // RGB Results:Number 0-255
$RGB = array();

if($S == 0)
{
$R = $G = $B = $V * 255;
}
else
{
$var_H = $H * 6;
$var_i = floor( $var_H );
$var_1 = $V * ( 1 - $S );
$var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
$var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );

if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }

$R = $var_R * 255;
$G = $var_G * 255;
$B = $var_B * 255;
}

$RGB['R'] = $R;
$RGB['G'] = $G;
$RGB['B'] = $B;

return $RGB;
}
?>

senocular
07-12-2004, 07:14 PM
Im pretty sure there are some on proto.layer51

Also, its not uncommon to use readily available JS implementations ;)

But yours looks just as good as any of the others as far as I can tell.

petefs
07-12-2004, 07:28 PM
bah, it figures. I should have looked there first : ) Plain as day.

You'd think for the five thousand times I've read through all the code available there that I would have remembered ^_^

Well, reinventing the wheel is part of learning ; )

Thanks ^_^

buddhaflow
11-19-2010, 04:22 AM
Man, Thank you, thank you, thank you! I was looking all over for this, and didn't want to convert it from BASIC to PHP. THANKS for posting it!

djh101
11-20-2010, 11:55 PM
So simple. My Flash color changer is like over 400 lines of code...