23#include "kis_color_conversions.h"
29void rgb_to_hsv( int R, int G, int B, int *H, int *S, int *V)
33 unsigned char maxValue = 0;
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;
56 if (delta == 0) delta = 1;
59 *S = max ? (510 * delta + max) / ( 2 * max) : 0;
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);
90void hsv_to_rgb( int H, int S, int V, int *R, int *G, int *B)
94 if (S != 0 && H != -1) {
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
151void RGBToHSV( float r, float g, float b, float *h, float *s, float *v)
153 float max = TQMAX(r, TQMAX(g, b));
154 float min = TQMIN(r, TQMIN(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;
184void 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)));
242void rgb_to_hls(TQ_UINT8 red, TQ_UINT8 green, TQ_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;
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;
297float hue_value( float n1, float n2, float hue)
304 return n1 + (((n2 - n1) * hue) / 60);
308 return n1 + (((n2 - n1) * (240 - hue)) / 60);
313void hls_to_rgb( float h, float l, float s, TQ_UINT8 * r, TQ_UINT8 * g, TQ_UINT8 * b)
324 *r = (TQ_UINT8)(hue_value(m1, m2, h + 120) * 255 + 0.5);
325 *g = (TQ_UINT8)(hue_value(m1, m2, h) * 255 + 0.5);
326 *b = (TQ_UINT8)(hue_value(m1, m2, h - 120) * 255 + 0.5);
330void rgb_to_hls(TQ_UINT8 r, TQ_UINT8 g, TQ_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);
340void hls_to_rgb( int h, int l, int s, TQ_UINT8 * r, TQ_UINT8 * g, TQ_UINT8 * b)
343 float lightness = l / 255.0;
344 float saturation = s / 255.0;
346 hls_to_rgb(hue, lightness, saturation, r, g, b);
355void 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);
395void 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;
|