27 #ifndef MLN_FUN_V2V_RGB_TO_HSI_HH
28 # define MLN_FUN_V2V_RGB_TO_HSI_HH
32 #include <mln/value/rgb8.hh>
33 #include <mln/math/round.hh>
35 #include <mln/value/hsi.hh>
48 template <
typename T_hsi>
49 struct f_rgb_to_hsi_ :
public Function_v2v< f_rgb_to_hsi_<T_hsi> >
55 template <
typename T_rgb>
56 T_hsi operator()(
const T_rgb& rgb)
const;
60 typedef f_rgb_to_hsi_<value::hsi_f> f_rgb_to_hsi_f_t;
65 # ifndef MLN_INCLUDE_ONLY
67 # ifndef MLN_WO_GLOBAL_VARS
74 # endif // ! MLN_WO_GLOBAL_VARS
77 template <
typename T_hsi>
78 f_rgb_to_hsi_<T_hsi>::f_rgb_to_hsi_()
82 template <
typename T_hsi>
83 template <
typename T_rgb>
86 f_rgb_to_hsi_<T_hsi>::operator()(
const T_rgb&
rgb)
const
89 static const double sqrt3_3 = std::sqrt(3) / 3;
90 static const double inv_sqrt6 = 1 / std::sqrt(6);
91 static const double inv_sqrt2 = 1 / std::sqrt(2);
95 double alpha = inv_sqrt2 * rgb.green() - inv_sqrt2 * rgb.blue();
97 2 * inv_sqrt6 * rgb.red() -
98 inv_sqrt6 * rgb.green() -
99 inv_sqrt6 * rgb.blue();
102 hsi.hue() = atan2(beta, alpha) / 3.1415 * 180.0;
104 hsi.hue() = hsi.hue() + 360.0;
105 mln_invariant(hsi.hue() >= 0);
106 hsi.sat() = std::sqrt(alpha * alpha + beta * beta);
108 sqrt3_3 * rgb.red() +
109 sqrt3_3 * rgb.green() +
110 sqrt3_3 * rgb.blue();
116 # endif // !MLN_INCLUDE_ONLY
124 #endif // ! MLN_FUN_V2V_RGB_TO_HSI_HH