DGtal  1.2.0
ColorBrightnessColorMap.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
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
21  *
22  * @date 2010/07/19
23  *
24  * Implementation of inline methods defined in ColorBrightnessColorMap.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 
42 template <typename PValue, int PDefaultColor>
43 inline
44 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::ColorBrightnessColorMap
45 ( const PValue & minV, const PValue & maxV, const Color color )
46  : myMin( minV ), myMax( maxV ), myColor( color )
47 {
48  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
49 }
50 
51 
52 template <typename PValue, int PDefaultColor>
53 inline
54 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::ColorBrightnessColorMap
55 ( const ColorBrightnessColorMap<PValue,PDefaultColor> & other )
56  : myMin( other.myMin ), myMax( other.myMax ), myColor( other.myColor )
57 {
58  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
59 }
60 
61 template <typename PValue, int PDefaultColor>
62 inline
63 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::~ColorBrightnessColorMap()
64 {
65 }
66 
67 template <typename PValue, int PDefaultColor>
68 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor> &
69 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::operator=
70 ( const ColorBrightnessColorMap<PValue,PDefaultColor> & other )
71 {
72  if ( &other != this ) {
73  myMin = other.myMin;
74  myMax = other.myMax;
75  myColor = other.myColor;
76  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
77  }
78  return *this;
79 }
80 
81 ///////////////////////////////////////////////////////////////////////////////
82 // Interface - public :
83 
84 template<typename PValue, int PDefaultColor>
85 inline
86 const PValue &
87 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::min() const
88 {
89  return myMin;
90 }
91 
92 template<typename PValue, int PDefaultColor>
93 inline
94 const PValue &
95 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::max() const
96 {
97  return myMax;
98 }
99 
100 template<typename PValue, int PDefaultColor>
101 inline
102 DGtal::Color
103 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::operator()( const PValue & value ) const
104 {
105  return ColorBrightnessColorMap<PValue,PDefaultColor>::getColor( myColor, myMin, myMax, value );
106 }
107 
108 /**
109  * Writes/Displays the object on an output stream.
110  * @param out the output stream where the object is written.
111  */
112 template <typename PValue, int PDefaultColor>
113 inline
114 void
115 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::selfDisplay ( std::ostream & out ) const
116 {
117  out << "[ColorBrightnessColorMap "
118  << " min=" << myMin
119  << " max=" << myMax
120  << " color=("
121  << myColor.red() << ","
122  << myColor.green() << ","
123  << myColor.blue() << ") "
124  << " ]";
125 }
126 
127 /**
128  * Checks the validity/consistency of the object.
129  * @return 'true' if the object is valid, 'false' otherwise.
130  */
131 template <typename PValue, int PDefaultColor>
132 inline
133 bool
134 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::isValid() const
135 {
136  return true;
137 }
138 
139 template <typename PValue, int PDefaultColor>
140 inline
141 DGtal::Color
142 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::getColor( const Color color,
143  const PValue & min,
144  const PValue & max,
145  const PValue & value )
146 {
147  ASSERT_MSG(min < max, "Max should be strictly greather than Min in a colormap.");
148  double h=0.0,s,v;
149  ColorBrightnessColorMap<PValue,PDefaultColor>::RGBtoHSV( h, s, v,
150  color.red(),
151  color.green(),
152  color.blue() );
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) );
160 }
161 
162 ///////////////////////////////////////////////////////////////////////////////
163 // Implementation of inline functions //
164 
165 template <typename PValue, int PDefaultColor>
166 inline
167 std::ostream&
168 DGtal::operator<< ( std::ostream & out,
169  const ColorBrightnessColorMap<PValue,PDefaultColor> & object )
170 {
171  object.selfDisplay( out );
172  return out;
173 }
174 
175 // //
176 ///////////////////////////////////////////////////////////////////////////////
177 
178 
179 ///////////////////////////////////////////////////////////////////////////////
180 // Interface - private :
181 
182 template <typename PValue, int PDefaultColor>
183 inline
184 void
185 DGtal::ColorBrightnessColorMap<PValue,PDefaultColor>::HSVtoRGB
186 ( double &r, double &g, double &b,
187  const double h, const double s, const double v)
188 {
189  int i;
190  double f, p, q, t;
191  if( s == 0 ) { // achromatic (gray)
192  r = g = b = v;
193  return;
194  }
195  i = static_cast<int>( floor( h / 60 ) );
196  f = ( h / 60 ) - i; // factorial part of h
197  p = v * ( 1 - s );
198  q = v * ( 1 - s * f );
199  t = v * ( 1 - s * ( 1 - f ) );
200  switch( i ) {
201  case 0:
202  r = v; g = t; b = p;
203  break;
204  case 1:
205  r = q; g = v; b = p;
206  break;
207  case 2:
208  r = p; g = v; b = t;
209  break;
210  case 3:
211  r = p; g = q; b = v;
212  break;
213  case 4:
214  r = t; g = p; b = v;
215  break;
216  default: // case 5:
217  r = v; g = p; b = q;
218  break;
219  }
220 }
221 
222 template <typename PValue, int PDefaultColor>
223 inline
224 void
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 )
230 {
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;
235 
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);
240  if ( max == min ) {
241  h = 0;
242  s = 0;
243  return;
244  } else {
245  double diff = ( max - min ) / 255.0;
246  if ( max == r ) {
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 );
252  }
253  h *= 60.0;
254  if ( h < 0 ) h += 360;
255  s = diff / v;
256  }
257 }