DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GradientColorMap.ih
1 
29 
30 #include <cstdlib>
31 #include <vector>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 template < typename PValue,
42  int PDefaultPreset,
43  int PDefaultFirstColor,
44  int PDefaultLastColor >
45 inline
47 ( const PValue & minV,
48  const PValue & maxV,
49  const ColorGradientPreset preset,
50  const Color firstColor,
51  const Color lastColor )
52  : myMin( minV ), myMax( maxV )
53 {
54  switch ( preset ) {
55  case CMAP_GRAYSCALE:
56  myColors.push_back( Color::Black );
57  myColors.push_back( Color::White );
58  break;
59  case CMAP_SPRING:
60  myColors.push_back( Color( 255, 0, 255 ));
61  myColors.push_back( Color( 255, 255, 0 ));
62  break;
63  case CMAP_SUMMER:
64  myColors.push_back( Color( 0, 132, 100 ));
65  myColors.push_back( Color( 255, 255, 100 ));
66  break;
67  case CMAP_AUTUMN:
68  myColors.push_back( Color( 255, 0, 0 ));
69  myColors.push_back( Color( 255, 255, 0));
70  break;
71  case CMAP_WINTER:
72  myColors.push_back( Color( 0, 0, 255 ));
73  myColors.push_back( Color( 0, 255, 132 ));
74  break;
75  case CMAP_COOL:
76  myColors.push_back( Color( 0, 255, 255 ));
77  myColors.push_back( Color( 255, 0, 255 ));
78  break;
79  case CMAP_COPPER:
80  myColors.push_back( Color( 0, 0, 0 ));
81  myColors.push_back( Color( 255, 198, 123 ));
82  break;
83  case CMAP_HOT:
84  myColors.push_back( Color::Black );
85  myColors.push_back( Color::Red );
86  myColors.push_back( Color( 255, 140, 0 ) );
87  myColors.push_back( Color::Yellow );
88  myColors.push_back( Color::White );
89  break;
90  case CMAP_JET:
91  myColors.push_back( Color::Blue );
92  myColors.push_back( Color::Cyan );
93  myColors.push_back( Color::Yellow );
94  myColors.push_back( Color( 255, 140, 0 ) );
95  myColors.push_back( Color::Red );
96  myColors.push_back( Color( 132, 0, 0 ) );
97  break;
98  case CMAP_CUSTOM:
99  if ( firstColor != Color::None
100  && lastColor != Color::None ) {
101  myColors.push_back( firstColor );
102  myColors.push_back( lastColor );
103  }
104  break;
105  }
106 }
107 
108 template <typename PValue,
109  int PDefaultPreset,
110  int PDefaultFirstColor,
111  int PDefaultLastColor >
112 inline
115  : myMin( other.myMin ), myMax( other.myMax ), myColors( other.myColors )
116 {
117 }
118 
119 template <typename PValue,
120  int PDefaultPreset,
121  int PDefaultFirstColor,
122  int PDefaultLastColor >
123 inline
125 {
126 }
127 
128 template <typename PValue,
129  int PDefaultPreset,
130  int PDefaultFirstColor,
131  int PDefaultLastColor >
135 {
136  if ( &other != this ) {
137  myMin = other.myMin;
138  myMax = other.myMax;
139  myColors = other.myColors;
140  }
141  return *this;
142 }
143 
145 // Interface - public :
146 
147 
148 template<typename PValue,
149  int PDefaultPreset,
150  int PDefaultFirstColor,
151  int PDefaultLastColor >
152 inline
153 void
155 ( const Color & color )
156 {
157  myColors.push_back( color );
158 }
159 
160 template<typename PValue,
161  int PDefaultPreset,
162  int PDefaultFirstColor,
163  int PDefaultLastColor >
164 inline
165 void
167 {
168  myColors.clear();
169 }
170 
171 template<typename PValue,
172  int PDefaultPreset,
173  int PDefaultFirstColor,
174  int PDefaultLastColor >
175 inline
176 const PValue &
178 {
179  return myMin;
180 }
181 
182 template<typename PValue,
183  int PDefaultPreset,
184  int PDefaultFirstColor,
185  int PDefaultLastColor >
186 inline
187 const PValue &
189 {
190  return myMax;
191 }
192 
193 template<typename PValue,
194  int PDefaultPreset,
195  int PDefaultFirstColor,
196  int PDefaultLastColor >
197 inline
198 Color
200  ( const Value & value ) const
201 {
203  myMin,
204  myMax,
205  value );
206 }
207 
212 template <typename PValue,
213  int PDefaultPreset,
214  int PDefaultFirstColor,
215  int PDefaultLastColor >
216 inline
217 void
219 ( std::ostream & out ) const
220 {
221  out << "[GradientColorMap "
222  << " min=" << myMin
223  << " max=" << myMax;
224  std::vector<Color>::iterator it = myColors.begin();
225  while ( it != myColors.end() ) {
226  out << " RGB("
227  << it->red() << ","
228  << it->green() << ","
229  << it->blue() << ") ";
230  ++it;
231  }
232  out << " ]";
233 }
234 
239 template <typename PValue,
240  int PDefaultPreset,
241  int PDefaultFirstColor,
242  int PDefaultLastColor >
243 inline
244 bool
246 {
247  return true;
248 }
249 
250 template <typename PValue,
251  int PDefaultPreset,
252  int PDefaultFirstColor,
253  int PDefaultLastColor >
254 inline
255 Color
257 ( const std::vector<Color> & colors,
258  const Value & min,
259  const Value & max,
260  const Value & value )
261 {
262  if ( colors.size() < 2 )
263  return Color::None;
264  double scale = static_cast<double>( value - min ) / (max - min);
265  const int intervals = (const int)colors.size() - 1;
266  int upper_index = static_cast<int>( ceil( intervals * scale ) );
267  if ( !upper_index ) // Special case when value == min.
268  upper_index = 1;
269  const Color & firstColor = colors[ upper_index - 1 ];
270  const Color & lastColor = colors[ upper_index ];
271  scale = ( scale * intervals ) - (upper_index - 1);
272 
273  const unsigned char red = static_cast<unsigned char>( firstColor.red() +
274  scale * ( lastColor.red() -
275  firstColor.red() ));
276  const unsigned char green = static_cast<unsigned char>( firstColor.green() +
277  scale * ( lastColor.green() -
278  firstColor.green() ));
279  const unsigned char blue = static_cast<unsigned char>( firstColor.blue() +
280  scale * ( lastColor.blue() -
281  firstColor.blue() ));
282  return Color( red, green, blue );
283 }
284 
286 // Implementation of inline functions //
287 
288 template <typename PValue,
289  int PDefaultPreset,
290  int PDefaultFirstColor,
291  int PDefaultLastColor >
292 inline
293 std::ostream&
294 DGtal::operator<< ( std::ostream & out,
296 {
297  object.selfDisplay( out );
298  return out;
299 }
300 
301 // //
303 
304 
306 // Interface - private :
307