DGtal 1.4.0
Loading...
Searching...
No Matches
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 */
52inline
53std::ostream&
54DGtal::operator<< ( std::ostream & out,
55 const Color & object )
56{
57 object.selfDisplay ( out );
58 return out;
59}
60
61inline
62DGtal::Color
63DGtal::operator*( const double coeff,
64 const Color & aColor )
65 {
66 Color c=aColor*coeff;
67 return c;
68 }
69
70inline DGtal::Color &
71DGtal::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
83inline void
84DGtal::Color::red( const unsigned char aRedValue )
85{
86 myRed = aRedValue;
87}
88
89inline void
90DGtal::Color::green( unsigned char aGreenValue )
91{
92 myGreen = aGreenValue;
93}
94
95inline void
96DGtal::Color::blue( unsigned char aBlueValue )
97{
98 myBlue = aBlueValue;
99}
100
101inline void
102DGtal::Color::alpha( unsigned char aAlphaValue )
103{
104 myAlpha = aAlphaValue;
105}
106
107inline
108unsigned char
109DGtal::Color::red() const
110{
111 return myRed;
112}
113
114inline
115unsigned char
116DGtal::Color::green() const
117{
118 return myGreen;
119}
120
121inline
122unsigned char
123DGtal::Color::blue() const
124{
125 return myBlue;
126}
127
128inline
129unsigned char
130DGtal::Color::alpha() const
131{
132 return myAlpha;
133}
134inline
135double
136DGtal::Color::r() const
137{
138 return ((double) myRed)/255.0;
139}
140
141inline
142double
143DGtal::Color::g() const
144{
145 return ((double) myGreen)/255.0;
146}
147
148inline
149double
150DGtal::Color::b() const
151{
152 return ((double) myBlue)/255.0;
153}
154
155inline
156double
157DGtal::Color::a() const
158{
159 return ((double) myAlpha)/255.0;
160}
161
162inline
163DGtal::uint32_t
164DGtal::Color::getRGB() const
165{
166 return (((DGtal::uint32_t) myRed) << 16)
167 | (((DGtal::uint32_t) myGreen) << 8)
168 | ((DGtal::uint32_t) myBlue);
169}
170
171inline
172DGtal::uint32_t
173DGtal::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
181inline
182bool
183DGtal::Color::valid() const
184{
185 return (*this) != Color::None;
186}
187
188inline
189void
190DGtal::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
227inline
228void
229DGtal::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