fix missing bold for your account item
[brisk.git] / bin / rgb_hsv.php
1 #!/usr/bin/php
2 <?php
3 function RGB_to_HSV ($norm, $R, $G, $B)  // RGB Values:Number 0-255
4 {                                 // HSV Results:Number 0-1
5    $HSL = array();
6
7    $var_R = ($R / $norm);
8    $var_G = ($G / $norm);
9    $var_B = ($B / $norm);
10
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;
14
15    $V = $var_Max;
16
17    if ($del_Max == 0)
18    {
19       $H = 0;
20       $S = 0;
21    }
22    else
23    {
24       $S = $del_Max / $var_Max;
25
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;
29
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;
33
34       if ($H<0) $H++;
35       if ($H>1) $H--;
36    }
37
38    $HSL = array($H * $norm, $S * $norm, $V * $norm);
39
40    return $HSL;
41 }
42
43 function HSV_to_RGB($norm, $H, $S, $V) {
44     $H /= $norm;
45     $S /= $norm;
46     $V /= $norm;
47     //1
48     $H *= 6;
49     //2
50     $I = floor($H);
51     $F = $H - $I;
52     //3
53     $M = $V * (1 - $S);
54     $N = $V * (1 - $S * $F);
55     $K = $V * (1 - $S * (1 - $F));
56     //4
57     switch ($I) {
58         case 0:
59             list($R,$G,$B) = array($V,$K,$M);
60             break;
61         case 1:
62             list($R,$G,$B) = array($N,$V,$M);
63             break;
64         case 2:
65             list($R,$G,$B) = array($M,$V,$K);
66             break;
67         case 3:
68             list($R,$G,$B) = array($M,$N,$V);
69             break;
70         case 4:
71             list($R,$G,$B) = array($K,$M,$V);
72             break;
73         case 5:
74         case 6: //for when $H=1 is given
75             list($R,$G,$B) = array($V,$M,$N);
76             break;
77     }
78     return array($R * $norm, $G * $norm, $B * $norm);
79 }
80
81 function main()
82 {
83     GLOBAL $argv;
84     
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]);
89         }
90         else {
91             printf("%02x%02x%02x\n", (int)$a[0], (int)$a[1], (int)$a[2]);
92         }
93     }
94     
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]);
99         }
100         else {
101             printf("%02x%02x%02x\n", (int)$a[0], (int)$a[1], (int)$a[2]);
102         }
103     }
104 }
105
106 main();
107
108 ?>