Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / color.c
blob3403e898e265ee97c51b58a01e16aa79a8595c69
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"
33 #include "wr_i18n.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)
42 void RHSVtoRGB(const RHSVColor * hsv, RColor * rgb)
44 int h = hsv->hue % 360;
45 int s = hsv->saturation;
46 int v = hsv->value;
47 int i, f;
48 int p, q, t;
50 if (s == 0) {
51 rgb->red = rgb->green = rgb->blue = v;
52 return;
54 i = h / 60;
55 f = h % 60;
56 p = v * (255 - s) / 255;
57 q = v * (255 - s * f / 60) / 255;
58 t = v * (255 - s * (60 - f) / 60) / 255;
60 switch (i) {
61 case 0:
62 rgb->red = v;
63 rgb->green = t;
64 rgb->blue = p;
65 break;
66 case 1:
67 rgb->red = q;
68 rgb->green = v;
69 rgb->blue = p;
70 break;
71 case 2:
72 rgb->red = p;
73 rgb->green = v;
74 rgb->blue = t;
75 break;
76 case 3:
77 rgb->red = p;
78 rgb->green = q;
79 rgb->blue = v;
80 break;
81 case 4:
82 rgb->red = t;
83 rgb->green = p;
84 rgb->blue = v;
85 break;
86 case 5:
87 rgb->red = v;
88 rgb->green = p;
89 rgb->blue = q;
90 break;
94 void RRGBtoHSV(const RColor * rgb, RHSVColor * hsv)
96 int h, s, v;
97 int max = MAX3(rgb->red, rgb->green, rgb->blue);
98 int min = MIN3(rgb->red, rgb->green, rgb->blue);
100 v = max;
102 if (max == 0)
103 s = 0;
104 else
105 s = (max - min) * 255 / max;
107 if (s == 0)
108 h = 0;
109 else {
110 int rc, gc, bc;
112 rc = (max - rgb->red) * 255 / (max - min);
113 gc = (max - rgb->green) * 255 / (max - min);
114 bc = (max - rgb->blue) * 255 / (max - min);
116 if (rgb->red == max) {
117 h = ((bc - gc) * 60 / 255);
118 } else if (rgb->green == max) {
119 h = 2 * 60 + ((rc - bc) * 60 / 255);
120 } else {
121 h = 4 * 60 + ((gc - rc) * 60 / 255);
123 if (h < 0)
124 h += 360;
127 hsv->hue = h;
128 hsv->saturation = s;
129 hsv->value = v;