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//////////////////////////////////////////////////////////////////////////////
36//////////////////////////////////////////////////////////////////////////////
40///////////////////////////////////////////////////////////////////////////////
41// Implementation of inline methods //
44///////////////////////////////////////////////////////////////////////////////
45// Implementation of inline functions and external operators //
48 * Overloads 'operator<<' for displaying objects of class 'Color'.
49 * @param out the output stream where the object is written.
50 * @param object the object of class 'Color' to write.
51 * @return the output stream after the writing.
55DGtal::operator<< ( std::ostream & out,
56 const Color & object )
58 object.selfDisplay ( out );
64DGtal::operator*( const double coeff,
65 const Color & aColor )
72DGtal::Color::setRGBi( const unsigned char aRedValue,
73 const unsigned char aGreenValue,
74 const unsigned char aBlueValue,
75 const unsigned char aAlphaValue ) {
77 myGreen = aGreenValue;
79 myAlpha = aAlphaValue;
85DGtal::Color::red( const unsigned char aRedValue )
91DGtal::Color::green( unsigned char aGreenValue )
93 myGreen = aGreenValue;
97DGtal::Color::blue( unsigned char aBlueValue )
103DGtal::Color::alpha( unsigned char aAlphaValue )
105 myAlpha = aAlphaValue;
110DGtal::Color::red() const
117DGtal::Color::green() const
124DGtal::Color::blue() const
131DGtal::Color::alpha() const
137DGtal::Color::r() const
139 return ((double) myRed)/255.0;
144DGtal::Color::g() const
146 return ((double) myGreen)/255.0;
151DGtal::Color::b() const
153 return ((double) myBlue)/255.0;
158DGtal::Color::a() const
160 return ((double) myAlpha)/255.0;
165DGtal::Color::getRGB() const
167 return (((DGtal::uint32_t) myRed) << 16)
168 | (((DGtal::uint32_t) myGreen) << 8)
169 | ((DGtal::uint32_t) myBlue);
174DGtal::Color::getRGBA() const
176 return (((DGtal::uint32_t) myRed) << 24)
177 | (((DGtal::uint32_t) myGreen) << 16)
178 | (((DGtal::uint32_t) myBlue)<< 8)
179 | ((DGtal::uint32_t) myAlpha);
184DGtal::Color::valid() const
186 return (*this) != Color::None;
191DGtal::Color::HSVtoRGB
192( double & r, double & g, double & b,
193 const double h, const double s, const double v)
197 if( s == 0 ) { // achromatic (gray)
201 i = static_cast<int>( floor( h / 60 ) );
202 f = ( h / 60 ) - i; // factorial part of h
204 q = v * ( 1.0 - s * f );
205 t = v * ( 1.0 - s * ( 1.0 - f ) );
230DGtal::Color::RGBtoHSV
231( double &h, double &s, double &v,
232 const unsigned char r,
233 const unsigned char g,
234 const unsigned char b )
236 double min = (r<g) ? r : g;
237 if ( b < min ) min = b;
238 unsigned char max = (r>g) ? r : g;
239 if ( b > max ) max = b;
241 double dr = r / 255.0;
242 double dg = g / 255.0;
243 double db = b / 255.0;
244 v = max / 255.0; // (0.3*dr + 0.59*dg + 0.11*db);
250 double diff = ( max - min ) / 255.0;
252 h = (dg - db ) / diff;
253 } else if ( max == g ) {
254 h = 2.0 + ( ( db - dr ) / diff );
255 } else if ( max == b ) {
256 h = 4.0 + ( ( dr - dg ) / diff );
259 if ( h < 0 ) h += 360;
265///////////////////////////////////////////////////////////////////////////////
267///////////////////////////////////////////////////////////////////////////////
269///////////////////////////////////////////////////////////////////////////////
272///////////////////////////////////////////////////////////////////////////////
273// Standard services - public :
277DGtal::Color::Color( const unsigned int rgb, unsigned char aAlphaValue )
278 :myAlpha( aAlphaValue )
280 myRed = ( rgb & 0xFF0000u ) >> 16;
281 myGreen = ( rgb & 0xFF00u ) >> 8;
288DGtal::Color::setRGBA( const DGtal::uint32_t aRGBA )
290 myRed = ( aRGBA & 0xFF000000u ) >> 24;
291 myGreen = ( aRGBA & 0xFF0000u ) >> 16;
292 myBlue = ( aRGBA & 0xFF00u ) >> 8;
293 myAlpha = aRGBA & 0xFF;
300DGtal::Color::setRGBf( float aRedValue,
303 float aAlphaValue ) {
304 if ( aRedValue > 1.0f ) aRedValue = 1.0f;
305 if ( aRedValue < 0.0f ) aRedValue = 0.0f;
306 myRed = static_cast<unsigned char>( 255 * aRedValue );
307 if ( aGreenValue > 1.0f ) aGreenValue = 1.0f;
308 if ( aGreenValue < 0.0f ) aGreenValue = 0.0f;
309 myGreen = static_cast<unsigned char>( 255 * aGreenValue );
310 if ( aBlueValue > 1.0f ) aBlueValue = 1.0f;
311 if ( aBlueValue < 0.0f ) aBlueValue = 0.0f;
312 myBlue = static_cast<unsigned char>( 255 * aBlueValue );
313 if ( aAlphaValue > 1.0f ) aAlphaValue = 1.0f;
314 if ( aAlphaValue < 0.0f ) aAlphaValue = 0.0f;
315 myAlpha = static_cast<unsigned char>( 255 * aAlphaValue );
322DGtal::Color::operator==( const Color & aColor ) const
324 return myRed == aColor.myRed
325 && myGreen == aColor.myGreen
326 && myBlue == aColor.myBlue
327 && myAlpha == aColor.myAlpha;
333DGtal::Color::operator!=( const Color & aColor ) const
335 return myRed != aColor.myRed
336 || myGreen != aColor.myGreen
337 || myBlue != aColor.myBlue
338 || myAlpha != aColor.myAlpha;
344DGtal::Color::operator<( const Color & aColor ) const
346 if ( myRed < aColor.myRed )
348 if ( myRed == aColor.myRed ) {
349 if ( myGreen < aColor.myGreen )
351 if ( myGreen == aColor.myGreen ) {
352 if ( myBlue < aColor.myBlue )
354 if ( myBlue == aColor.myBlue )
355 return myAlpha < aColor.myAlpha;
364DGtal::Color::operator>( const Color & aColor ) const
366 return !this->operator<(aColor);
372DGtal::Color::operator<=( const Color & aColor ) const
374 return this->operator<(aColor) || this->operator==(aColor);
380DGtal::Color::operator>=( const Color & aColor ) const
382 return this->operator>(aColor) || this->operator==(aColor);
388DGtal::Color::flushPostscript( std::ostream & stream ) const
390 stream << ((double)myRed/255.0) << " "
391 << ((double)myGreen/255.0) << " "
392 << ((double)myBlue/255.0) << " srgb\n";
398DGtal::Color::postscript() const
401 secured_sprintf( buffer, 255, "%.4f %.4f %.4f", myRed/255.0, myGreen/255.0, myBlue/255.0 );
408DGtal::Color::svg() const
411 if ( *this == DGtal::Color::None ) return "none";
412 secured_sprintf( buffer, 255, "rgb(%d,%d,%d)",myRed, myGreen, myBlue );
419DGtal::Color::svgAlpha( const char * prefix ) const
422 if ( myAlpha == 255 || *this == DGtal::Color::None ) return "";
423 secured_sprintf( buffer, 255, " %s-opacity=\"%f\"", prefix, myAlpha/255.0f );
430DGtal::Color::tikz() const
432 // see tex/generic/pgf/utilities/pgfutil-plain.def for color definitions
434 if ( *this == DGtal::Color::None ) return "none";
435 if ( *this == DGtal::Color::Black ) return "black";
436 if ( *this == DGtal::Color::Gray ) return "gray";
437 if ( *this == DGtal::Color::White ) return "white";
438 if ( *this == DGtal::Color::Red ) return "red";
439 if ( *this == DGtal::Color::Green ) return "green!50!black";
440 if ( *this == DGtal::Color::Lime ) return "green";
441 if ( *this == DGtal::Color::Blue ) return "blue";
442// if ( *this == DGtal::Color::Cyan ) return "cyan";
443// if ( *this == DGtal::Color::Magenta ) return "magenta";
444// if ( *this == DGtal::Color::Yellow ) return "yellow";
445 if ( *this == DGtal::Color::Silver ) return "white!75!black";
446 if ( *this == DGtal::Color::Purple ) return "purple";
447 if ( *this == DGtal::Color::Navy ) return "blue!50!black";
448// if ( *this == DGtal::Color::Aqua ) return "cyan"; // ???: Is Color::Aqua meant to be equal to Color::Cyan?
449 secured_sprintf( buffer, 255, "{rgb,255:red,%d;green,%d;blue,%d}", myRed, myGreen, myBlue );
454///////////////////////////////////////////////////////////////////////////////
455// Interface - public :
458 * Writes/Displays the object on an output stream.
459 * @param out the output stream where the object is written.
463DGtal::Color::selfDisplay ( std::ostream & out ) const
465 out << "[Color] RGBA("<<(int)myRed<<","<<(int)myGreen<<","<<(int)myBlue<<","<<(int)myAlpha<<")";
469 * Checks the validity/consistency of the object.
470 * @return 'true' if the object is valid, 'false' otherwise.
474DGtal::Color::isValid() const
480///////////////////////////////////////////////////////////////////////////////
481// Interface - Constants :
483inline const DGtal::Color DGtal::Color::None(0,0,0,0);
484inline const DGtal::Color DGtal::Color::Black((unsigned char)0,(unsigned char)0,(unsigned char)0);
485inline const DGtal::Color DGtal::Color::Gray((unsigned char)128,(unsigned char)128,(unsigned char)128);
486inline const DGtal::Color DGtal::Color::White((unsigned char)255,(unsigned char)255,(unsigned char)255);
487inline const DGtal::Color DGtal::Color::Red((unsigned char)255,(unsigned char)0,(unsigned char)0);
488inline const DGtal::Color DGtal::Color::Green((unsigned char)0,(unsigned char)255,(unsigned char)0);
489inline const DGtal::Color DGtal::Color::Lime((unsigned char)0,(unsigned char)255,(unsigned char)0);
490inline const DGtal::Color DGtal::Color::Blue((unsigned char)0,(unsigned char)0,(unsigned char)255);
491inline const DGtal::Color DGtal::Color::Cyan((unsigned char)0,(unsigned char)255,(unsigned char)255);
492inline const DGtal::Color DGtal::Color::Magenta((unsigned char)255,(unsigned char)0,(unsigned char)255);
493inline const DGtal::Color DGtal::Color::Yellow((unsigned char)255,(unsigned char)255,(unsigned char)0);
494inline const DGtal::Color DGtal::Color::Silver((unsigned char)190,(unsigned char)190,(unsigned char)190);
495inline const DGtal::Color DGtal::Color::Purple((unsigned char)128,(unsigned char)0,(unsigned char)128);
496inline const DGtal::Color DGtal::Color::Navy((unsigned char)0,(unsigned char)0,(unsigned char)128);
497inline const DGtal::Color DGtal::Color::Aqua((unsigned char)0,(unsigned char)255,(unsigned char)255);
500///////////////////////////////////////////////////////////////////////////////
503///////////////////////////////////////////////////////////////////////////////
504// Internals - private :
507///////////////////////////////////////////////////////////////////////////////