Remove dup code from getWindowMakerIconImage()
[wmaker-crm.git] / wrlib / color.c
blob75140f900c4cbbbdc9ef74eb045d0343bd3018c7
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.
22 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
29 #include <assert.h>
31 #include "wraster.h"
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;
43 int v = hsv->value;
44 int i, f;
45 int p, q, t;
47 if (s == 0) {
48 rgb->red = rgb->green = rgb->blue = v;
49 return;
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;
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;
91 void RRGBtoHSV(RColor * rgb, RHSVColor * hsv)
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);
97 v = max;
99 if (max == 0)
100 s = 0;
101 else
102 s = (max - min) * 255 / max;
104 if (s == 0)
105 h = 0;
106 else {
107 int rc, gc, bc;
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);
117 } else {
118 h = 4 * 60 + ((gc - rc) * 60 / 255);
120 if (h < 0)
121 h += 360;
124 hsv->hue = h;
125 hsv->saturation = s;
126 hsv->value = v;