*** empty log message ***
[wmaker-crm.git] / wrlib / color.c
blob90963806681628fa951b4ff2e6464a3e869043f9
1 /* color.c - color stuff (rgb -> hsv convertion etc.)
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1998 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.
22 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
30 #include <assert.h>
32 #include "wraster.h"
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)
43 void
44 RHSVtoRGB(RHSVColor *hsv, RColor *rgb)
46 int h = hsv->hue % 360;
47 int s = hsv->saturation;
48 int v = hsv->value;
49 int i, f;
50 int p, q, t;
52 if (s == 0) {
53 rgb->red = rgb->green = rgb->blue = v;
54 return;
56 i = h / 60;
57 f = h % 60;
58 p = v * (255 - s) / 255;
59 q = v * (255 - s * f / 60) / 255;
60 t = v * (255 - s * (60 - f) / 60) / 255;
62 switch (i) {
63 case 0:
64 rgb->red = v;
65 rgb->green = t;
66 rgb->blue = p;
67 break;
68 case 1:
69 rgb->red = q;
70 rgb->green = v;
71 rgb->blue = p;
72 break;
73 case 2:
74 rgb->red = p;
75 rgb->green = v;
76 rgb->blue = t;
77 break;
78 case 3:
79 rgb->red = p;
80 rgb->green = q;
81 rgb->blue = v;
82 break;
83 case 4:
84 rgb->red = t;
85 rgb->green = p;
86 rgb->blue = v;
87 break;
88 case 5:
89 rgb->red = v;
90 rgb->green = p;
91 rgb->blue = q;
92 break;
97 void
98 RRGBtoHSV(RColor *rgb, RHSVColor *hsv)
100 int h, s, v;
101 int max = MAX3(rgb->red, rgb->green, rgb->blue);
102 int min = MIN3(rgb->red, rgb->green, rgb->blue);
104 v = max;
106 if (max == 0)
107 s = 0;
108 else
109 s = (max - min) * 255 / max;
111 if (s == 0)
112 h = 0;
113 else {
114 int rc, gc, bc;
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);
124 } else {
125 h = 4*60 + ((gc - rc) * 60 / 255);
127 if (h < 0)
128 h += 360;
131 hsv->hue = h;
132 hsv->saturation = s;
133 hsv->value = v;