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., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #define MIN(a,b) ((a)<(b) ? (a) : (b))
34 #define MAX(a,b) ((a)>(b) ? (a) : (b))
36 #define MIN3(a,b,c) MIN(MIN(a,b), c)
37 #define MAX3(a,b,c) MAX(MAX(a,b), c)
39 void RHSVtoRGB(RHSVColor
* hsv
, RColor
* rgb
)
41 int h
= hsv
->hue
% 360;
42 int s
= hsv
->saturation
;
48 rgb
->red
= rgb
->green
= rgb
->blue
= v
;
53 p
= v
* (255 - s
) / 255;
54 q
= v
* (255 - s
* f
/ 60) / 255;
55 t
= v
* (255 - s
* (60 - f
) / 60) / 255;
91 void RRGBtoHSV(RColor
* rgb
, RHSVColor
* hsv
)
94 int max
= MAX3(rgb
->red
, rgb
->green
, rgb
->blue
);
95 int min
= MIN3(rgb
->red
, rgb
->green
, rgb
->blue
);
102 s
= (max
- min
) * 255 / max
;
109 rc
= (max
- rgb
->red
) * 255 / (max
- min
);
110 gc
= (max
- rgb
->green
) * 255 / (max
- min
);
111 bc
= (max
- rgb
->blue
) * 255 / (max
- min
);
113 if (rgb
->red
== max
) {
114 h
= ((bc
- gc
) * 60 / 255);
115 } else if (rgb
->green
== max
) {
116 h
= 2 * 60 + ((rc
- bc
) * 60 / 255);
118 h
= 4 * 60 + ((gc
- rc
) * 60 / 255);