1 /* color.c - color stuff (rgb -> hsv convertion etc.)
3 * Raster graphics library
5 * Copyright (c) 1998-2002 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., 675 Mass Ave, Cambridge, MA 02139, USA.
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)
44 RHSVtoRGB(RHSVColor
*hsv
, RColor
*rgb
)
46 int h
= hsv
->hue
% 360;
47 int s
= hsv
->saturation
;
53 rgb
->red
= rgb
->green
= rgb
->blue
= v
;
58 p
= v
* (255 - s
) / 255;
59 q
= v
* (255 - s
* f
/ 60) / 255;
60 t
= v
* (255 - s
* (60 - f
) / 60) / 255;
98 RRGBtoHSV(RColor
*rgb
, RHSVColor
*hsv
)
101 int max
= MAX3(rgb
->red
, rgb
->green
, rgb
->blue
);
102 int min
= MIN3(rgb
->red
, rgb
->green
, rgb
->blue
);
109 s
= (max
- min
) * 255 / max
;
116 rc
= (max
- rgb
->red
) * 255 / (max
- min
);
117 gc
= (max
- rgb
->green
) * 255 / (max
- min
);
118 bc
= (max
- rgb
->blue
) * 255 / (max
- min
);
120 if (rgb
->red
== max
) {
121 h
= ((bc
- gc
) * 60 / 255);
122 } else if (rgb
->green
== max
) {
123 h
= 2*60 + ((rc
- bc
) * 60 / 255);
125 h
= 4*60 + ((gc
- rc
) * 60 / 255);