27 #ifndef MLN_FUN_V2V_RGB_TO_HSL_HH
28 # define MLN_FUN_V2V_RGB_TO_HSL_HH
32 # include <mln/math/round.hh>
33 # include <mln/math/max.hh>
34 # include <mln/math/min.hh>
36 # include <mln/trait/value_.hh>
38 # include <mln/value/rgb.hh>
46 template <
typename H,
typename S,
typename L>
class hsl_;
47 typedef hsl_<float, float, float> hsl_f;
56 template <
typename T_hsl>
57 struct f_rgb_to_hsl_ :
public Function_v2v< f_rgb_to_hsl_<T_hsl> >
63 template <
typename T_rgb>
64 T_hsl operator()(
const T_rgb& rgb)
const;
68 typedef f_rgb_to_hsl_<value::hsl_f> f_rgb_to_hsl_f_t;
73 # ifndef MLN_INCLUDE_ONLY
75 # ifndef MLN_WO_GLOBAL_VARS
82 # endif // ! MLN_WO_GLOBAL_VARS
84 template <
typename T_hsl>
85 f_rgb_to_hsl_<T_hsl>::f_rgb_to_hsl_()
89 template <
typename T_hsl>
90 template <
typename T_rgb>
93 f_rgb_to_hsl_<T_hsl>::operator()(
const T_rgb&
rgb)
const
98 typename T_rgb::red_t rmax = math::max(rgb.red(), math::max(rgb.green(), rgb.blue()));
99 typename T_rgb::red_t rmin = math::min(rgb.red(), math::min(rgb.green(), rgb.blue()));
104 if (rmax == rgb.red())
106 hsl.hue() = (60. * (rgb.green() - rgb.blue()) / (rmax - rmin));
111 if (rmax == rgb.green())
112 hsl.hue() = (60. * (rgb.blue() - rgb.red()) / (rmax - rmin)) + 120.;
114 hsl.hue() = (60. * (rgb.red() - rgb.green()) / (rmax - rmin)) + 240;
117 rmax -= mln_min(
typename T_rgb::red_t);
118 rmin -= mln_min(
typename T_rgb::red_t);
119 double nmax = (double) rmax / (mln_max(
typename T_rgb::red_t) - mln_min(
typename T_rgb::red_t));
120 double nmin = (double) rmin / (mln_max(
typename T_rgb::red_t) - mln_min(
typename T_rgb::red_t));
122 hsl.lum() = ((double) nmax + (
double) nmin) / 2;
127 if (hsl.lum() <= 0.5)
128 hsl.sat() = (nmax - nmin) / (nmax + nmin);
130 hsl.sat() = (nmax - nmin) / (2 - nmax - nmin);
136 # endif // !MLN_INCLUDE_ONLY
144 #endif // ! MLN_FUN_V2V_RGB_TO_HSL_HH