1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__
10 #include <vnl/vnl_math.h>
12 #include <itkRGBPixel.h>
13 #include <itkVector.h>
24 struct RGBToHSVFunction
26 typedef RGBToHSVFunction Self;
27 typedef itk::Vector< O, 3 > TOutPixel;
29 template< class Tr, class Tg, class Tb >
30 TOutPixel operator()( const Tr& r, const Tg& g, const Tb& b ) const
32 static const double mVal =
33 double( std::numeric_limits< O >::max( ) );
34 static const double _0 = double( 0 );
35 static const double _1 = double( 1 );
36 static const double _2 = double( 2 );
37 static const double _3 = double( 3 );
38 static const double _2pi = _2 * double( vnl_math::pi );
42 double R = double( r );
43 double G = double( g );
44 double B = double( b );
45 double sRGB = R + G + B;
51 double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
53 A = std::acos( ( RG + RB ) / ( _2 * A ) );
55 hsv[ 0 ] = O( mVal * ( ( G >= B )? A: _1 - A ) );
60 double C = ( G < R )? G: R;
62 hsv[ 1 ] = O( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
68 hsv[ 2 ] = O( sRGB / _3 );
73 TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
77 rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
89 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__