DGtal  1.3.beta
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  DGtal::Color::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  DGtal::Color::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