2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file ColorBrightnessColorMap.ih
19 * @author Sebastien Fourey (\c Sebastien.Fourey@greyc.ensicaen.fr )
20 * Groupe de Recherche en Informatique, Image, Automatique et Instrumentation de Caen - GREYC (CNRS, UMR 6072), ENSICAEN, France
24 * Implementation of inline methods defined in ColorBrightnessColorMap.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
42 template <typename PValue, int PDefaultColor>
44 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::ColorBrightnessColorMap
45 ( const PValue & minV, const PValue & maxV, const Color color )
46 : myMin( minV ), myMax( maxV ), myColor( color )
48 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
52 template <typename PValue, int PDefaultColor>
54 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::ColorBrightnessColorMap
55 ( const ColorBrightnessColorMap<PValue,PDefaultColor> & other )
56 : myMin( other.myMin ), myMax( other.myMax ), myColor( other.myColor )
58 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
61 template <typename PValue, int PDefaultColor>
63 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::~ColorBrightnessColorMap()
67 template <typename PValue, int PDefaultColor>
68 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor> &
69 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::operator=
70 ( const ColorBrightnessColorMap<PValue,PDefaultColor> & other )
72 if ( &other != this ) {
75 myColor = other.myColor;
76 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
81 ///////////////////////////////////////////////////////////////////////////////
82 // Interface - public :
84 template<typename PValue, int PDefaultColor>
87 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::min() const
92 template<typename PValue, int PDefaultColor>
95 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::max() const
100 template<typename PValue, int PDefaultColor>
103 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::operator()( const PValue & value ) const
105 return ColorBrightnessColorMap<PValue,PDefaultColor>::getColor( myColor, myMin, myMax, value );
109 * Writes/Displays the object on an output stream.
110 * @param out the output stream where the object is written.
112 template <typename PValue, int PDefaultColor>
115 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::selfDisplay ( std::ostream & out ) const
117 out << "[ColorBrightnessColorMap "
121 << myColor.red() << ","
122 << myColor.green() << ","
123 << myColor.blue() << ") "
128 * Checks the validity/consistency of the object.
129 * @return 'true' if the object is valid, 'false' otherwise.
131 template <typename PValue, int PDefaultColor>
134 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::isValid() const
139 template <typename PValue, int PDefaultColor>
142 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::getColor( const Color color,
145 const PValue & value )
147 ASSERT_MSG(min < max, "Max should be strictly greather than Min in a colormap.");
149 ColorBrightnessColorMap<PValue,PDefaultColor>::RGBtoHSV( h, s, v,
153 const double range = static_cast<double>( max - min );
154 const double scale = static_cast<double>( value - min ) / range;
155 double red, green, blue;
156 ColorBrightnessColorMap<PValue,PDefaultColor>::HSVtoRGB( red, green, blue, h, s, v * scale );
157 return DGtal::Color( static_cast<unsigned char>( red * 255),
158 static_cast<unsigned char>( green * 255),
159 static_cast<unsigned char>( blue * 255) );
162 ///////////////////////////////////////////////////////////////////////////////
163 // Implementation of inline functions //
165 template <typename PValue, int PDefaultColor>
168 DGtal::operator<< ( std::ostream & out,
169 const ColorBrightnessColorMap<PValue,PDefaultColor> & object )
171 object.selfDisplay( out );
176 ///////////////////////////////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////////////////////////////
180 // Interface - private :
182 template <typename PValue, int PDefaultColor>
185 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::HSVtoRGB
186 ( double &r, double &g, double &b,
187 const double h, const double s, const double v)
191 if( s == 0 ) { // achromatic (gray)
195 i = static_cast<int>( floor( h / 60 ) );
196 f = ( h / 60 ) - i; // factorial part of h
198 q = v * ( 1 - s * f );
199 t = v * ( 1 - s * ( 1 - f ) );
222 template <typename PValue, int PDefaultColor>
225 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::RGBtoHSV
226 ( double &h, double &s, double &v,
227 const unsigned char r,
228 const unsigned char g,
229 const unsigned char b )
231 double min = (r<g) ? r : g;
232 if ( b < min ) min = b;
233 unsigned char max = (r>g) ? r : g;
234 if ( b > max ) max = b;
236 double dr = r / 255.0;
237 double dg = g / 255.0;
238 double db = b / 255.0;
239 v = max / 255.0; // (0.3*dr + 0.59*dg + 0.11*db);
245 double diff = ( max - min ) / 255.0;
247 h = (dg - db ) / diff;
248 } else if ( max == g ) {
249 h = 2.0 + ( ( db - dr ) / diff );
250 } else if ( max == b ) {
251 h = 4.0 + ( ( dr - dg ) / diff );
254 if ( h < 0 ) h += 360;