• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

rgb_to_hsl.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory
00002 // (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_FUN_V2V_RGB_TO_HSL_HH
00028 # define MLN_FUN_V2V_RGB_TO_HSL_HH
00029 
00030 # include <cmath>
00031 
00032 # include <mln/math/round.hh>
00033 # include <mln/math/max.hh>
00034 # include <mln/math/min.hh>
00035 
00036 # include <mln/trait/value_.hh>
00037 
00038 # include <mln/value/rgb.hh>
00039 
00040 namespace mln
00041 {
00042 
00043   // Forward declaration
00044   namespace value
00045   {
00046     template <typename H, typename S, typename L> class hsl_;
00047     typedef hsl_<float, float, float> hsl_f;
00048   }
00049 
00050   namespace fun
00051   {
00052 
00053     namespace v2v
00054     {
00055 
00056       template <typename T_hsl>
00057       struct f_rgb_to_hsl_ : public Function_v2v< f_rgb_to_hsl_<T_hsl> >
00058       {
00059         typedef T_hsl result;
00060 
00061         template <typename T_rgb>
00062         T_hsl operator()(const T_rgb& rgb) const;
00063 
00064       };
00065 
00066       typedef f_rgb_to_hsl_<value::hsl_f> f_rgb_to_hsl_f_t;
00067 
00068       extern f_rgb_to_hsl_f_t f_rgb_to_hsl_f;
00069 
00070 
00071 # ifndef MLN_INCLUDE_ONLY
00072 
00075       f_rgb_to_hsl_f_t f_rgb_to_hsl_f;
00077 
00078 
00079       template <typename T_hsl>
00080       template <typename T_rgb>
00081       inline
00082       T_hsl
00083       f_rgb_to_hsl_<T_hsl>::operator()(const T_rgb& rgb) const
00084       {
00085         // Locals.
00086         T_hsl hsl;
00087 
00088         typename T_rgb::red_t rmax = math::max(rgb.red(), math::max(rgb.green(), rgb.blue()));
00089         typename T_rgb::red_t rmin = math::min(rgb.red(), math::min(rgb.green(), rgb.blue()));
00090 
00091         if (rmin == rmax)
00092           hsl.hue() = 0;
00093         else
00094           if (rmax == rgb.red())
00095             {
00096               hsl.hue() = (60. * (rgb.green() - rgb.blue()) / (rmax - rmin));
00097               if (hsl.hue() < 0)
00098                 hsl.hue() += 360.;
00099             }
00100           else
00101             if (rmax == rgb.green())
00102               hsl.hue() = (60. * (rgb.blue() - rgb.red()) / (rmax - rmin)) + 120.;
00103             else
00104               hsl.hue() = (60. * (rgb.red() - rgb.green()) / (rmax - rmin)) + 240;
00105 
00106         // We want min and max between 0 and 1
00107         rmax -= mln_min(typename T_rgb::red_t);
00108         rmin -= mln_min(typename T_rgb::red_t);
00109         double nmax = (double) rmax / (mln_max(typename T_rgb::red_t) - mln_min(typename T_rgb::red_t));
00110         double nmin = (double) rmin / (mln_max(typename T_rgb::red_t) - mln_min(typename T_rgb::red_t));
00111 
00112         hsl.lum() = ((double) nmax + (double) nmin) / 2;
00113 
00114         if (rmin == rmax)
00115           hsl.sat() = 0;
00116         else
00117           if (hsl.lum() <= 0.5)
00118             hsl.sat() = (nmax - nmin) / (nmax + nmin);
00119           else
00120             hsl.sat() = (nmax - nmin) / (2 - nmax - nmin);
00121 
00122         return hsl;
00123       }
00124 
00125 
00126 # endif // !MLN_INCLUDE_ONLY
00127 
00128     } // end of namespace fun::v2v
00129 
00130   } // end of namespace fun
00131 
00132 } // end of namespace mln
00133 
00134 #endif // ! MLN_FUN_V2V_RGB_TO_HSL_HH

Generated on Tue Oct 4 2011 15:24:22 for Milena (Olena) by  doxygen 1.7.1