Download code

Jump to: navigation, search

Back to RGB_to_HSV_color_space_conversion_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

rgb_to_hsv.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/RGB_to_HSV_color_space_conversion_(C)?oldid=17206
14 */
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 
19 #define MIN3(x,y,z)  ((y) <= (z) ? \
20                          ((x) <= (y) ? (x) : (y)) \
21                      : \
22                          ((x) <= (z) ? (x) : (z)))
23 
24 #define MAX3(x,y,z)  ((y) >= (z) ? \
25                          ((x) >= (y) ? (x) : (y)) \
26                      : \
27                          ((x) >= (z) ? (x) : (z)))
28 
29 struct rgb_color {
30     double r, g, b;    /* Channel intensities between 0.0 and 1.0 */
31 };
32 
33 struct hsv_color {
34     double hue;        /* Hue degree between 0.0 and 360.0 */
35     double sat;        /* Saturation between 0.0 (gray) and 1.0 */
36     double val;        /* Value between 0.0 (black) and 1.0 */
37 };
38 
39 
40 struct hsv_color rgb_to_hsv(struct rgb_color rgb) {
41     struct hsv_color hsv;
42     double rgb_min, rgb_max;
43     rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
44     rgb_max = MAX3(rgb.r, rgb.g, rgb.b);
45     hsv.val = rgb_max;
46     if (hsv.val == 0) {
47         hsv.hue = hsv.sat = 0;
48         return hsv;
49     }
50     /* Normalize value to 1 */
51     rgb.r /= hsv.val;
52     rgb.g /= hsv.val;
53     rgb.b /= hsv.val;
54     rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
55     rgb_max = MAX3(rgb.r, rgb.g, rgb.b);
56     hsv.sat = rgb_max - rgb_min;
57     if (hsv.sat == 0) {
58         hsv.hue = 0;
59         return hsv;
60     }
61     /* Normalize saturation to 1 */
62     rgb.r = (rgb.r - rgb_min)/(rgb_max - rgb_min);
63     rgb.g = (rgb.g - rgb_min)/(rgb_max - rgb_min);
64     rgb.b = (rgb.b - rgb_min)/(rgb_max - rgb_min);
65     rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
66     rgb_max = MAX3(rgb.r, rgb.g, rgb.b);
67     /* Compute hue */
68     if (rgb_max == rgb.r) {
69         hsv.hue = 0.0 + 60.0*(rgb.g - rgb.b);
70         if (hsv.hue < 0.0) {
71             hsv.hue += 360.0;
72         }
73     } else if (rgb_max == rgb.g) {
74         hsv.hue = 120.0 + 60.0*(rgb.b - rgb.r);
75     } else /* rgb_max == rgb.b */ {
76         hsv.hue = 240.0 + 60.0*(rgb.r - rgb.g);
77     }
78     return hsv;
79 }
80 
81 
82 int main(int argc, char* argv[]) {
83     struct rgb_color rgb;
84     struct hsv_color hsv;
85     rgb.r = atof(argv[1]);
86     rgb.g = atof(argv[2]);
87     rgb.b = atof(argv[3]);
88     hsv = rgb_to_hsv(rgb);
89     printf("Hue: %f\nSaturation: %f\nValue: %f\n\n", hsv.hue, hsv.sat, hsv.val);
90     return 0;
91 }


hijacker
hijacker
hijacker
hijacker

rgb_to_hsv_int.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/RGB_to_HSV_color_space_conversion_(C)?oldid=17206
14 */
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 
19 #define MIN3(x,y,z)  ((y) <= (z) ? \
20                          ((x) <= (y) ? (x) : (y)) \
21                      : \
22                          ((x) <= (z) ? (x) : (z)))
23 
24 #define MAX3(x,y,z)  ((y) >= (z) ? \
25                          ((x) >= (y) ? (x) : (y)) \
26                      : \
27                          ((x) >= (z) ? (x) : (z)))
28 
29 struct rgb_color {
30     unsigned char r, g, b;    /* Channel intensities between 0 and 255 */
31 };
32 
33 struct hsv_color {
34     unsigned char hue;        /* Hue degree between 0 and 255 */
35     unsigned char sat;        /* Saturation between 0 (gray) and 255 */
36     unsigned char val;        /* Value between 0 (black) and 255 */
37 };
38 
39 struct hsv_color rgb_to_hsv(struct rgb_color rgb) {
40     struct hsv_color hsv;
41     unsigned char rgb_min, rgb_max;
42     rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
43     rgb_max = MAX3(rgb.r, rgb.g, rgb.b);
44     hsv.val = rgb_max;
45     if (hsv.val == 0) {
46         hsv.hue = hsv.sat = 0;
47         return hsv;
48     }
49     hsv.sat = 255*long(rgb_max - rgb_min)/hsv.val;
50     if (hsv.sat == 0) {
51         hsv.hue = 0;
52         return hsv;
53     }
54     /* Compute hue */
55     if (rgb_max == rgb.r) {
56         hsv.hue = 0 + 43*(rgb.g - rgb.b)/(rgb_max - rgb_min);
57     } else if (rgb_max == rgb.g) {
58         hsv.hue = 85 + 43*(rgb.b - rgb.r)/(rgb_max - rgb_min);
59     } else /* rgb_max == rgb.b */ {
60         hsv.hue = 171 + 43*(rgb.r - rgb.g)/(rgb_max - rgb_min);
61     }
62     return hsv;
63 }
64 
65 int main(int argc, char* argv[]) {
66     struct rgb_color rgb;
67     struct hsv_color hsv;
68     rgb.r = (unsigned char)atoi(argv[1]);
69     rgb.g = (unsigned char)atoi(argv[2]);
70     rgb.b = (unsigned char)atoi(argv[3]);
71     hsv = rgb_to_hsv(rgb);
72     printf("Hue: %d\nSaturation: %d\nValue: %d\n\n", hsv.hue, hsv.sat, hsv.val);
73     return 0;
74 }


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog3885509/rgb_to_hsv_int.c: In function 'rgb_to_hsv':
2 /tmp/litprog3885509/rgb_to_hsv_int.c:49:19: error: expected expression before 'long'