DGtal  1.4.beta
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  DGtal::Color::HSVtoRGB( red, green, blue, hue, 0.9, 1.0 );
162  return Color( static_cast<unsigned char>( red * 255),
163  static_cast<unsigned char>( green * 255),
164  static_cast<unsigned char>( 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 
188