3 function RGB_to_HSV ($norm, $R, $G, $B) // RGB Values:Number 0-255
4 { // HSV Results:Number 0-1
11 $var_Min = min($var_R, $var_G, $var_B);
12 $var_Max = max($var_R, $var_G, $var_B);
13 $del_Max = $var_Max - $var_Min;
24 $S = $del_Max / $var_Max;
26 $del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
27 $del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
28 $del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
30 if ($var_R == $var_Max) $H = $del_B - $del_G;
31 else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
32 else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
38 $HSL = array($H * $norm, $S * $norm, $V * $norm);
43 function HSV_to_RGB($norm, $H, $S, $V) {
54 $N = $V * (1 - $S * $F);
55 $K = $V * (1 - $S * (1 - $F));
59 list($R,$G,$B) = array($V,$K,$M);
62 list($R,$G,$B) = array($N,$V,$M);
65 list($R,$G,$B) = array($M,$V,$K);
68 list($R,$G,$B) = array($M,$N,$V);
71 list($R,$G,$B) = array($K,$M,$V);
74 case 6: //for when $H=1 is given
75 list($R,$G,$B) = array($V,$M,$N);
78 return array($R * $norm, $G * $norm, $B * $norm);
85 if ($argv[1] == "-tohsv" || $argv[1] == "-toxhsv") {
86 $a = RGB_to_HSV($argv[2], $argv[3], $argv[4], $argv[5]);
87 if ($argv[1] == "-tohsv") {
88 printf("%f,%f,%f\n", $a[0], $a[1], $a[2]);
91 printf("%02x%02x%02x\n", (int)$a[0], (int)$a[1], (int)$a[2]);
95 if ($argv[1] == "-torgb" || $argv[1] == "-toxrgb") {
96 $a = HSV_to_RGB($argv[2], $argv[3], $argv[4], $argv[5]);
97 if ($argv[1] == "-torgb") {
98 printf("%f,%f,%f\n", $a[0], $a[1], $a[2]);
101 printf("%02x%02x%02x\n", (int)$a[0], (int)$a[1], (int)$a[2]);