1 /* color.c - color stuff (rgb -> hsv convertion etc.)
3 * Raster graphics library
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
36 #define MIN(a,b) ((a)<(b) ? (a) : (b))
37 #define MAX(a,b) ((a)>(b) ? (a) : (b))
39 #define MIN3(a,b,c) MIN(MIN(a,b), c)
40 #define MAX3(a,b,c) MAX(MAX(a,b), c)
42 void RHSVtoRGB(const RHSVColor
* hsv
, RColor
* rgb
)
44 int h
= hsv
->hue
% 360;
45 int s
= hsv
->saturation
;
51 rgb
->red
= rgb
->green
= rgb
->blue
= v
;
56 p
= v
* (255 - s
) / 255;
57 q
= v
* (255 - s
* f
/ 60) / 255;
58 t
= v
* (255 - s
* (60 - f
) / 60) / 255;
94 void RRGBtoHSV(const RColor
* rgb
, RHSVColor
* hsv
)
97 int max
= MAX3(rgb
->red
, rgb
->green
, rgb
->blue
);
98 int min
= MIN3(rgb
->red
, rgb
->green
, rgb
->blue
);
105 s
= (max
- min
) * 255 / max
;
112 rc
= (max
- rgb
->red
) * 255 / (max
- min
);
113 gc
= (max
- rgb
->green
) * 255 / (max
- min
);
114 bc
= (max
- rgb
->blue
) * 255 / (max
- min
);
116 if (rgb
->red
== max
) {
117 h
= ((bc
- gc
) * 60 / 255);
118 } else if (rgb
->green
== max
) {
119 h
= 2 * 60 + ((rc
- bc
) * 60 / 255);
121 h
= 4 * 60 + ((gc
- rc
) * 60 / 255);