15 if (hue > 360) hue = hue - 360;
16 if (hue < 0) hue = hue + 360;
17 if (hue < 60 )
return rn1 + (rn2-rn1)*hue/60;
18 if (hue < 180)
return rn2;
19 if (hue < 240)
return rn1 + (rn2-rn1)*(240-hue)/60;
24 inline void hls_to_rgb(T hue,T light,T satur,T& a_r,T& a_g,T& a_b){
29 T rh, rl, rs, rm1, rm2;
32 if (hue > 0) rh = hue;
33 if (rh > 360) rh = 360;
35 if (light > 0) rl = light;
38 if (satur > 0) rs = satur;
44 rm2 = rl + rs - rl*rs;
48 if (!rs) { a_r = rl; a_g = rl; a_b = rl;
return; }
55 inline void rgb_to_hls(T rr,T gg,T bb,T& hue,T& light,T& satur) {
59 T rnorm, gnorm, bnorm, minval, maxval, msum, mdiff, cr, cg, cb;
73 if (cg < minval) minval = cg;
74 if (cb < minval) minval = cb;
76 if (cg > maxval) maxval = cg;
77 if (cb > maxval) maxval = cb;
79 rnorm = gnorm = bnorm = 0;
80 mdiff = maxval - minval;
81 msum = maxval + minval;
82 light = T(0.5) * msum;
83 if (maxval != minval) {
84 rnorm = (maxval - cr)/mdiff;
85 gnorm = (maxval - cg)/mdiff;
86 bnorm = (maxval - cb)/mdiff;
95 satur = mdiff/(2 - msum);
98 hue = 60 * (6 + bnorm - gnorm);
99 else if (cg == maxval)
100 hue = 60 * (2 + rnorm - bnorm);
102 hue = 60 * (4 + gnorm - rnorm);