DGtal  0.9.2
HueShadeColorMap.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 HueShadeColorMap.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 HueShadeColorMap.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 Value, int DefaultCycles>
43 inline
44 DGtal::HueShadeColorMap<Value,DefaultCycles>::HueShadeColorMap
45 ( const Value & amin,
46  const Value & amax,
47  const unsigned int cycles )
48  : myMin( amin ), myMax( amax ), myCycles( cycles )
49 {
50  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
51 }
52 
53 
54 template <typename Value, int DefaultCycles>
55 inline
56 DGtal::HueShadeColorMap<Value,DefaultCycles>::HueShadeColorMap
57 ( const HueShadeColorMap<Value,DefaultCycles> & other )
58  : myMin( other.myMin ), myMax( other.myMax ), myCycles( other.myCycles )
59 {
60  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
61 }
62 
63 template <typename Value, int DefaultCycles>
64 inline
65 DGtal::HueShadeColorMap<Value,DefaultCycles>::~HueShadeColorMap()
66 {
67 }
68 
69 template <typename Value, int DefaultCycles>
70 DGtal::HueShadeColorMap<Value,DefaultCycles> &
71 DGtal::HueShadeColorMap<Value,DefaultCycles>::operator=
72 ( const HueShadeColorMap<Value,DefaultCycles> & other )
73 {
74  if ( &other != this ) {
75  myMin = other.myMin;
76  myMax = other.myMax;
77  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
78  myCycles = other.myCycles;
79  }
80  return *this;
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////////
84 // Interface - public :
85 
86 template<typename Value, int DefaultCycles>
87 inline
88 const Value &
89 DGtal::HueShadeColorMap<Value,DefaultCycles>::min() const
90 {
91  return myMin;
92 }
93 
94 template<typename Value, int DefaultCycles>
95 inline
96 const Value &
97 DGtal::HueShadeColorMap<Value,DefaultCycles>::max() const
98 {
99  return myMax;
100 }
101 
102 template<typename Value, int DefaultCycles>
103 inline
104 void
105 DGtal::HueShadeColorMap<Value,DefaultCycles>::setCycles( int cycles )
106 {
107  myCycles = cycles;
108 }
109 
110 
111 template<typename Value, int DefaultCycles>
112 inline
113 DGtal::Color
114 DGtal::HueShadeColorMap<Value,DefaultCycles>::operator()
115  ( const Value & value ) const
116 {
117  return getColor( myCycles, myMin, myMax, value );
118 }
119 
120 /**
121  * Writes/Displays the object on an output stream.
122  * @param out the output stream where the object is written.
123  */
124 template <typename Value, int DefaultCycles>
125 inline
126 void
127 DGtal::HueShadeColorMap<Value,DefaultCycles>::selfDisplay ( std::ostream & out ) const
128 {
129  out << "[HueShadeColorMap "
130  << " min=" << myMin
131  << " max=" << myMax
132  << " cycles=" << myCycles
133  << " ]";
134 }
135 
136 /**
137  * Checks the validity/consistency of the object.
138  * @return 'true' if the object is valid, 'false' otherwise.
139  */
140 template <typename Value, int DefaultCycles>
141 inline
142 bool
143 DGtal::HueShadeColorMap<Value,DefaultCycles>::isValid() const
144 {
145  return true;
146 }
147 
148 template <typename Value, int DefaultCycles>
149 inline
150 DGtal::Color
151 DGtal::HueShadeColorMap<Value,DefaultCycles>::getColor
152 ( const unsigned int cycles,
153  const Value & min,
154  const Value & max,
155  const Value & value )
156 {
157  ASSERT_MSG(min < max, "Max should be strictly greather than Min in a colormap.");
158  const double scale = ( value - min ) / static_cast<double>( max - min );
159  const double hue = 360 * ( scale * cycles - floor(scale * cycles));
160  double red, green, blue;
161  HueShadeColorMap<Value,DefaultCycles>::HSVtoRGB( red, green, blue, hue, 0.9, 1.0 );
162  return Color( static_cast<int>( red * 255),
163  static_cast<int>( green * 255),
164  static_cast<int>( blue * 255) );
165 }
166 
167 ///////////////////////////////////////////////////////////////////////////////
168 // Implementation of inline functions //
169 
170 template <typename Value, int DefaultCycles>
171 inline
172 std::ostream&
173 DGtal::operator<< ( std::ostream & out,
174  const HueShadeColorMap<Value,DefaultCycles> & object )
175 {
176  object.selfDisplay( out );
177  return out;
178 }
179 
180 // //
181 ///////////////////////////////////////////////////////////////////////////////
182 
183 
184 ///////////////////////////////////////////////////////////////////////////////
185 // Interface - private :
186 
187 template <typename Value, int DefaultCycles>
188 inline
189 void
190 DGtal::HueShadeColorMap<Value,DefaultCycles>::HSVtoRGB
191 ( double & r, double & g, double & b,
192  const double h, const double s, const double v)
193 {
194  int i;
195  double f, p, q, t;
196  if( s == 0 ) { // achromatic (gray)
197  r = g = b = v;
198  return;
199  }
200  i = static_cast<int>( floor( h / 60 ) );
201  f = ( h / 60 ) - i; // factorial part of h
202  p = v * ( 1.0 - s );
203  q = v * ( 1.0 - s * f );
204  t = v * ( 1.0 - s * ( 1.0 - f ) );
205  switch( i ) {
206  case 0:
207  r = v; g = t; b = p;
208  break;
209  case 1:
210  r = q; g = v; b = p;
211  break;
212  case 2:
213  r = p; g = v; b = t;
214  break;
215  case 3:
216  r = p; g = q; b = v;
217  break;
218  case 4:
219  r = t; g = p; b = v;
220  break;
221  default: // case 5:
222  r = v; g = p; b = q;
223  break;
224  }
225 }
226 
227