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 GradientColorMap.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 GradientColorMap.h
26 * This file is part of the DGtal library.
29//////////////////////////////////////////////////////////////////////////////
33//////////////////////////////////////////////////////////////////////////////
35///////////////////////////////////////////////////////////////////////////////
36// IMPLEMENTATION of inline methods.
37///////////////////////////////////////////////////////////////////////////////
39///////////////////////////////////////////////////////////////////////////////
40// ----------------------- Standard services ------------------------------
42template < typename PValue,
44 int PDefaultFirstColor,
45 int PDefaultLastColor >
47DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::GradientColorMap
50 const ColorGradientPreset preset,
51 const Color firstColor,
52 const Color lastColor )
53 : myMin( minV ), myMax( maxV )
55 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
58 myColors.push_back( Color::Black );
59 myColors.push_back( Color::White );
62 myColors.push_back( Color( 255, 0, 255 ));
63 myColors.push_back( Color( 255, 255, 0 ));
66 myColors.push_back( Color( 0, 132, 100 ));
67 myColors.push_back( Color( 255, 255, 100 ));
70 myColors.push_back( Color( 255, 0, 0 ));
71 myColors.push_back( Color( 255, 255, 0));
74 myColors.push_back( Color( 0, 0, 255 ));
75 myColors.push_back( Color( 0, 255, 132 ));
78 myColors.push_back( Color( 0, 255, 255 ));
79 myColors.push_back( Color( 255, 0, 255 ));
82 myColors.push_back( Color( 0, 0, 0 ));
83 myColors.push_back( Color( 255, 198, 123 ));
86 myColors.push_back( Color::Black );
87 myColors.push_back( Color::Red );
88 myColors.push_back( Color( 255, 140, 0 ) );
89 myColors.push_back( Color::Yellow );
90 myColors.push_back( Color::White );
93 myColors.push_back( Color::Blue );
94 myColors.push_back( Color::Cyan );
95 myColors.push_back( Color::Yellow );
96 myColors.push_back( Color( 255, 140, 0 ) );
97 myColors.push_back( Color::Red );
98 myColors.push_back( Color( 132, 0, 0 ) );
101 myColors.push_back( Color(63,17,81) );
102 myColors.push_back( Color(63,110,139) );
103 myColors.push_back( Color( 40, 175, 127 ));
104 myColors.push_back( Color( 184, 222, 41 ));
105 myColors.push_back( Color( 253, 232, 37 ));
109 if ( firstColor != Color::None
110 && lastColor != Color::None ) {
111 myColors.push_back( firstColor );
112 myColors.push_back( lastColor );
118template <typename PValue,
120 int PDefaultFirstColor,
121 int PDefaultLastColor >
123DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::GradientColorMap
124( const GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor> & other )
125 : myMin( other.myMin ), myMax( other.myMax ), myColors( other.myColors )
127 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
130template <typename PValue,
132 int PDefaultFirstColor,
133 int PDefaultLastColor >
135DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::~GradientColorMap()
139template <typename PValue,
141 int PDefaultFirstColor,
142 int PDefaultLastColor >
143DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor> &
144DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::operator=
145( const DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor> & other )
147 if ( &other != this ) {
150 ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
151 myColors = other.myColors;
156///////////////////////////////////////////////////////////////////////////////
157// Interface - public :
160template<typename PValue,
162 int PDefaultFirstColor,
163 int PDefaultLastColor >
166DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::addColor
167( const Color & color )
169 myColors.push_back( color );
172template<typename PValue,
174 int PDefaultFirstColor,
175 int PDefaultLastColor >
178DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::clearColors()
183template<typename PValue,
185 int PDefaultFirstColor,
186 int PDefaultLastColor >
189DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::min() const
194template<typename PValue,
196 int PDefaultFirstColor,
197 int PDefaultLastColor >
200DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::max() const
205template<typename PValue,
207 int PDefaultFirstColor,
208 int PDefaultLastColor >
211DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::operator()
212 ( const Value & value ) const
214 return GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::getColor( myColors,
221 * Writes/Displays the object on an output stream.
222 * @param out the output stream where the object is written.
224template <typename PValue,
226 int PDefaultFirstColor,
227 int PDefaultLastColor >
230DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::selfDisplay
231( std::ostream & out ) const
233 out << "[GradientColorMap "
236 std::vector<Color>::const_iterator it = myColors.begin();
237 while ( it != myColors.end() ) {
240 << it->green() << ","
241 << it->blue() << ") ";
248 * Checks the validity/consistency of the object.
249 * @return 'true' if the object is valid, 'false' otherwise.
251template <typename PValue,
253 int PDefaultFirstColor,
254 int PDefaultLastColor >
257DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::isValid() const
262template <typename PValue,
264 int PDefaultFirstColor,
265 int PDefaultLastColor >
268DGtal::GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor>::getColor
269( const std::vector<Color> & colors,
274 ASSERT_MSG(min < max, "Max should be strictly greather than Min in a colormap.");
275 if ( colors.size() < 2 )
276 return DGtal::Color::None;
277 if ( std::is_floating_point<PValue>::value
278 && ! ( std::isfinite( value ) || std::isinf( value ) ) )
279 return DGtal::Color::Black;
280 if ( value < min ) value = min;
281 if ( max < value ) value = max;
282 double scale = static_cast<double>( value - min ) / (max - min);
283 const int intervals = (const int)colors.size() - 1;
284 int upper_index = static_cast<int>( ceil( intervals * scale ) );
285 if ( !upper_index ) // Special case when value == min.
287 const Color & firstColor = colors[ upper_index - 1 ];
288 const Color & lastColor = colors[ upper_index ];
289 scale = ( scale * intervals ) - (upper_index - 1);
291 const unsigned char red = static_cast<unsigned char>( firstColor.red() +
292 scale * ( lastColor.red() -
294 const unsigned char green = static_cast<unsigned char>( firstColor.green() +
295 scale * ( lastColor.green() -
296 firstColor.green() ));
297 const unsigned char blue = static_cast<unsigned char>( firstColor.blue() +
298 scale * ( lastColor.blue() -
299 firstColor.blue() ));
300 return DGtal::Color( red, green, blue );
303///////////////////////////////////////////////////////////////////////////////
304// Implementation of inline functions //
306template <typename PValue,
308 int PDefaultFirstColor,
309 int PDefaultLastColor >
312DGtal::operator<< ( std::ostream & out,
313 const GradientColorMap<PValue,PDefaultPreset,PDefaultFirstColor,PDefaultLastColor> & object )
315 object.selfDisplay( out );
320///////////////////////////////////////////////////////////////////////////////
323///////////////////////////////////////////////////////////////////////////////
324// Interface - private :