1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __CPEXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__
6 #define __CPEXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__
10 #include <vnl/vnl_math.h>
12 #include <itkRGBPixel.h>
14 namespace cpExtensions
21 struct RGBToHSVFunction
23 typedef RGBToHSVFunction Self;
25 typedef typename P::ValueType TValue;
27 template< class Tr, class Tg, class Tb >
28 P operator()( const Tr& r, const Tg& g, const Tb& b ) const
30 static const double mVal =
31 double( std::numeric_limits< TValue >::max( ) );
32 static const double _0 = double( 0 );
33 static const double _1 = double( 1 );
34 static const double _2 = double( 2 );
35 static const double _3 = double( 3 );
36 static const double _2pi = _2 * double( vnl_math::pi );
40 double R = double( r );
41 double G = double( g );
42 double B = double( b );
43 double sRGB = R + G + B;
49 double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
51 A = std::acos( ( RG + RB ) / ( _2 * A ) );
53 hsv[ 0 ] = TValue( mVal * ( ( G >= B )? A: _1 - A ) );
58 double C = ( G < R )? G: R;
60 hsv[ 1 ] = TValue( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
63 hsv[ 1 ] = TValue( 0 );
66 hsv[ 2 ] = TValue( sRGB / _3 );
71 TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
75 rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
85 #endif // __CPEXTENSIONS__ALGORITHMS__RGBTOHSVFUNCTION__H__