2
* Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include "kis_color_conversions.h"
26
* A number of often-used conversions between color models
29
void rgb_to_hsv(int R, int G, int B, int *H, int *S, int *V)
33
unsigned char maxValue = 0; // r = 0, g = 1, b = 2
35
// find maximum and minimum RGB values
36
if(static_cast<unsigned int>(G) > max) {
41
if (static_cast<unsigned int>(B) > max)
47
if(static_cast<unsigned int>(G) < min)
50
if(static_cast<unsigned int>(B) < min )
53
int delta = max - min;
55
// To prevent division by zero later on.
56
if (delta == 0) delta = 1;
59
*S = max ? (510 * delta + max) / ( 2 * max) : 0; // saturation
63
*H = -1; // undefined hue
70
*H = (120 * (G - B) + delta) / (2 * delta);
72
*H = (120 * (G - B + delta) + delta) / (2 * delta) + 300;
76
*H = 120 + (120 * (B - R) + delta) / (2 * delta);
78
*H = 60 + (120 * (B - R + delta) + delta) / (2 * delta);
82
*H = 240 + (120 * (R - G) + delta) / (2 * delta);
84
*H = 180 + (120 * (R - G + delta) + delta) / (2 * delta);
90
void hsv_to_rgb(int H, int S, int V, int *R, int *G, int *B)
94
if (S != 0 && H != -1) { // chromatic
101
unsigned int f = H % 60;
103
unsigned int p = static_cast<unsigned int>(2*V*(255-S)+255)/510;
107
q = static_cast<unsigned int>(2 * V * (15300 - S * f) + 15300) / 30600;
110
*R = static_cast<int>(q);
111
*G = static_cast<int>(V);
112
*B = static_cast<int>(p);
115
*R = static_cast<int>(p);
116
*G = static_cast<int>(q);
117
*B = static_cast<int>(V);
120
*R = static_cast<int>(V);
121
*G = static_cast<int>(p);
122
*B = static_cast<int>(q);
126
t = static_cast<unsigned int>(2 * V * (15300 - (S * (60 - f))) + 15300) / 30600;
129
*R = static_cast<int>(V);
130
*G = static_cast<int>(t);
131
*B = static_cast<int>(p);
134
*R = static_cast<int>(p);
135
*G = static_cast<int>(V);
136
*B = static_cast<int>(t);
139
*R = static_cast<int>(t);
140
*G = static_cast<int>(p);
141
*B = static_cast<int>(V);
149
#define UNDEFINED_HUE -1
151
void RGBToHSV(float r, float g, float b, float *h, float *s, float *v)
153
float max = QMAX(r, QMAX(g, b));
154
float min = QMIN(r, QMIN(g, b));
159
*s = (max - min) / max;
167
float delta = max - min;
170
*h = (g - b) / delta;
171
} else if (g == max) {
172
*h = 2 + (b - r) / delta;
174
*h = 4 + (r - g) / delta;
184
void HSVToRGB(float h, float s, float v, float *r, float *g, float *b)
186
if (s < EPSILON || h == UNDEFINED_HUE) {
196
if (h > 360 - EPSILON) {
201
i = static_cast<int>(floor(h));
204
q = v * (1 - (s * f));
205
t = v * (1 - (s * (1 - f)));
242
void rgb_to_hls(Q_UINT8 red, Q_UINT8 green, Q_UINT8 blue, float * hue, float * lightness, float * saturation)
244
float r = red / 255.0;
245
float g = green / 255.0;
246
float b = blue / 255.0;
251
float max, min, delta;
264
// This is a gray, no chroma...
270
s = delta / ( max + min );
272
s = delta / ( 2 - max - min );
274
float delta_r, delta_g, delta_b;
276
delta_r = (( max - r ) / 6 ) / delta;
277
delta_g = (( max - g ) / 6 ) / delta;
278
delta_b = (( max - b ) / 6 ) / delta;
281
h = delta_b - delta_g;
283
h = ( 1.0 / 3 ) + delta_r - delta_b;
285
h = ( 2.0 / 3 ) + delta_g - delta_r;
297
float hue_value(float n1, float n2, float hue)
304
return n1 + (((n2 - n1) * hue) / 60);
308
return n1 + (((n2 - n1) * (240 - hue)) / 60);
313
void hls_to_rgb(float h, float l, float s, Q_UINT8 * r, Q_UINT8 * g, Q_UINT8 * b)
324
*r = (Q_UINT8)(hue_value(m1, m2, h + 120) * 255 + 0.5);
325
*g = (Q_UINT8)(hue_value(m1, m2, h) * 255 + 0.5);
326
*b = (Q_UINT8)(hue_value(m1, m2, h - 120) * 255 + 0.5);
330
void rgb_to_hls(Q_UINT8 r, Q_UINT8 g, Q_UINT8 b, int * h, int * l, int * s)
332
float hue, saturation, lightness;
334
rgb_to_hls(r, g, b, &hue, &lightness, &saturation);
335
*h = (int)(hue + 0.5);
336
*l = (int)(lightness * 255 + 0.5);
337
*s = (int)(saturation * 255 + 0.5);
340
void hls_to_rgb(int h, int l, int s, Q_UINT8 * r, Q_UINT8 * g, Q_UINT8 * b)
343
float lightness = l / 255.0;
344
float saturation = s / 255.0;
346
hls_to_rgb(hue, lightness, saturation, r, g, b);
350
A Fast HSL-to-RGB Transform
352
from "Graphics Gems", Academic Press, 1990
355
void RGBToHSL(float r, float g, float b, float *h, float *s, float *l)
367
if ((*l = (m + v) / 2.0) <= 0.0) {
372
if ((*s = vm = v - m) > 0.0) {
373
*s /= (*l <= 0.5) ? (v + m ) :
386
*h = (g == m ? 5.0 + b2 : 1.0 - g2);
388
*h = (b == m ? 1.0 + r2 : 3.0 - b2);
390
*h = (r == m ? 3.0 + g2 : 5.0 - r2);
395
void HSLToRGB(float h, float sl, float l, float *r, float *g, float *b)
400
v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
407
float fract, vsf, mid1, mid2;
412
sextant = static_cast<int>(h);
414
vsf = v * sv * fract;
418
case 0: *r = v; *g = mid1; *b = m; break;
419
case 1: *r = mid2; *g = v; *b = m; break;
420
case 2: *r = m; *g = v; *b = mid1; break;
421
case 3: *r = m; *g = mid2; *b = v; break;
422
case 4: *r = mid1; *g = m; *b = v; break;
423
case 5: *r = v; *g = m; *b = mid2; break;