Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / color.c
1 /* color.c - color stuff (rgb -> hsv convertion etc.)
2  *
3  * Raster graphics library
4  *
5  * Copyright (c) 1998-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28
29 #include <assert.h>
30
31 #include "wraster.h"
32
33 #define MIN(a,b)        ((a)<(b) ? (a) : (b))
34 #define MAX(a,b)        ((a)>(b) ? (a) : (b))
35
36 #define MIN3(a,b,c)     MIN(MIN(a,b), c)
37 #define MAX3(a,b,c)     MAX(MAX(a,b), c)
38
39 void RHSVtoRGB(RHSVColor * hsv, RColor * rgb)
40 {
41         int h = hsv->hue % 360;
42         int s = hsv->saturation;
43         int v = hsv->value;
44         int i, f;
45         int p, q, t;
46
47         if (s == 0) {
48                 rgb->red = rgb->green = rgb->blue = v;
49                 return;
50         }
51         i = h / 60;
52         f = h % 60;
53         p = v * (255 - s) / 255;
54         q = v * (255 - s * f / 60) / 255;
55         t = v * (255 - s * (60 - f) / 60) / 255;
56
57         switch (i) {
58         case 0:
59                 rgb->red = v;
60                 rgb->green = t;
61                 rgb->blue = p;
62                 break;
63         case 1:
64                 rgb->red = q;
65                 rgb->green = v;
66                 rgb->blue = p;
67                 break;
68         case 2:
69                 rgb->red = p;
70                 rgb->green = v;
71                 rgb->blue = t;
72                 break;
73         case 3:
74                 rgb->red = p;
75                 rgb->green = q;
76                 rgb->blue = v;
77                 break;
78         case 4:
79                 rgb->red = t;
80                 rgb->green = p;
81                 rgb->blue = v;
82                 break;
83         case 5:
84                 rgb->red = v;
85                 rgb->green = p;
86                 rgb->blue = q;
87                 break;
88         }
89 }
90
91 void RRGBtoHSV(RColor * rgb, RHSVColor * hsv)
92 {
93         int h, s, v;
94         int max = MAX3(rgb->red, rgb->green, rgb->blue);
95         int min = MIN3(rgb->red, rgb->green, rgb->blue);
96
97         v = max;
98
99         if (max == 0)
100                 s = 0;
101         else
102                 s = (max - min) * 255 / max;
103
104         if (s == 0)
105                 h = 0;
106         else {
107                 int rc, gc, bc;
108
109                 rc = (max - rgb->red) * 255 / (max - min);
110                 gc = (max - rgb->green) * 255 / (max - min);
111                 bc = (max - rgb->blue) * 255 / (max - min);
112
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);
117                 } else {
118                         h = 4 * 60 + ((gc - rc) * 60 / 255);
119                 }
120                 if (h < 0)
121                         h += 360;
122         }
123
124         hsv->hue = h;
125         hsv->saturation = s;
126         hsv->value = v;
127 }