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/>.
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
24 * Implementation of inline methods defined in Color.h
26 * This file is part of the DGtal library.
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
33 //////////////////////////////////////////////////////////////////////////////
35 //////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////////
40 // Implementation of inline methods //
43 ///////////////////////////////////////////////////////////////////////////////
44 // Implementation of inline functions and external operators //
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.
54 DGtal::operator<< ( std::ostream & out,
55 const Color & object )
57 object.selfDisplay ( out );
63 DGtal::operator*( const double coeff,
64 const Color & aColor )
71 DGtal::Color::setRGBi( const unsigned char aRedValue,
72 const unsigned char aGreenValue,
73 const unsigned char aBlueValue,
74 const unsigned char aAlphaValue ) {
76 myGreen = aGreenValue;
78 myAlpha = aAlphaValue;
84 DGtal::Color::red( const unsigned char aRedValue )
90 DGtal::Color::green( unsigned char aGreenValue )
92 myGreen = aGreenValue;
96 DGtal::Color::blue( unsigned char aBlueValue )
102 DGtal::Color::alpha( unsigned char aAlphaValue )
104 myAlpha = aAlphaValue;
109 DGtal::Color::red() const
116 DGtal::Color::green() const
123 DGtal::Color::blue() const
130 DGtal::Color::alpha() const
136 DGtal::Color::r() const
138 return ((double) myRed)/255.0;
143 DGtal::Color::g() const
145 return ((double) myGreen)/255.0;
150 DGtal::Color::b() const
152 return ((double) myBlue)/255.0;
157 DGtal::Color::a() const
159 return ((double) myAlpha)/255.0;
164 DGtal::Color::getRGB() const
166 return (((DGtal::uint32_t) myRed) << 16)
167 | (((DGtal::uint32_t) myGreen) << 8)
168 | ((DGtal::uint32_t) myBlue);
173 DGtal::Color::getRGBA() const
175 return (((DGtal::uint32_t) myRed) << 24)
176 | (((DGtal::uint32_t) myGreen) << 16)
177 | (((DGtal::uint32_t) myBlue)<< 8)
178 | ((DGtal::uint32_t) myAlpha);
183 DGtal::Color::valid() const
185 return (*this) != Color::None;
190 DGtal::Color::HSVtoRGB
191 ( double & r, double & g, double & b,
192 const double h, const double s, const double v)
196 if( s == 0 ) { // achromatic (gray)
200 i = static_cast<int>( floor( h / 60 ) );
201 f = ( h / 60 ) - i; // factorial part of h
203 q = v * ( 1.0 - s * f );
204 t = v * ( 1.0 - s * ( 1.0 - f ) );
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 )
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;
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);
249 double diff = ( max - min ) / 255.0;
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 );
258 if ( h < 0 ) h += 360;
264 ///////////////////////////////////////////////////////////////////////////////