27 #ifndef MLN_FUN_V2V_HSL_TO_RGB_HH
28 # define MLN_FUN_V2V_HSL_TO_RGB_HH
38 # include <mln/math/round.hh>
39 # include <mln/math/max.hh>
40 # include <mln/math/min.hh>
42 # include <mln/trait/value_.hh>
44 # include <mln/value/rgb.hh>
54 template <
typename H,
typename S,
typename L>
class hsl_;
55 typedef hsl_<float, float, float> hsl_f;
56 template <
unsigned n>
struct rgb;
69 template <
typename T_rgb>
70 struct f_hsl_to_rgb_ :
public Function_v2v< f_hsl_to_rgb_<T_rgb> >
77 template <
typename T_hsl>
78 T_rgb operator()(
const T_hsl& hsl)
const;
82 typedef f_hsl_to_rgb_< value::rgb<8> > f_hsl_to_rgb_3x8_t;
83 typedef f_hsl_to_rgb_< value::rgb<16> > f_hsl_to_rgb_3x16_t;
86 extern f_hsl_to_rgb_3x16_t f_hsl_to_rgb_3x16;
89 # ifndef MLN_INCLUDE_ONLY
91 # ifndef MLN_WO_GLOBAL_VARS
96 f_hsl_to_rgb_3x16_t f_hsl_to_rgb_3x16;
98 # endif // !MLN_WO_GLOBAL_VARS
100 template <
typename T_rgb>
101 f_hsl_to_rgb_<T_rgb>::f_hsl_to_rgb_()
108 template <
typename T_rgb>
109 template <
typename T_hsl>
112 f_hsl_to_rgb_<T_rgb>::operator()(
const T_hsl& hsl)
const
114 typedef typename T_rgb::red_t red_t;
115 typedef typename T_rgb::green_t green_t;
116 typedef typename T_rgb::blue_t blue_t;
118 static math::round<red_t> to_r;
119 static math::round<green_t> to_g;
120 static math::round<blue_t> to_b;
122 const float q = (hsl.lum() < 0.5) ? hsl.lum() * (1.0 + hsl.sat()) :
123 hsl.lum() + hsl.sat() - (hsl.lum() * hsl.sat());
124 const float p = 2.0 * hsl.lum() - q;
125 const float hk = hsl.hue() / 360.0;
126 float tr = hk + (1.0 / 3.0);
128 float tb = hk - (1.0 / 3.0);
147 if (tr < (1.0 / 6.0))
148 red = p + ((q - p) * 6 * tr);
149 else if (tr < (1.0 / 2.0))
151 else if (tr < (2.0 / 3.0))
152 red = p + ((q - p) * 6 * ((2.0 / 3.0) - tr));
158 if (tg < (1.0 / 6.0))
159 green = p + ((q - p) * 6 * tg);
160 else if (tg < (1.0 / 2.0))
162 else if (tg < (2.0 / 3.0))
163 green = p + ((q - p) * 6 * ((2.0 / 3.0) - tg));
169 if (tb < (1.0 / 6.0))
170 blue = p + ((q - p) * 6 * tb);
171 else if (tb < (1.0 / 2.0))
173 else if (tb < (2.0 / 3.0))
174 blue = p + ((q - p) * 6 * ((2.0 / 3.0) - tb));
179 red_t r = to_r(red * 255);
180 green_t g = to_g(green * 255);
181 blue_t b = to_b(blue * 255);
183 T_rgb rgb_result(r, g, b);
188 # endif // !MLN_INCLUDE_ONLY
197 #endif // ! MLN_FUN_V2V_HSL_TO_RGB_HH