Krita Source Code Documentation
Loading...
Searching...
No Matches
KoColorConversions.h File Reference
#include <QtGlobal>
#include "kritapigment_export.h"

Go to the source code of this file.

Functions

KRITAPIGMENT_EXPORT void CMYKToCMY (qreal *c, qreal *m, qreal *y, qreal *k)
 
KRITAPIGMENT_EXPORT void CMYToCMYK (qreal *c, qreal *m, qreal *y, qreal *k)
 
KRITAPIGMENT_EXPORT void HCIToRGB (const qreal h, const qreal s, const qreal i, qreal *red, qreal *green, qreal *blue)
 
KRITAPIGMENT_EXPORT void HCYToRGB (const qreal h, const qreal s, const qreal y, qreal *red, qreal *green, qreal *blue, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 
KRITAPIGMENT_EXPORT void hls_to_rgb (float h, float l, float s, quint8 *r, quint8 *g, quint8 *b)
 
KRITAPIGMENT_EXPORT void hls_to_rgb (int h, int l, int s, quint8 *r, quint8 *g, quint8 *b)
 
KRITAPIGMENT_EXPORT void HSIToRGB (const qreal h, const qreal s, const qreal i, qreal *red, qreal *green, qreal *blue)
 
KRITAPIGMENT_EXPORT void HSLToRGB (float h, float sl, float l, float *r, float *g, float *b)
 
KRITAPIGMENT_EXPORT void hsv_to_rgb (int H, int S, int V, int *R, int *G, int *B)
 
KRITAPIGMENT_EXPORT void HSVToRGB (float h, float s, float v, float *r, float *g, float *b)
 
KRITAPIGMENT_EXPORT void HSYToRGB (const qreal h, const qreal s, const qreal y, qreal *red, qreal *green, qreal *blue, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 
KRITAPIGMENT_EXPORT float hue_value (float n1, float n2, float hue)
 
KRITAPIGMENT_EXPORT void LabToLCH (const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
 
KRITAPIGMENT_EXPORT void LCHToLab (const qreal L, const qreal C, const qreal H, qreal *l, qreal *a, qreal *b)
 
KRITAPIGMENT_EXPORT void rgb_to_hls (quint8 r, quint8 g, quint8 b, float *h, float *l, float *s)
 
KRITAPIGMENT_EXPORT void rgb_to_hls (quint8 r, quint8 g, quint8 b, int *h, int *l, int *s)
 
KRITAPIGMENT_EXPORT void rgb_to_hsv (int R, int G, int B, int *H, int *S, int *V)
 
KRITAPIGMENT_EXPORT void RGBToHCI (const qreal r, const qreal g, const qreal b, qreal *h, qreal *c, qreal *i)
 
KRITAPIGMENT_EXPORT void RGBToHCY (const qreal r, const qreal g, const qreal b, qreal *h, qreal *c, qreal *y, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 
KRITAPIGMENT_EXPORT void RGBToHSI (qreal r, qreal g, qreal b, qreal *h, qreal *s, qreal *i)
 
KRITAPIGMENT_EXPORT void RGBToHSL (float r, float g, float b, float *h, float *s, float *l)
 
KRITAPIGMENT_EXPORT void RGBToHSV (float r, float g, float b, float *h, float *s, float *v)
 
KRITAPIGMENT_EXPORT void RGBToHSY (qreal r, qreal g, qreal b, qreal *h, qreal *s, qreal *y, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 
KRITAPIGMENT_EXPORT void RGBToYUV (qreal r, qreal g, qreal b, qreal *y, qreal *cb, qreal *cr, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 
KRITAPIGMENT_EXPORT void xyYToXYZ (const qreal x, const qreal y, const qreal yY, qreal *X, qreal *Y, qreal *Z)
 
KRITAPIGMENT_EXPORT void XYZToxyY (const qreal X, const qreal Y, const qreal Z, qreal *x, qreal *y, qreal *yY)
 
KRITAPIGMENT_EXPORT void YUVToRGB (const qreal y, const qreal cb, const qreal cr, qreal *r, qreal *g, qreal *b, qreal R=0.299, qreal G=0.587, qreal B=0.114)
 

Function Documentation

◆ CMYKToCMY()

KRITAPIGMENT_EXPORT void CMYKToCMY ( qreal * c,
qreal * m,
qreal * y,
qreal * k )

Definition at line 882 of file KoColorConversions.cpp.

883{
884 qreal key = *k;
885 qreal cyan = *c;
886 qreal magenta = *m;
887 qreal yellow = *y;
888
889 cyan = ( cyan * ( 1.0 - key ) + key );
890 magenta = ( magenta * ( 1.0 - key ) + key );
891 yellow = ( yellow * ( 1.0 - key ) + key );
892
893 *c=qBound(0.0,cyan ,1.0);
894 *m=qBound(0.0,magenta,1.0);
895 *y=qBound(0.0,yellow ,1.0);
896}

◆ CMYToCMYK()

KRITAPIGMENT_EXPORT void CMYToCMYK ( qreal * c,
qreal * m,
qreal * y,
qreal * k )

Definition at line 854 of file KoColorConversions.cpp.

855{
856 qreal cyan, magenta, yellow, key = 1.0;
857 cyan = *c;
858 magenta = *m;
859 yellow = *y;
860 if ( cyan < key ) {key = cyan;}
861 if ( magenta < key ) {key = magenta;}
862 if ( yellow < key ) {key = yellow;}
863
864 if ( key == 1 ) { //Black
865 cyan = 0;
866 magenta = 0;
867 yellow = 0;
868 }
869 else {
870 cyan = ( cyan - key ) / ( 1.0 - key );
871 magenta = ( magenta - key ) / ( 1.0 - key );
872 yellow = ( yellow - key ) / ( 1.0 - key );
873 }
874
875 *c=qBound(0.0,cyan ,1.0);
876 *m=qBound(0.0,magenta,1.0);
877 *y=qBound(0.0,yellow ,1.0);
878 *k=qBound(0.0,key ,1.0);
879}

◆ HCIToRGB()

KRITAPIGMENT_EXPORT void HCIToRGB ( const qreal h,
const qreal s,
const qreal i,
qreal * red,
qreal * green,
qreal * blue )

Definition at line 640 of file KoColorConversions.cpp.

641{
642//This function may not be correct, but it's based on the HCY function on the basis of seeing HCI as similar
643//to the weighted HCY, but assuming that the weights are the same(one-third).
644 qreal hue=0.0;
645 qreal chroma=0.0;
646 qreal intensity=0.0;
647 if(i<0.0){intensity = 0.0;} else{intensity = i;}
648 if (h>1.0 || h<0.0){hue=fmod(h, 1.0);} else {hue=h;}
649 if(c<0.0){chroma = 0.0;} else{chroma = c;}
650 const qreal onethird = 1.0/3.0;
651 qreal r=0.0;
652 qreal g=0.0;
653 qreal b=0.0;
654
655 int fract = static_cast<int>(hue*6.0);
656 qreal x = (1-fabs(fmod(hue*6.0,2)-1) )*chroma;
657 switch (fract) {
658 case 0:r = chroma; g=x; b=0;break;
659 case 1:r = x; g=chroma; b=0;break;
660 case 2:r = 0; g=chroma; b=x;break;
661 case 3:r = 0; g=x; b=chroma;break;
662 case 4:r = x; g=0; b=chroma;break;
663 case 5:r = chroma; g=0; b=x;break;
664 }
665 qreal m = intensity-( onethird*(r+g+b) );
666 r += m; g += m; b += m;
667
668 *red=r;
669 *green=g;
670 *blue=b;
671}

◆ HCYToRGB()

KRITAPIGMENT_EXPORT void HCYToRGB ( const qreal h,
const qreal s,
const qreal y,
qreal * red,
qreal * green,
qreal * blue,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 708 of file KoColorConversions.cpp.

709{
710 qreal hue=0.0;
711 qreal chroma=c;
712 qreal luma=y;
713 if (h>1.0 || h<0.0){hue=(fmod((h*2.0), 2.0))/2.0;} else {hue=h;}
714 //const qreal R=0.299;
715 //const qreal G=0.587;
716 //const qreal B=0.114;
717 qreal r=0.0;
718 qreal g=0.0;
719 qreal b=0.0;
720
721 int fract =static_cast<int>(hue*6.0);
722 qreal x = (1-fabs(fmod(hue*6.0,2)-1) )*chroma;
723 switch (fract) {
724 case 0:r = chroma; g=x; b=0;break;
725 case 1:r = x; g=chroma; b=0;break;
726 case 2:r = 0; g=chroma; b=x;break;
727 case 3:r = 0; g=x; b=chroma;break;
728 case 4:r = x; g=0; b=chroma;break;
729 case 5:r = chroma; g=0; b=x;break;
730 }
731 qreal m = luma-( (R*r)+(B*b)+(G*g) );
732 r += m; g += m; b += m;
733
734 *red=r;
735 *green=g;
736 *blue=b;
737}
Eigen::Matrix< double, 4, 2 > R

References B, G, and R.

◆ hls_to_rgb() [1/2]

KRITAPIGMENT_EXPORT void hls_to_rgb ( float h,
float l,
float s,
quint8 * r,
quint8 * g,
quint8 * b )

Definition at line 293 of file KoColorConversions.cpp.

294{
295 float m1, m2;
296
297 if (l <= 0.5)
298 m2 = l * (1 + s);
299 else
300 m2 = l + s - l * s;
301
302 m1 = 2 * l - m2;
303
304 *r = (quint8)(hue_value(m1, m2, h + 120) * 255 + 0.5);
305 *g = (quint8)(hue_value(m1, m2, h) * 255 + 0.5);
306 *b = (quint8)(hue_value(m1, m2, h - 120) * 255 + 0.5);
307
308}
float hue_value(float n1, float n2, float hue)

References hue_value().

◆ hls_to_rgb() [2/2]

KRITAPIGMENT_EXPORT void hls_to_rgb ( int h,
int l,
int s,
quint8 * r,
quint8 * g,
quint8 * b )

Definition at line 320 of file KoColorConversions.cpp.

321{
322 float hue = h;
323 float lightness = l / 255.0;
324 float saturation = s / 255.0;
325
326 hls_to_rgb(hue, lightness, saturation, r, g, b);
327}
void hls_to_rgb(float h, float l, float s, quint8 *r, quint8 *g, quint8 *b)

References hls_to_rgb().

◆ HSIToRGB()

KRITAPIGMENT_EXPORT void HSIToRGB ( const qreal h,
const qreal s,
const qreal i,
qreal * red,
qreal * green,
qreal * blue )

Definition at line 411 of file KoColorConversions.cpp.

412{//This function takes H, S and I values, which are converted to rgb.
413 qreal onethird = 1.0/3.0;
414 HSYToRGB(h, s, i, red, green, blue, onethird, onethird, onethird);
415}
void HSYToRGB(const qreal h, const qreal s, const qreal y, qreal *red, qreal *green, qreal *blue, qreal R, qreal G, qreal B)

References HSYToRGB().

◆ HSLToRGB()

KRITAPIGMENT_EXPORT void HSLToRGB ( float h,
float sl,
float l,
float * r,
float * g,
float * b )

Definition at line 376 of file KoColorConversions.cpp.

378{
379 float v;
380
381 v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
382 if (v <= 0) {
383 *r = *g = *b = 0.0;
384 } else {
385 float m;
386 float sv;
387 int sextant;
388 float fract, vsf, mid1, mid2;
389
390 m = l + l - v;
391 sv = (v - m) / v;
392 h = fmod(h, 360.0);
393 h /= 60.0;
394 sextant = static_cast<int>(h);
395 fract = h - sextant;
396 vsf = v * sv * fract;
397 mid1 = m + vsf;
398 mid2 = v - vsf;
399 switch (sextant) {
400 case 0: *r = v; *g = mid1; *b = m; break;
401 case 1: *r = mid2; *g = v; *b = m; break;
402 case 2: *r = m; *g = v; *b = mid1; break;
403 case 3: *r = m; *g = mid2; *b = v; break;
404 case 4: *r = mid1; *g = m; *b = v; break;
405 case 5: *r = v; *g = m; *b = mid2; break;
406 }
407 }
408}
qreal v

References v.

◆ hsv_to_rgb()

KRITAPIGMENT_EXPORT void hsv_to_rgb ( int H,
int S,
int V,
int * R,
int * G,
int * B )

Definition at line 72 of file KoColorConversions.cpp.

73{
74 *R = *G = *B = V;
75
76 if (S != 0 && H != -1) { // chromatic
77
78 if (H >= 360) {
79 // angle > 360
80 H %= 360;
81 }
82
83 unsigned int f = H % 60;
84 H /= 60;
85 unsigned int p = static_cast<unsigned int>(2 * V * (255 - S) + 255) / 510;
86
87 if (H & 1) {
88 unsigned int q = static_cast<unsigned int>(2 * V * (15300 - S * f) + 15300) / 30600;
89 switch (H) {
90 case 1:
91 *R = static_cast<int>(q);
92 *G = static_cast<int>(V);
93 *B = static_cast<int>(p);
94 break;
95 case 3:
96 *R = static_cast<int>(p);
97 *G = static_cast<int>(q);
98 *B = static_cast<int>(V);
99 break;
100 case 5:
101 *R = static_cast<int>(V);
102 *G = static_cast<int>(p);
103 *B = static_cast<int>(q);
104 break;
105 }
106 } else {
107 unsigned int t = static_cast<unsigned int>(2 * V * (15300 - (S * (60 - f))) + 15300) / 30600;
108 switch (H) {
109 case 0:
110 *R = static_cast<int>(V);
111 *G = static_cast<int>(t);
112 *B = static_cast<int>(p);
113 break;
114 case 2:
115 *R = static_cast<int>(p);
116 *G = static_cast<int>(V);
117 *B = static_cast<int>(t);
118 break;
119 case 4:
120 *R = static_cast<int>(t);
121 *G = static_cast<int>(p);
122 *B = static_cast<int>(V);
123 break;
124 }
125 }
126 }
127}
Eigen::Matrix< double, 4, 2 > S
const Params2D p

References B, G, p, R, and S.

◆ HSVToRGB()

KRITAPIGMENT_EXPORT void HSVToRGB ( float h,
float s,
float v,
float * r,
float * g,
float * b )

Definition at line 165 of file KoColorConversions.cpp.

166{
167 if (s < EPSILON || h == UNDEFINED_HUE) {
168 // Achromatic case
169
170 *r = v;
171 *g = v;
172 *b = v;
173 } else {
174 float f, p, q, t;
175 int i;
176
177 if (h > 360 - EPSILON) {
178 h -= 360;
179 }
180
181 h /= 60;
182 i = static_cast<int>(floor(h));
183 f = h - i;
184 p = v * (1 - s);
185 q = v * (1 - (s * f));
186 t = v * (1 - (s * (1 - f)));
187
188 switch (i) {
189 case 0:
190 *r = v;
191 *g = t;
192 *b = p;
193 break;
194 case 1:
195 *r = q;
196 *g = v;
197 *b = p;
198 break;
199 case 2:
200 *r = p;
201 *g = v;
202 *b = t;
203 break;
204 case 3:
205 *r = p;
206 *g = q;
207 *b = v;
208 break;
209 case 4:
210 *r = t;
211 *g = p;
212 *b = v;
213 break;
214 case 5:
215 *r = v;
216 *g = p;
217 *b = q;
218 break;
219 }
220 }
221}
#define EPSILON
#define UNDEFINED_HUE

References EPSILON, p, UNDEFINED_HUE, and v.

◆ HSYToRGB()

KRITAPIGMENT_EXPORT void HSYToRGB ( const qreal h,
const qreal s,
const qreal y,
qreal * red,
qreal * green,
qreal * blue,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 423 of file KoColorConversions.cpp.

424{//This function takes H, S and Y values, which are converted to rgb.
425//Those are then used to create a qcolor.
426 qreal hue = 0.0;
427 qreal sat = 0.0;
428 qreal luma = 0.0;
429 if (h>1.0 || h<0.0){hue=fmod(h, 1.0);} else {hue=h;}
430 if (s<0.0){sat=0.0;} else {sat=s;}
431 //if (y>1.0){luma=1.0;}
432 if (y<0.0){luma=0.0;}
433 else {luma=y;}
434
435 qreal segment = 0.166667;//1/6;
436 qreal r=0.0;
437 qreal g=0.0;
438 qreal b=0.0;
439//weights for rgb to Y'(Luma), these are the same weights used in color space maths and the desaturate.
440//This is not luminance or luminosity, it just quacks like it.
441 //qreal R=0.299;
442 //qreal G=0.587;
443 //qreal B=0.114;
444//The intermediary variables for the weighted HSL formula, based on the HSL in KoColorConversions.
445 qreal max_sat, m, fract, luma_a, chroma, x;
446 if (hue >= 0.0 && hue < (segment) ) {
447 //need to treat this as a weighted hsl thingy.
448 //so first things first, at which luma is the maximum saturation for this hue?
449 //between R and G+R (yellow)
450 max_sat = R + ( G*(hue*6) );
451 if (luma<=max_sat){luma_a = (luma/max_sat)*0.5; chroma=sat*2*luma_a;}
452 else {luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);}
453
454 fract = hue*6.0;
455 x = (1-fabs(fmod(fract,2)-1))*chroma;
456 r = chroma; g=x; b=0;
457 m = luma-( (R*r)+(B*b)+(G*g) );
458 r += m; g += m; b += m;
459 } else if (hue >= (segment) && hue < (2.0*segment) ) {
460 max_sat = (G+R) - (R*(hue-segment)*6);
461
462 if (luma<max_sat) {
463 luma_a = (luma/max_sat)*0.5; chroma=sat*(2*luma_a);
464 } else {
465 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);
466 }
467
468 fract = hue*6.0;
469 x = (1-fabs(fmod(fract,2)-1) )*chroma;
470 r = x; g=chroma; b=0;
471 m = luma-( (R*r)+(B*b)+(G*g) );
472 r += m; g += m; b += m;
473 } else if (hue >= (2.0*segment) && hue < (3.0*segment) ) {
474 max_sat = G + (B*(hue-2.0*segment)*6);
475 if (luma<max_sat) {
476 luma_a = (luma/max_sat)*0.5; chroma=sat*(2*luma_a);
477 } else {
478 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);
479 }
480 fract = hue*6.0;
481 x = (1-fabs(fmod(fract,2)-1) )*chroma;
482 r = 0; g=chroma; b=x;
483 m = luma-( (R*r)+(B*b)+(G*g) );
484 r += m; g += m; b += m;
485 } else if (hue >= (3.0*segment) && hue < (4.0*segment) ) {
486 max_sat = (G+B) - (G*(hue-3.0*segment)*6);
487 if (luma<max_sat){
488 luma_a = (luma/max_sat)*0.5; chroma=sat*(2*luma_a);
489 } else {
490 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);
491 }
492
493 fract = hue*6.0;
494 x = (1-fabs(fmod(fract,2)-1) )*chroma;
495 r = 0; g=x; b=chroma;
496 m = luma-( (R*r)+(B*b)+(G*g) );
497 r += m; g += m; b += m;
498 } else if (hue >= (4.0*segment) && hue < (5*segment) ) {
499 max_sat = B + (R*((hue-4.0*segment)*6));
500 if (luma<max_sat) {
501 luma_a = (luma/max_sat)*0.5; chroma=sat*(2*luma_a);
502 } else {
503 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);
504 }
505 fract = hue*6.0;
506 x = (1-fabs(fmod(fract,2)-1) )*chroma;
507 r = x; g=0; b=chroma;
508 m = luma-( (R*r)+(B*b)+(G*g) );
509 r += m; g += m; b += m;
510 } else if (hue >= (5.0*segment) && hue <= 1.0) {
511 max_sat = (B+R) - (B*(hue-5.0*segment)*6);
512 if (luma<max_sat){
513 luma_a = (luma/max_sat)*0.5; chroma=sat*(2*luma_a);
514 } else {
515 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5; chroma=sat*(2-2*luma_a);
516 }
517 fract = hue*6.0;
518 x = (1-fabs(fmod(fract,2)-1) )*chroma;
519 r = chroma; g=0; b=x;
520 m = luma-( (R*r)+(B*b)+(G*g) );
521 r += m; g += m; b += m;
522 } else {
523 r=0.0;
524 g=0.0;
525 b=0.0;
526 }
527
528 //dbgPigment<<"red: "<<r<<", green: "<<g<<", blue: "<<b;
529 //if (r>1.0){r=1.0;}
530 //if (g>1.0){g=1.0;}
531 //if (b>1.0){b=1.0;}
532 //don't limit upwards due to floating point.
533 if (r<0.0){r=0.0;}
534 if (g<0.0){g=0.0;}
535 if (b<0.0){b=0.0;}
536
537 *red=r;
538 *green=g;
539 *blue=b;
540}

References B, G, and R.

◆ hue_value()

KRITAPIGMENT_EXPORT float hue_value ( float n1,
float n2,
float hue )

Definition at line 277 of file KoColorConversions.cpp.

278{
279 if (hue > 360)
280 hue = hue - 360;
281 else if (hue < 0)
282 hue = hue + 360;
283 if (hue < 60)
284 return n1 + (((n2 - n1) * hue) / 60);
285 else if (hue < 180)
286 return n2;
287 else if (hue < 240)
288 return n1 + (((n2 - n1) *(240 - hue)) / 60);
289 else return n1;
290}

◆ LabToLCH()

KRITAPIGMENT_EXPORT void LabToLCH ( const qreal l,
const qreal a,
const qreal b,
qreal * L,
qreal * C,
qreal * H )

Definition at line 810 of file KoColorConversions.cpp.

811{
812 qreal atemp = (a - 0.5)*10.0;//the multiplication is only so that we get out of floating-point maths
813 qreal btemp = (b - 0.5)*10.0;
814 *L=qBound(0.0,l,1.0);
815 *C=sqrt( pow(atemp,2.0) + pow(btemp,2.0) )*0.1;
816 qreal hue = (atan2(btemp,atemp))* 180.0 / M_PI;
817
818 if (hue<0.0) {
819 hue+=360.0;
820 } else {
821 hue = fmod(hue, 360.0);
822 }
823 *H=hue/360.0;
824}
#define C(i, j)
#define M_PI
Definition kis_global.h:111
KRITAIMAGE_EXPORT qreal atan2(qreal y, qreal x)
atan2 replacement

References C, and M_PI.

◆ LCHToLab()

KRITAPIGMENT_EXPORT void LCHToLab ( const qreal L,
const qreal C,
const qreal H,
qreal * l,
qreal * a,
qreal * b )

Definition at line 826 of file KoColorConversions.cpp.

827{
828 qreal chroma = qBound(0.0,C,1.0);
829 qreal hue = (qBound(0.0,H,1.0)*360.0)* M_PI / 180.0;
830 *l=qBound(0.0,L,1.0);
831 *a=(chroma * cos(hue) ) + 0.5;
832 *b=(chroma * sin(hue) ) + 0.5;
833}

References C, and M_PI.

◆ rgb_to_hls() [1/2]

KRITAPIGMENT_EXPORT void rgb_to_hls ( quint8 r,
quint8 g,
quint8 b,
float * h,
float * l,
float * s )

Definition at line 223 of file KoColorConversions.cpp.

224{
225 float r = red / 255.0;
226 float g = green / 255.0;
227 float b = blue / 255.0;
228 float h = 0;
229 float l = 0;
230 float s = 0;
231
232 float max, min, delta;
233
234 max = qMax(r, g);
235 max = qMax(max, b);
236
237 min = qMin(r, g);
238 min = qMin(min, b);
239
240 delta = max - min;
241
242 l = (max + min) / 2;
243
244 if (delta == 0) {
245 // This is a gray, no chroma...
246 h = 0;
247 s = 0;
248 } else {
249 if (l < 0.5)
250 s = delta / (max + min);
251 else
252 s = delta / (2 - max - min);
253
254 float delta_r, delta_g, delta_b;
255
256 delta_r = ((max - r) / 6) / delta;
257 delta_g = ((max - g) / 6) / delta;
258 delta_b = ((max - b) / 6) / delta;
259
260 if (r == max)
261 h = delta_b - delta_g;
262 else if (g == max)
263 h = (1.0 / 3) + delta_r - delta_b;
264 else if (b == max)
265 h = (2.0 / 3) + delta_g - delta_r;
266
267 if (h < 0) h += 1;
268 if (h > 1) h += 1;
269
270 }
271
272 *hue = h * 360;
273 *saturation = s;
274 *lightness = l;
275}
T min(T a, T b, T c)
constexpr std::enable_if< sizeof...(values)==0, size_t >::type max()

◆ rgb_to_hls() [2/2]

KRITAPIGMENT_EXPORT void rgb_to_hls ( quint8 r,
quint8 g,
quint8 b,
int * h,
int * l,
int * s )

Definition at line 310 of file KoColorConversions.cpp.

311{
312 float hue, saturation, lightness;
313
314 rgb_to_hls(r, g, b, &hue, &lightness, &saturation);
315 *h = (int)(hue + 0.5);
316 *l = (int)(lightness * 255 + 0.5);
317 *s = (int)(saturation * 255 + 0.5);
318}
void rgb_to_hls(quint8 red, quint8 green, quint8 blue, float *hue, float *lightness, float *saturation)

References rgb_to_hls().

◆ rgb_to_hsv()

KRITAPIGMENT_EXPORT void rgb_to_hsv ( int R,
int G,
int B,
int * H,
int * S,
int * V )

A number of often-used conversions between color models

Definition at line 18 of file KoColorConversions.cpp.

19{
20 unsigned int max = R;
21 unsigned int min = R;
22 unsigned char maxValue = 0; // r = 0, g = 1, b = 2
23
24 // find maximum and minimum RGB values
25 if (static_cast<unsigned int>(G) > max) {
26 max = G;
27 maxValue = 1;
28 }
29
30 if (static_cast<unsigned int>(B) > max) {
31 max = B;
32 maxValue = 2;
33 }
34
35 if (static_cast<unsigned int>(G) < min)
36 min = G;
37
38 if (static_cast<unsigned int>(B) < min)
39 min = B;
40
41 int delta = max - min;
42 *V = max; // value
43 *S = max ? (510 * delta + max) / (2 * max) : 0; // saturation
44
45 // calc hue
46 if (*S == 0)
47 *H = -1; // undefined hue
48 else {
49 switch (maxValue) {
50 case 0: // red
51 if (G >= B)
52 *H = (120 * (G - B) + delta) / (2 * delta);
53 else
54 *H = (120 * (G - B + delta) + delta) / (2 * delta) + 300;
55 break;
56 case 1: // green
57 if (B > R)
58 *H = 120 + (120 * (B - R) + delta) / (2 * delta);
59 else
60 *H = 60 + (120 * (B - R + delta) + delta) / (2 * delta);
61 break;
62 case 2: // blue
63 if (R > G)
64 *H = 240 + (120 * (R - G) + delta) / (2 * delta);
65 else
66 *H = 180 + (120 * (R - G + delta) + delta) / (2 * delta);
67 break;
68 }
69 }
70}

References B, G, R, and S.

◆ RGBToHCI()

KRITAPIGMENT_EXPORT void RGBToHCI ( const qreal r,
const qreal g,
const qreal b,
qreal * h,
qreal * c,
qreal * i )

Definition at line 673 of file KoColorConversions.cpp.

674{
675 qreal minval = qMin(r, qMin(g, b));
676 qreal maxval = qMax(r, qMax(g, b));
677 qreal hue = 0.0;
678 qreal sat = 0.0;
679 qreal intensity = 0.0;
680 intensity=(r+g+b)/3.0;
681 qreal chroma = maxval-minval;
682 if(chroma==0) {
683 hue = 0.0;
684 sat = 0.0;
685 } else {
686 //the following finds the hue
687
688 if(maxval==r) {
689 if (minval==b) {
690 hue = (g-b)/chroma;
691 } else {
692 hue = (g-b)/chroma + 6.0;
693 }
694 } else if(maxval==g) {
695 hue = (b-r)/chroma + 2.0;
696 } else if(maxval==b) {
697 hue = (r-g)/chroma + 4.0;
698 }
699 hue /=6.0;//this makes sure that hue is in the 0-1.0 range.
700 sat= 1-(minval/intensity);
701 }
702
703 *h=hue;
704 *c=sat;
705 *i=intensity;
706
707}

◆ RGBToHCY()

KRITAPIGMENT_EXPORT void RGBToHCY ( const qreal r,
const qreal g,
const qreal b,
qreal * h,
qreal * c,
qreal * y,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 739 of file KoColorConversions.cpp.

740{
741 qreal minval = qMin(r, qMin(g, b));
742 qreal maxval = qMax(r, qMax(g, b));
743 qreal hue = 0.0;
744 qreal chroma = 0.0;
745 qreal luma = 0.0;
746 //weights for rgb, these are the same weights used in color space maths and the desaturate.
747 //qreal R=0.299;
748 //qreal G=0.587;
749 //qreal B=0.114;
750 luma=(R*r+G*g+B*b);
751 chroma = maxval-minval;
752
753 if(chroma==0) {
754 hue = 0.0;
755 }
756 else {
757 //the following finds the hue
758
759 if(maxval==r) {
760 //hue = fmod(((g-b)/chroma), 6.0);
761 //above doesn't work so let's try this one:
762 if (minval==b) {
763 hue = (g-b)/chroma;
764 } else {
765 hue = (g-b)/chroma + 6.0;
766 }
767 } else if(maxval==g) {
768 hue = (b-r)/chroma + 2.0;
769 } else if(maxval==b) {
770 hue = (r-g)/chroma + 4.0;
771 }
772 hue /=6.0;//this makes sure that hue is in the 0-1.0 range.
773 }
774 if (chroma<0.0){chroma=0.0;}
775 if (luma<0.0){luma=0.0;}
776
777 *h=qBound(0.0,hue,1.0);
778 *c=chroma;
779 *y=luma;
780
781}

References B, G, and R.

◆ RGBToHSI()

KRITAPIGMENT_EXPORT void RGBToHSI ( qreal r,
qreal g,
qreal b,
qreal * h,
qreal * s,
qreal * i )

Definition at line 416 of file KoColorConversions.cpp.

417{
418 qreal onethird = 1.0/3.0;
419 RGBToHSY(r, g, b, h, s, i, onethird, onethird, onethird);
420
421}
void RGBToHSY(const qreal r, const qreal g, const qreal b, qreal *h, qreal *s, qreal *y, qreal R, qreal G, qreal B)

References RGBToHSY().

◆ RGBToHSL()

KRITAPIGMENT_EXPORT void RGBToHSL ( float r,
float g,
float b,
float * h,
float * s,
float * l )

Definition at line 335 of file KoColorConversions.cpp.

336{
337 float v;
338 float m;
339 float vm;
340 float r2, g2, b2;
341
342 v = qMax(r, g);
343 v = qMax(v, b);
344 m = qMin(r, g);
345 m = qMin(m, b);
346
347 if ((*l = (m + v) / 2.0) <= 0.0) {
348 *h = UNDEFINED_HUE;
349 *s = 0;
350 return;
351 }
352 if ((*s = vm = v - m) > 0.0) {
353 *s /= (*l <= 0.5) ? (v + m) :
354 (2.0 - v - m) ;
355 } else {
356 *h = UNDEFINED_HUE;
357 return;
358 }
359
360
361 r2 = (v - r) / vm;
362 g2 = (v - g) / vm;
363 b2 = (v - b) / vm;
364
365 if (r == v)
366 *h = (g == m ? 5.0 + b2 : 1.0 - g2);
367 else if (g == v)
368 *h = (b == m ? 1.0 + r2 : 3.0 - b2);
369 else
370 *h = (r == m ? 3.0 + g2 : 5.0 - r2);
371
372 *h *= 60;
373 *h = fmod(*h, 360.0);
374}
QPointF r2

References r2, UNDEFINED_HUE, and v.

◆ RGBToHSV()

KRITAPIGMENT_EXPORT void RGBToHSV ( float r,
float g,
float b,
float * h,
float * s,
float * v )

Definition at line 132 of file KoColorConversions.cpp.

133{
134 float max = qMax(r, qMax(g, b));
135 float min = qMin(r, qMin(g, b));
136
137 *v = max;
138
139 if (max > EPSILON) {
140 *s = (max - min) / max;
141 } else {
142 *s = 0;
143 }
144
145 if (*s < EPSILON) {
146 *h = UNDEFINED_HUE;
147 } else {
148 float delta = max - min;
149
150 if (r == max) {
151 *h = (g - b) / delta;
152 } else if (g == max) {
153 *h = 2 + (b - r) / delta;
154 } else {
155 *h = 4 + (r - g) / delta;
156 }
157
158 *h *= 60;
159 if (*h < 0) {
160 *h += 360;
161 }
162 }
163}

References EPSILON, UNDEFINED_HUE, and v.

◆ RGBToHSY()

KRITAPIGMENT_EXPORT void RGBToHSY ( qreal r,
qreal g,
qreal b,
qreal * h,
qreal * s,
qreal * y,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 541 of file KoColorConversions.cpp.

542{
543//This is LUMA btw, not Luminance.
544//Using these RGB values, we calculate the H, S and I.
545 qreal red; qreal green; qreal blue;
546 if (r<0.0){red=0.0;} else {red=r;}
547 if (g<0.0){green=0.0;} else {green=g;}
548 if (b<0.0){blue=0.0;} else {blue=b;}
549
550 qreal minval = qMin(r, qMin(g, b));
551 qreal maxval = qMax(r, qMax(g, b));
552 qreal hue = 0.0;
553 qreal sat = 0.0;
554 qreal luma = 0.0;
555 //weights for rgb, these are the same weights used in color space maths and the desaturate.
556 //qreal R=0.299;
557 //qreal G=0.587;
558 //qreal B=0.114;
559 luma=(R*red+G*green+B*blue);
560 qreal luma_a=luma;//defined later
561 qreal chroma = maxval-minval;
562 qreal max_sat=0.5;
563 if(chroma==0) {
564 hue = 0.0;
565 sat = 0.0;
566 } else {
567 //the following finds the hue
568
569 if(maxval==r) {
570 //hue = fmod(((g-b)/chroma), 6.0);
571 //above doesn't work so let's try this one:
572 if (minval==b) {
573 hue = (g-b)/chroma;
574 } else {
575 hue = (g-b)/chroma + 6.0;
576 }
577 } else if(maxval==g) {
578 hue = (b-r)/chroma + 2.0;
579 } else if(maxval==b) {
580 hue = (r-g)/chroma + 4.0;
581 }
582
583 hue /=6.0;//this makes sure that hue is in the 0-1.0 range.
584 //Most HSY formula will tell you that Sat=Chroma. However, this HSY' formula tries to be a
585 //weighted HSL formula, where instead of 0.5, we search for a Max_Sat value, which is the Y'
586 //at which the saturation is maximum.
587 //This requires using the hue, and combining the weighting values accordingly.
588 qreal segment = 0.166667;
589 if (hue>1.0 || hue<0.0) {
590 hue=fmod(hue, 1.0);
591 }
592
593 if (hue>=0.0 && hue<segment) {
594 max_sat = R + G*(hue*6);
595 } else if (hue>=segment && hue<(2.0*segment)) {
596 max_sat = (G+R) - R*((hue-segment)*6);
597 } else if (hue>=(2.0*segment) && hue<(3.0*segment)) {
598 max_sat = G + B*((hue-2.0*segment)*6);
599 } else if (hue>=(3.0*segment) && hue<(4.0*segment)) {
600 max_sat = (B+G) - G*((hue-3.0*segment)*6);
601 } else if (hue>=(4.0*segment) && hue<(5.0*segment)) {
602 max_sat = (B) + R*((hue-4.0*segment)*6);
603 } else if (hue>=(5.0*segment) && hue<=1.0) {
604 max_sat = (R+B) - B*((hue-5.0*segment)*6);
605 } else {
606 max_sat=0.5;
607 }
608
609 if(max_sat>1.0 || max_sat<0.0){ //This should not show up during normal use
610 max_sat=(fmod(max_sat,1.0));
611 } //If it does, it'll try to correct, but it's not good!
612
613 //This is weighting the luma for the saturation)
614 if (luma <= max_sat) {
615 luma_a = (luma/max_sat)*0.5;
616 } else{
617 luma_a = ((luma-max_sat)/(1-max_sat)*0.5)+0.5;
618 }
619
620 if (chroma > 0.0) {
621 sat = (luma <= max_sat) ? (chroma/ (2*luma_a) ) :(chroma/(2.0-(2*luma_a) ) ) ;
622 }
623 }
624
625 //if (sat>1.0){sat=1.0;}
626 //if (luma>1.0){luma=1.0;}
627 if (sat<0.0){sat=0.0;}
628 if (luma<0.0){luma=0.0;}
629
630 *h=hue;
631 *s=sat;
632 *y=luma;
633
634
635}

References B, G, and R.

◆ RGBToYUV()

KRITAPIGMENT_EXPORT void RGBToYUV ( qreal r,
qreal g,
qreal b,
qreal * y,
qreal * cb,
qreal * cr,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 782 of file KoColorConversions.cpp.

783{
784 qreal uvmax = 0.5;
785 qreal luma = R*r+G*g+B*b;
786 qreal chromaBlue = uvmax*( (b - luma) / (1.0-B) );
787 qreal chromaRed = uvmax*( (r - luma) / (1.0-R) );
788
789 *y = luma; //qBound(0.0,luma,1.0);
790 *u = chromaBlue+uvmax;//qBound(0.0,chromaBlue+ uvmax,1.0);
791 *v = chromaRed+uvmax;//qBound(0.0,chromaRed + uvmax,1.0);
792}
qreal u

References B, G, R, u, and v.

◆ xyYToXYZ()

KRITAPIGMENT_EXPORT void xyYToXYZ ( const qreal x,
const qreal y,
const qreal yY,
qreal * X,
qreal * Y,
qreal * Z )

Definition at line 844 of file KoColorConversions.cpp.

845{
846 qreal xb = x;
847 qreal yb = y;
848 qreal yYb = yY;
849 *X=(xb*yYb)/yb;
850 *Z=((1.0-xb-yb)*yYb)/yb;
851 *Y=yYb;
852}
const QString Y
const QString X

◆ XYZToxyY()

KRITAPIGMENT_EXPORT void XYZToxyY ( const qreal X,
const qreal Y,
const qreal Z,
qreal * x,
qreal * y,
qreal * yY )

Definition at line 835 of file KoColorConversions.cpp.

836{
837 qreal Xb = X;
838 qreal Yb = Y;
839 qreal Zb = Z;
840 *x=Xb/(Xb+Yb+Zb);
841 *y=Yb/(Xb+Yb+Zb);
842 *yY=Yb;
843}

◆ YUVToRGB()

KRITAPIGMENT_EXPORT void YUVToRGB ( const qreal y,
const qreal cb,
const qreal cr,
qreal * r,
qreal * g,
qreal * b,
qreal R = 0.299,
qreal G = 0.587,
qreal B = 0.114 )

Definition at line 793 of file KoColorConversions.cpp.

794{
795 qreal uvmax = 0.5;
796 qreal chromaBlue = u-uvmax;//qBound(0.0,u,1.0)- uvmax;//put into -0.5-+0.5 range//
797 qreal chromaRed = v-uvmax;//qBound(0.0,v,1.0)- uvmax;
798
799 qreal negB = 1.0-B;
800 qreal negR = 1.0-R;
801 qreal red = y+(chromaRed * (negR / uvmax) );
802 qreal green = y-(chromaBlue * ((B*negB) / (uvmax*G)) ) - (chromaRed* ((R*negR) / (uvmax*G)));
803 qreal blue = y+(chromaBlue * (negB / uvmax) );
804
805 *r=red;//qBound(0.0,red ,1.0);
806 *g=green;//qBound(0.0,green,1.0);
807 *b=blue;//qBound(0.0,blue ,1.0);
808}

References B, G, R, u, and v.