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>
61 template <
typename T_rgb>
69 template <
typename T_hsl>
81 # ifndef MLN_INCLUDE_ONLY
83 # ifndef MLN_WO_GLOBAL_VARS
90 # endif // !MLN_WO_GLOBAL_VARS
92 template <
typename T_rgb>
100 template <
typename T_rgb>
101 template <
typename T_hsl>
106 typedef typename T_rgb::red_t red_t;
107 typedef typename T_rgb::green_t green_t;
108 typedef typename T_rgb::blue_t blue_t;
110 const float q = (hsl.lum() < 0.5) ? hsl.lum() * (1.0 + hsl.sat()) :
111 hsl.lum() + hsl.sat() - (hsl.lum() * hsl.sat());
112 const float p = 2.0 * hsl.lum() - q;
113 const float hk = hsl.hue() / 360.0;
114 float tr = hk + (1.0 / 3.0);
116 float tb = hk - (1.0 / 3.0);
135 if (tr < (1.0 / 6.0))
136 red = p + ((q -
p) * 6 * tr);
137 else if (tr < (1.0 / 2.0))
139 else if (tr < (2.0 / 3.0))
140 red = p + ((q -
p) * 6 * ((2.0 / 3.0) - tr));
146 if (tg < (1.0 / 6.0))
147 green = p + ((q -
p) * 6 * tg);
148 else if (tg < (1.0 / 2.0))
150 else if (tg < (2.0 / 3.0))
151 green = p + ((q -
p) * 6 * ((2.0 / 3.0) - tg));
157 if (tb < (1.0 / 6.0))
158 blue = p + ((q -
p) * 6 * tb);
159 else if (tb < (1.0 / 2.0))
161 else if (tb < (2.0 / 3.0))
162 blue = p + ((q -
p) * 6 * ((2.0 / 3.0) - tb));
167 red_t r = math::round<red_t>(red * 255);
168 green_t g = math::round<green_t>(green * 255);
169 blue_t b = math::round<blue_t>(blue * 255);
171 T_rgb rgb_result(r, g, b);
176 # endif // !MLN_INCLUDE_ONLY
185 #endif // ! MLN_FUN_V2V_HSL_TO_RGB_HH