wmaker: Added 'const' attribute to local function 'drawMultiLineString'
[wmaker-crm.git] / wrlib / color.c
blob085c461d15668c77fcb3cfe7dfc72c26ff4f3823
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,
20 * MA 02110-1301, USA.
23 #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"
34 #define MIN(a,b) ((a)<(b) ? (a) : (b))
35 #define MAX(a,b) ((a)>(b) ? (a) : (b))
37 #define MIN3(a,b,c) MIN(MIN(a,b), c)
38 #define MAX3(a,b,c) MAX(MAX(a,b), c)
40 void RHSVtoRGB(const RHSVColor * hsv, RColor * rgb)
42 int h = hsv->hue % 360;
43 int s = hsv->saturation;
44 int v = hsv->value;
45 int i, f;
46 int p, q, t;
48 if (s == 0) {
49 rgb->red = rgb->green = rgb->blue = v;
50 return;
52 i = h / 60;
53 f = h % 60;
54 p = v * (255 - s) / 255;
55 q = v * (255 - s * f / 60) / 255;
56 t = v * (255 - s * (60 - f) / 60) / 255;
58 switch (i) {
59 case 0:
60 rgb->red = v;
61 rgb->green = t;
62 rgb->blue = p;
63 break;
64 case 1:
65 rgb->red = q;
66 rgb->green = v;
67 rgb->blue = p;
68 break;
69 case 2:
70 rgb->red = p;
71 rgb->green = v;
72 rgb->blue = t;
73 break;
74 case 3:
75 rgb->red = p;
76 rgb->green = q;
77 rgb->blue = v;
78 break;
79 case 4:
80 rgb->red = t;
81 rgb->green = p;
82 rgb->blue = v;
83 break;
84 case 5:
85 rgb->red = v;
86 rgb->green = p;
87 rgb->blue = q;
88 break;
92 void RRGBtoHSV(const RColor * rgb, RHSVColor * hsv)
94 int h, s, v;
95 int max = MAX3(rgb->red, rgb->green, rgb->blue);
96 int min = MIN3(rgb->red, rgb->green, rgb->blue);
98 v = max;
100 if (max == 0)
101 s = 0;
102 else
103 s = (max - min) * 255 / max;
105 if (s == 0)
106 h = 0;
107 else {
108 int rc, gc, bc;
110 rc = (max - rgb->red) * 255 / (max - min);
111 gc = (max - rgb->green) * 255 / (max - min);
112 bc = (max - rgb->blue) * 255 / (max - min);
114 if (rgb->red == max) {
115 h = ((bc - gc) * 60 / 255);
116 } else if (rgb->green == max) {
117 h = 2 * 60 + ((rc - bc) * 60 / 255);
118 } else {
119 h = 4 * 60 + ((gc - rc) * 60 / 255);
121 if (h < 0)
122 h += 360;
125 hsv->hue = h;
126 hsv->saturation = s;
127 hsv->value = v;