00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef MLN_FUN_V2V_HSL_TO_RGB_HH
00028 # define MLN_FUN_V2V_HSL_TO_RGB_HH
00029 
00035 
00036 # include <cmath>
00037 
00038 # include <mln/math/round.hh>
00039 # include <mln/math/max.hh>
00040 # include <mln/math/min.hh>
00041 
00042 # include <mln/trait/value_.hh>
00043 
00044 # include <mln/value/rgb.hh>
00045 
00046 
00047 
00048 namespace mln
00049 {
00050 
00051   
00052   namespace value
00053   {
00054     template <typename H, typename S, typename L> class hsl_;
00055     typedef hsl_<float, float, float> hsl_f;
00056     template <unsigned n> struct rgb;
00057   }
00058 
00059   namespace fun
00060   {
00061 
00062     namespace v2v
00063     {
00064 
00068       
00069       template <typename T_rgb>
00070       struct f_hsl_to_rgb_ : public Function_v2v< f_hsl_to_rgb_<T_rgb> >
00071       {
00072         typedef T_rgb result;
00073 
00074         template <typename T_hsl>
00075         T_rgb operator()(const T_hsl& hsl) const;
00076 
00077       };
00078 
00079       typedef f_hsl_to_rgb_< value::rgb<8> > f_hsl_to_rgb_3x8_t;
00080       typedef f_hsl_to_rgb_< value::rgb<16> > f_hsl_to_rgb_3x16_t;
00081 
00082       extern f_hsl_to_rgb_3x8_t f_hsl_to_rgb_3x8;
00083       extern f_hsl_to_rgb_3x16_t f_hsl_to_rgb_3x16;
00084 
00085 
00086 # ifndef MLN_INCLUDE_ONLY
00087 
00090       f_hsl_to_rgb_3x8_t f_hsl_to_rgb_3x8;
00091       f_hsl_to_rgb_3x16_t f_hsl_to_rgb_3x16;
00093 
00094 
00095       template <typename T_rgb>
00096       template <typename T_hsl>
00097       inline
00098       T_rgb
00099       f_hsl_to_rgb_<T_rgb>::operator()(const T_hsl& hsl) const
00100       {
00101         typedef typename T_rgb::red_t   red_t;
00102         typedef typename T_rgb::green_t green_t;
00103         typedef typename T_rgb::blue_t  blue_t;
00104 
00105         static math::round<red_t>   to_r;
00106         static math::round<green_t> to_g;
00107         static math::round<blue_t>  to_b;
00108 
00109         static const float
00110           sqrt3_3 = std::sqrt(3.0f) / 3.0f,
00111                   inv_sqrt6 = 1 / std::sqrt(6.0f),
00112                   inv_sqrt2 = 1 / std::sqrt(2.0f);
00113 
00114         float
00115           h = hsl.hue() / 180.0 * 3.1415,
00116             alpha = hsl.sat() * std::cos(h),
00117             beta = hsl.sat() * std::sin(h);
00118 
00119 
00120         red_t   r = to_r(sqrt3_3 * hsl.lum() + 2 * inv_sqrt6 * beta);
00121         green_t g =
00122           to_g(sqrt3_3 * hsl.lum() + inv_sqrt2 * alpha - inv_sqrt6 * beta);
00123         blue_t  b =
00124           to_b(sqrt3_3 * hsl.lum() - inv_sqrt2 * alpha - inv_sqrt6 * beta);
00125 
00126         T_rgb rgb(r, g, b);
00127 
00128         return rgb;
00129       }
00130 
00131 # endif // !MLN_INCLUDE_ONLY
00132 
00133     } 
00134 
00135   } 
00136 
00137 } 
00138 
00139 
00140 #endif // ! MLN_FUN_V2V_HSL_TO_RGB_HH