DGtal  1.1.0
Color.h
1 
17 #pragma once
18 
32 #if defined(Color_RECURSES)
33 #error Recursive header files inclusion detected in Color.h
34 #else // defined(Color_RECURSES)
35 
36 #define Color_RECURSES
37 
38 #if !defined Color_h
39 
40 #define Color_h
41 
43 // Inclusions
44 #include <iostream>
45 #include "DGtal/base/Common.h"
46 #include <boost/lexical_cast.hpp>
47 #include <algorithm>
49 
50 namespace DGtal
51 {
52 
54  // class Color
66  class Color
67  {
68  // ----------------------- Standard services ------------------------------
69  public:
70 
74  ~Color();
75 
76  // ----------------------- Interface --------------------------------------
77  public:
78 
86  Color( const unsigned int aRgb,
87  unsigned char aAlpha = 255 );
88 
95  Color( const Color &aColor ):
96  myRed(aColor.myRed),myGreen(aColor.myGreen),
97  myBlue(aColor.myBlue), myAlpha(aColor.myAlpha)
98  {}
99 
110  Color( const unsigned char aRedValue,
111  const unsigned char aGreenValue,
112  const unsigned char aBlueValue,
113  const unsigned char aAlphaValue = 255 )
114  : myRed(aRedValue),myGreen(aGreenValue),myBlue(aBlueValue),myAlpha(aAlphaValue) { }
115 
116 
124  Color( unsigned char aGrayValue,
125  unsigned char aAlphaValue = 255 )
126  : myRed(aGrayValue),myGreen(aGrayValue), myBlue(aGrayValue), myAlpha(aAlphaValue) { }
127 
128 
134  Color( )
135  : myRed(0),myGreen(0),myBlue(0), myAlpha(255)
136  {
137  }
138 
139  Color& setRGBi( const unsigned char aRedValue,
140  const unsigned char aGreenValue,
141  const unsigned char aBlueValue,
142  const unsigned char aAlphaValue );
143 
154  Color& setRGBA( DGtal::uint32_t aRGBA );
155 
156  void red( const unsigned char aRedValue );
157 
158  void green( const unsigned char aGreenValue );
159 
160  void blue( const unsigned char aBlueValue );
161 
162  void alpha( const unsigned char aAlphaValue );
163 
164  unsigned char red() const ;
165 
166  unsigned char green() const ;
167 
168  unsigned char blue() const ;
169 
170  unsigned char alpha() const ;
171 
172 
173  double r() const ;
174 
175  double g() const ;
176 
177  double b() const ;
178 
179  double a() const ;
180 
181 
188 
195 
196 
197  bool valid() const;
198 
199 
204  void selfDisplay ( std::ostream & out ) const;
205 
210  bool isValid() const;
211 
212 
213 
214  Color & setRGBf( float red,
215  float green,
216  float blue,
217  float alpha = 1.0 );
218 
219  bool operator==( const Color & aColor ) const;
220 
221  bool operator!=( const Color & aColor ) const;
222 
223  bool operator<( const Color & aColor ) const;
224 
225 
234  Color & operator+= ( const Color & v )
235  {
236  this->myRed = clamp((int)this->myRed + (int)v.myRed);
237  this->myBlue = clamp((int)this->myBlue + (int)v.myBlue);
238  this->myGreen = clamp((int)this->myGreen + (int)v.myGreen);
239 #ifdef COLOR_WITH_ALPHA_ARITH
240  this->myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
241 #endif
242  return *this;
243  }
244 
254  Color operator+ ( const Color & v ) const
255  {
256  Color c;
257  c.myRed = clamp((int)this->myRed + (int)v.myRed);
258  c.myBlue =clamp((int)this->myBlue + (int)v.myBlue);
259  c.myGreen = clamp((int)this->myGreen + (int)v.myGreen);
260 #ifdef COLOR_WITH_ALPHA_ARITH
261  c.myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
262 #else
263  c.myAlpha = this->myAlpha ;
264 #endif
265  return c;
266  }
267 
277  Color & operator-= ( const Color & v )
278  {
279  this->myRed = clamp((int)this->myRed - (int)v.myRed);
280  this->myBlue = clamp((int)this->myBlue - (int)v.myBlue);
281  this->myGreen = clamp((int)this->myGreen - (int)v.myGreen);
282 #ifdef COLOR_WITH_ALPHA_ARITH
283  this->myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
284 #endif
285  return *this;
286  }
287 
296  Color operator- ( const Color & v ) const
297  {
298  Color c;
299  c.myRed = clamp((int)this->myRed - (int)v.myRed);
300  c.myBlue = clamp((int)this->myBlue - (int)v.myBlue);
301  c.myGreen = clamp((int)this->myGreen - (int)v.myGreen);
302 #ifdef COLOR_WITH_ALPHA_ARITH
303  c.myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
304 #else
305  c.myAlpha = this->myAlpha ;
306 #endif
307  return c;
308  }
309 
320  Color &operator *= ( const double coeff)
321  {
322  this->myRed = clamp((double)this->myRed*coeff);
323  this->myBlue = clamp((double)this->myBlue*coeff);
324  this->myGreen = clamp((double)this->myGreen*coeff);
325 #ifdef COLOR_WITH_ALPHA_ARITH
326  this->myAlpha = clamp((double)this->myAlpha*coeff);
327 #endif
328  return *this;
329  }
330 
339  Color operator * ( const double coeff) const
340  {
341  Color c;
342  c.myRed = clamp((double)this->myRed*coeff);
343  c.myBlue = clamp((double)this->myBlue*coeff);
344  c.myGreen = clamp((double)this->myGreen*coeff);
345 #ifdef COLOR_WITH_ALPHA_ARITH
346  c.myAlpha = clamp((double)this->myAlpha*coeff);
347 #else
348  c.myAlpha = this->myAlpha;
349 #endif
350  return c;
351  }
352 
359  Color & operator= ( const Color & pv )
360  {
361  this->myRed = pv.myRed;
362  this->myGreen = pv.myGreen;
363  this->myBlue = pv.myBlue;
364  this->myAlpha = pv.myAlpha;
365  return *this;
366  }
367 
368 
369 
370  void flushPostscript( std::ostream & ) const;
371 
372  std::string svg() const;
373 
382  std::string svgAlpha( const char * aPrefix ) const;
383 
384  std::string postscript() const;
385 
394  std::string tikz() const;
395 
396  static const Color None;
397  static const Color Black;
398  static const Color Gray;
399  static const Color White;
400  static const Color Red;
401  static const Color Green;
402  static const Color Lime;
403  static const Color Blue;
404  static const Color Cyan;
405  static const Color Magenta;
406  static const Color Yellow;
407  static const Color Silver;
408  static const Color Purple;
409  static const Color Navy;
410  static const Color Aqua;
411 
412  // ------------------------- Protected Datas ------------------------------
413  private:
414  // ------------------------- Private Datas --------------------------------
415  private:
416  unsigned char myRed;
417  unsigned char myGreen;
418  unsigned char myBlue;
419  unsigned char myAlpha;
420  // ------------------------- Hidden services ------------------------------
421  protected:
422 
423 
424  private:
425 
433  unsigned char clamp(const double value) const
434  {
435  return static_cast<unsigned char>(std::max( std::min(value, 255.0), 0.0));
436  }
437 
438 
439 
440  // ------------------------- Internals ------------------------------------
441  private:
442 
443  }; // end of class Color
444 
445 
446 
456  Color
457  operator*( const double coeff,
458  const Color &aColor );
459 
460 
461 
468  std::ostream&
469  operator<< ( std::ostream & out, const Color & aColor );
470 
471 
472 } // namespace DGtal
473 
474 
476 // Includes inline functions/methods
477 #include "DGtal/io/Color.ih"
478 
479 
480 // //
482 
483 #endif // !defined Color_h
484 
485 #undef Color_RECURSES
486 #endif // else defined(Color_RECURSES)
DGtal::Color::Color
Color(const Color &aColor)
Definition: Color.h:95
DGtal::Color::green
unsigned char green() const
DGtal::uint32_t
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
DGtal::Color::Lime
static const Color Lime
Definition: Color.h:402
DGtal::Color::a
double a() const
max
int max(int a, int b)
Definition: testArithmeticalDSS.cpp:1108
DGtal::Color::operator*=
Color & operator*=(const double coeff)
Definition: Color.h:320
DGtal::Color::svg
std::string svg() const
Definition: Color.cpp:140
DGtal::Color::red
unsigned char red() const
DGtal::Color::Green
static const Color Green
Definition: Color.h:401
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
DGtal::Color::setRGBA
Color & setRGBA(DGtal::uint32_t aRGBA)
Definition: Color.cpp:54
DGtal::Color::operator+
Color operator+(const Color &v) const
Definition: Color.h:254
DGtal::Color::Yellow
static const Color Yellow
Definition: Color.h:406
DGtal::Color::myBlue
unsigned char myBlue
Definition: Color.h:418
DGtal::Color::blue
unsigned char blue() const
DGtal::Color::Cyan
static const Color Cyan
Definition: Color.h:404
DGtal::Color::myAlpha
unsigned char myAlpha
Definition: Color.h:419
DGtal::Color::operator<
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
DGtal::Color::operator==
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
DGtal::Color::flushPostscript
void flushPostscript(std::ostream &) const
Definition: Color.cpp:124
DGtal::Color::operator!=
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
DGtal::Color::Black
static const Color Black
Definition: Color.h:397
DGtal::Color::alpha
unsigned char alpha() const
DGtal::Color::Blue
static const Color Blue
Definition: Color.h:403
DGtal::Color::Gray
static const Color Gray
Definition: Color.h:398
DGtal::Color::~Color
~Color()
Definition: Color.cpp:186
DGtal::Color::Purple
static const Color Purple
Definition: Color.h:408
DGtal::Color::Navy
static const Color Navy
Definition: Color.h:409
DGtal::Color::Color
Color(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
Definition: Color.h:110
DGtal::Color::White
static const Color White
Definition: Color.h:399
DGtal::Color::Color
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition: Color.h:124
DGtal::Color::red
void red(const unsigned char aRedValue)
DGtal::Color::setRGBf
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition: Color.cpp:65
DGtal::Color::selfDisplay
void selfDisplay(std::ostream &out) const
Definition: Color.cpp:217
DGtal::Color::Red
static const Color Red
Definition: Color.h:400
DGtal::Color::clamp
unsigned char clamp(const double value) const
Definition: Color.h:433
DGtal::Color::myRed
unsigned char myRed
Definition: Color.h:416
DGtal::Color::setRGBi
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue)
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
DGtal::Color::operator*
Color operator*(const double coeff) const
Definition: Color.h:339
DGtal::Color::operator-=
Color & operator-=(const Color &v)
Definition: Color.h:277
DGtal::Color::g
double g() const
DGtal::Color::r
double r() const
DGtal::Color::valid
bool valid() const
DGtal::Color::getRGBA
DGtal::uint32_t getRGBA() const
DGtal::Color::svgAlpha
std::string svgAlpha(const char *aPrefix) const
Definition: Color.cpp:149
DGtal::Color::operator+=
Color & operator+=(const Color &v)
Definition: Color.h:234
DGtal::Color::operator-
Color operator-(const Color &v) const
Definition: Color.h:296
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::Color::isValid
bool isValid() const
Definition: Color.cpp:227
DGtal::Color::Silver
static const Color Silver
Definition: Color.h:407
DGtal::Color::getRGB
DGtal::uint32_t getRGB() const
DGtal::Color::tikz
std::string tikz() const
Definition: Color.cpp:158
DGtal::Color::green
void green(const unsigned char aGreenValue)
DGtal::Color::Magenta
static const Color Magenta
Definition: Color.h:405
DGtal::Color::blue
void blue(const unsigned char aBlueValue)
DGtal::Color::Color
Color()
Definition: Color.h:134
DGtal::Color::myGreen
unsigned char myGreen
Definition: Color.h:417
DGtal::Color::None
static const Color None
Definition: Color.h:396
DGtal::operator*
KForm< Calculus, order, duality > operator*(const typename Calculus::Scalar &scalar, const KForm< Calculus, order, duality > &form)
DGtal::Color::b
double b() const
DGtal::Color::Aqua
static const Color Aqua
Definition: Color.h:410
DGtal::Color::operator=
Color & operator=(const Color &pv)
Definition: Color.h:359
DGtal::Color::alpha
void alpha(const unsigned char aAlphaValue)
DGtal::Color::postscript
std::string postscript() const
Definition: Color.cpp:132