// HSL-2-RGB void hsl2rgb(t_cs *x, int h, int s, int l) { long red, green, blue; double m1,m2,tr,tg,tb, hue, lightness, saturation; // scale to floating point... number range should be 360, 1, 1 hue = (float)h; //hue = ((hue/255.)*360.); saturation = (float)s/100.0; lightness = (float)l/100.0; if (lightness <= 0.5) m2 = lightness*(1.0+saturation); else m2 = lightness+saturation-lightness*saturation; m1 = 2.0*lightness-m2; if (saturation == 0.0){ tr = lightness; tg = lightness; tb = lightness; } else{ tr = hls_value(m1,m2,hue+120.); tg = hls_value(m1,m2,hue); tb = hls_value(m1,m2,hue-120.); } red = (int)(tr*255.); green = (int)(tg*255.); blue = (int)(tb*255.); x->calc1 = red; x->calc2 = green; x->calc3 = blue; }
void HSLUnit::convertToNeutral(const TTValue& input, TTValue& output) { double h = input.getFloat64(0);// input.getFloat64(0); double s = input.getFloat64(1);// input.getFloat64(1); double l = input.getFloat64(2);// input.getFloat64(2); double red, green, blue; double m1, m2, hue, lightness, saturation; // scale to floating point... number range should be 360, 1, 1 hue = h; saturation = s/100.0; lightness = l/100.0; if(lightness <= 0.5) m2 = lightness*(1.0+saturation); else m2 = lightness+saturation-lightness*saturation; m1 = 2.0 * lightness-m2; if(saturation == 0.0){ red = lightness; green = lightness; blue = lightness; } else{ red = hls_value(m1, m2, hue+120.0); green = hls_value(m1, m2, hue); blue = hls_value(m1, m2, hue-120.0); } output.setSize(3); output.set(0, red); output.set(1, green); output.set(2, blue); }
/** * Convert HLS to RGB. * * The HLS color system describes a color based on the qualities of * hue, lightness, and saturation. A particular color has three * coordinates, (H,L,S). The L and S coordinates must be between * 0 and 1, while the H coordinate must be between 0 and 360, and * is interpreted as an angle. * * The RGB color system describes a color based on the amounts of the * base colors red, green, and blue. Thus, a particular color * has three coordinates, (R,G,B). Each coordinate must be between * 0 and 1. */ void HLS_to_RGB(float H, float L, float S, float &R, float &G, float &B) { float m1, m2; if (L <= 0.5) { m2 = L + L*S; } else { m2 = L + S - L*S; } m1 = 2.0f * L - m2; if (S == 0.0) { R = G = B = L; } else { R = hls_value(m1, m2, H + 120.0f); G = hls_value(m1, m2, H); B = hls_value(m1, m2, H - 120.0f); } }
PLPixel24 hls2rgb (double h, double l, double s) { double m1, m2; l /= 255; s /= 100; // Warning: Foley, van Dam has a typo on the next line! m2 = (l<=0.5)?(l*(1.0+s)):(l+s-l*s); m1 = 2.0*l-m2; if (s<0.001) return PLPixel24(PLBYTE(l*255), PLBYTE(l*255), PLBYTE(l*255)); else { return PLPixel24(hls_value(m1,m2,h+120.0), hls_value(m1,m2,h), hls_value(m1,m2,h-120.0)); } }
inline void hls_to_rgb(float h, float l, float s, float &r, float &g, float &b) { /* Given: h in [0,360] or UNDEFINED, l and s in [0,1] * Desired: r, g, b each in [0,1] */ float m1,m2; m2 = (l<=0.5f)?(l+l*s):(l+s-l*s); m1 = (2.0f*l-m2); if (s==0.0f) r=g=b=l; else { r = hls_value(m1, m2, h+120.0f); g = hls_value(m1, m2, h); b = hls_value(m1, m2, h-120.0f); } }