DGtal  1.5.beta
Color.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 Color.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/07/17
23  *
24  * Implementation of inline methods defined in Color.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 //////////////////////////////////////////////////////////////////////////////
36 
37 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // Implementation of inline methods //
41 
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // Implementation of inline functions and external operators //
45 
46 /**
47  * Overloads 'operator<<' for displaying objects of class 'Color'.
48  * @param out the output stream where the object is written.
49  * @param object the object of class 'Color' to write.
50  * @return the output stream after the writing.
51  */
52 inline
53 std::ostream&
54 DGtal::operator<< ( std::ostream & out,
55  const Color & object )
56 {
57  object.selfDisplay ( out );
58  return out;
59 }
60 
61 inline
62 DGtal::Color
63 DGtal::operator*( const double coeff,
64  const Color & aColor )
65  {
66  Color c=aColor*coeff;
67  return c;
68  }
69 
70 inline DGtal::Color &
71 DGtal::Color::setRGBi( const unsigned char aRedValue,
72  const unsigned char aGreenValue,
73  const unsigned char aBlueValue,
74  const unsigned char aAlphaValue ) {
75  myRed = aRedValue;
76  myGreen = aGreenValue;
77  myBlue = aBlueValue;
78  myAlpha = aAlphaValue;
79  return *this;
80 }
81 
82 
83 inline void
84 DGtal::Color::red( const unsigned char aRedValue )
85 {
86  myRed = aRedValue;
87 }
88 
89 inline void
90 DGtal::Color::green( unsigned char aGreenValue )
91 {
92  myGreen = aGreenValue;
93 }
94 
95 inline void
96 DGtal::Color::blue( unsigned char aBlueValue )
97 {
98  myBlue = aBlueValue;
99 }
100 
101 inline void
102 DGtal::Color::alpha( unsigned char aAlphaValue )
103 {
104  myAlpha = aAlphaValue;
105 }
106 
107 inline
108 unsigned char
109 DGtal::Color::red() const
110 {
111  return myRed;
112 }
113 
114 inline
115 unsigned char
116 DGtal::Color::green() const
117 {
118  return myGreen;
119 }
120 
121 inline
122 unsigned char
123 DGtal::Color::blue() const
124 {
125  return myBlue;
126 }
127 
128 inline
129 unsigned char
130 DGtal::Color::alpha() const
131 {
132  return myAlpha;
133 }
134 inline
135 double
136 DGtal::Color::r() const
137 {
138  return ((double) myRed)/255.0;
139 }
140 
141 inline
142 double
143 DGtal::Color::g() const
144 {
145  return ((double) myGreen)/255.0;
146 }
147 
148 inline
149 double
150 DGtal::Color::b() const
151 {
152  return ((double) myBlue)/255.0;
153 }
154 
155 inline
156 double
157 DGtal::Color::a() const
158 {
159  return ((double) myAlpha)/255.0;
160 }
161 
162 inline
163 DGtal::uint32_t
164 DGtal::Color::getRGB() const
165 {
166  return (((DGtal::uint32_t) myRed) << 16)
167  | (((DGtal::uint32_t) myGreen) << 8)
168  | ((DGtal::uint32_t) myBlue);
169 }
170 
171 inline
172 DGtal::uint32_t
173 DGtal::Color::getRGBA() const
174 {
175  return (((DGtal::uint32_t) myRed) << 24)
176  | (((DGtal::uint32_t) myGreen) << 16)
177  | (((DGtal::uint32_t) myBlue)<< 8)
178  | ((DGtal::uint32_t) myAlpha);
179 }
180 
181 inline
182 bool
183 DGtal::Color::valid() const
184 {
185  return (*this) != Color::None;
186 }
187 
188 inline
189 void
190 DGtal::Color::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 inline
228 void
229 DGtal::Color::RGBtoHSV
230 ( double &h, double &s, double &v,
231  const unsigned char r,
232  const unsigned char g,
233  const unsigned char b )
234 {
235  double min = (r<g) ? r : g;
236  if ( b < min ) min = b;
237  unsigned char max = (r>g) ? r : g;
238  if ( b > max ) max = b;
239 
240  double dr = r / 255.0;
241  double dg = g / 255.0;
242  double db = b / 255.0;
243  v = max / 255.0; // (0.3*dr + 0.59*dg + 0.11*db);
244  if ( max == min ) {
245  h = 0;
246  s = 0;
247  return;
248  } else {
249  double diff = ( max - min ) / 255.0;
250  if ( max == r ) {
251  h = (dg - db ) / diff;
252  } else if ( max == g ) {
253  h = 2.0 + ( ( db - dr ) / diff );
254  } else if ( max == b ) {
255  h = 4.0 + ( ( dr - dg ) / diff );
256  }
257  h *= 60.0;
258  if ( h < 0 ) h += 360;
259  s = diff / v;
260  }
261 }
262 
263 // //
264 ///////////////////////////////////////////////////////////////////////////////
265 
266