DGtal  1.3.beta
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() = default;
75 
76  // ----------------------- Interface --------------------------------------
77  public:
78 
86  Color( const unsigned int aRgb,
87  unsigned char aAlpha = 255 );
88 
95  Color( const Color &aColor ) = default;
96 
105  Color( const unsigned char aRedValue,
106  const unsigned char aGreenValue,
107  const unsigned char aBlueValue,
108  const unsigned char aAlphaValue = 255 )
109  : myRed(aRedValue),myGreen(aGreenValue),myBlue(aBlueValue),myAlpha(aAlphaValue) { }
110 
111 
119  Color( unsigned char aGrayValue,
120  unsigned char aAlphaValue = 255 )
121  : myRed(aGrayValue),myGreen(aGrayValue), myBlue(aGrayValue), myAlpha(aAlphaValue) { }
122 
123 
129  Color( )
130  : myRed(0),myGreen(0),myBlue(0), myAlpha(255)
131  {
132  }
133 
134  Color& setRGBi( const unsigned char aRedValue,
135  const unsigned char aGreenValue,
136  const unsigned char aBlueValue,
137  const unsigned char aAlphaValue = 255);
138 
149  Color& setRGBA( DGtal::uint32_t aRGBA );
150 
151  void red( const unsigned char aRedValue );
152 
153  void green( const unsigned char aGreenValue );
154 
155  void blue( const unsigned char aBlueValue );
156 
157  void alpha( const unsigned char aAlphaValue );
158 
159  unsigned char red() const ;
160 
161  unsigned char green() const ;
162 
163  unsigned char blue() const ;
164 
165  unsigned char alpha() const ;
166 
167 
168  double r() const ;
169 
170  double g() const ;
171 
172  double b() const ;
173 
174  double a() const ;
175 
176 
182  DGtal::uint32_t getRGB() const;
183 
189  DGtal::uint32_t getRGBA() const;
190 
191 
192  bool valid() const;
193 
194 
199  void selfDisplay ( std::ostream & out ) const;
200 
205  bool isValid() const;
206 
207 
208 
209  Color & setRGBf( float red,
210  float green,
211  float blue,
212  float alpha = 1.0 );
213 
214  bool operator==( const Color & aColor ) const;
215 
216  bool operator!=( const Color & aColor ) const;
217 
218  bool operator<( const Color & aColor ) const;
219 
220  bool operator>( const Color & aColor ) const;
221 
222  bool operator<=( const Color & aColor ) const;
223 
224  bool operator>=( const Color & aColor ) const;
225 
226 
235  Color & operator+= ( const Color & v )
236  {
237  this->myRed = clamp((int)this->myRed + (int)v.myRed);
238  this->myBlue = clamp((int)this->myBlue + (int)v.myBlue);
239  this->myGreen = clamp((int)this->myGreen + (int)v.myGreen);
240 #ifdef COLOR_WITH_ALPHA_ARITH
241  this->myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
242 #endif
243  return *this;
244  }
245 
255  Color operator+ ( const Color & v ) const
256  {
257  Color c;
258  c.myRed = clamp((int)this->myRed + (int)v.myRed);
259  c.myBlue =clamp((int)this->myBlue + (int)v.myBlue);
260  c.myGreen = clamp((int)this->myGreen + (int)v.myGreen);
261 #ifdef COLOR_WITH_ALPHA_ARITH
262  c.myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
263 #else
264  c.myAlpha = this->myAlpha ;
265 #endif
266  return c;
267  }
268 
278  Color & operator-= ( const Color & v )
279  {
280  this->myRed = clamp((int)this->myRed - (int)v.myRed);
281  this->myBlue = clamp((int)this->myBlue - (int)v.myBlue);
282  this->myGreen = clamp((int)this->myGreen - (int)v.myGreen);
283 #ifdef COLOR_WITH_ALPHA_ARITH
284  this->myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
285 #endif
286  return *this;
287  }
288 
297  Color operator- ( const Color & v ) const
298  {
299  Color c;
300  c.myRed = clamp((int)this->myRed - (int)v.myRed);
301  c.myBlue = clamp((int)this->myBlue - (int)v.myBlue);
302  c.myGreen = clamp((int)this->myGreen - (int)v.myGreen);
303 #ifdef COLOR_WITH_ALPHA_ARITH
304  c.myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
305 #else
306  c.myAlpha = this->myAlpha ;
307 #endif
308  return c;
309  }
310 
321  Color &operator *= ( const double coeff)
322  {
323  this->myRed = clamp((double)this->myRed*coeff);
324  this->myBlue = clamp((double)this->myBlue*coeff);
325  this->myGreen = clamp((double)this->myGreen*coeff);
326 #ifdef COLOR_WITH_ALPHA_ARITH
327  this->myAlpha = clamp((double)this->myAlpha*coeff);
328 #endif
329  return *this;
330  }
331 
340  Color operator * ( const double coeff) const
341  {
342  Color c;
343  c.myRed = clamp((double)this->myRed*coeff);
344  c.myBlue = clamp((double)this->myBlue*coeff);
345  c.myGreen = clamp((double)this->myGreen*coeff);
346 #ifdef COLOR_WITH_ALPHA_ARITH
347  c.myAlpha = clamp((double)this->myAlpha*coeff);
348 #else
349  c.myAlpha = this->myAlpha;
350 #endif
351  return c;
352  }
353 
360  Color & operator= ( const Color & pv ) = default;
361 
362  void flushPostscript( std::ostream & ) const;
363 
364  std::string svg() const;
365 
374  std::string svgAlpha( const char * aPrefix ) const;
375 
376  std::string postscript() const;
377 
386  std::string tikz() const;
387 
388  static const Color None;
389  static const Color Black;
390  static const Color Gray;
391  static const Color White;
392  static const Color Red;
393  static const Color Green;
394  static const Color Lime;
395  static const Color Blue;
396  static const Color Cyan;
397  static const Color Magenta;
398  static const Color Yellow;
399  static const Color Silver;
400  static const Color Purple;
401  static const Color Navy;
402  static const Color Aqua;
403 
404  // ------------------------- Protected Datas ------------------------------
405  private:
406  // ------------------------- Private Datas --------------------------------
407  private:
408  unsigned char myRed;
409  unsigned char myGreen;
410  unsigned char myBlue;
411  unsigned char myAlpha;
412  // ------------------------- Hidden services ------------------------------
413  protected:
414 
415 
416  private:
417 
425  unsigned char clamp(const double value) const
426  {
427  return static_cast<unsigned char>(std::max( std::min(value, 255.0), 0.0));
428  }
429 
430 
431 
432  // ------------------------- Internals ------------------------------------
433  private:
434 
435  }; // end of class Color
436 
437 
438 
448  Color
449  operator*( const double coeff,
450  const Color &aColor );
451 
452 
453 
460  std::ostream&
461  operator<< ( std::ostream & out, const Color & aColor );
462 
463 
464 } // namespace DGtal
465 
466 
468 // Includes inline functions/methods
469 #include "DGtal/io/Color.ih"
470 
471 
472 // //
474 
475 #endif // !defined Color_h
476 
477 #undef Color_RECURSES
478 #endif // else defined(Color_RECURSES)
DGtal::Color::operator<=
bool operator<=(const Color &aColor) const
Definition: Color.cpp:129
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:394
DGtal::Color::a
double a() const
max
int max(int a, int b)
Definition: testArithmeticalDSS.cpp:1108
DGtal::Color::operator>=
bool operator>=(const Color &aColor) const
Definition: Color.cpp:135
DGtal::Color::operator*=
Color & operator*=(const double coeff)
Definition: Color.h:321
DGtal::Color::svg
std::string svg() const
Definition: Color.cpp:158
DGtal::Color::red
unsigned char red() const
DGtal::Color::operator>
bool operator>(const Color &aColor) const
Definition: Color.cpp:123
DGtal::Color::Green
static const Color Green
Definition: Color.h:393
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
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:255
DGtal::Color::Yellow
static const Color Yellow
Definition: Color.h:398
DGtal::Color::myBlue
unsigned char myBlue
Definition: Color.h:410
DGtal::Color::blue
unsigned char blue() const
DGtal::Color::Cyan
static const Color Cyan
Definition: Color.h:396
DGtal::Color::myAlpha
unsigned char myAlpha
Definition: Color.h:411
DGtal::Color::operator<
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
DGtal::Color::setRGBi
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
DGtal::Color::operator==
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
DGtal::Color::flushPostscript
void flushPostscript(std::ostream &) const
Definition: Color.cpp:142
DGtal::Color::operator!=
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
DGtal::Color::Black
static const Color Black
Definition: Color.h:389
DGtal::Color::alpha
unsigned char alpha() const
DGtal::Color::Blue
static const Color Blue
Definition: Color.h:395
DGtal::Color::Gray
static const Color Gray
Definition: Color.h:390
DGtal::Color::Purple
static const Color Purple
Definition: Color.h:400
DGtal::Color::Navy
static const Color Navy
Definition: Color.h:401
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ATu0v1< TKSpace, TLinearAlgebra > &object)
DGtal::Color::Color
Color(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
Definition: Color.h:105
DGtal::Color::White
static const Color White
Definition: Color.h:391
DGtal::Color::Color
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition: Color.h:119
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:225
DGtal::Color::Red
static const Color Red
Definition: Color.h:392
DGtal::Color::clamp
unsigned char clamp(const double value) const
Definition: Color.h:425
DGtal::Color::~Color
~Color()=default
DGtal::Color::myRed
unsigned char myRed
Definition: Color.h:408
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::Color::operator*
Color operator*(const double coeff) const
Definition: Color.h:340
DGtal::Color::operator-=
Color & operator-=(const Color &v)
Definition: Color.h:278
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:167
DGtal::Color::operator+=
Color & operator+=(const Color &v)
Definition: Color.h:235
DGtal::Color::operator-
Color operator-(const Color &v) const
Definition: Color.h:297
DGtal::Color::isValid
bool isValid() const
Definition: Color.cpp:235
DGtal::Color::Silver
static const Color Silver
Definition: Color.h:399
DGtal::Color::getRGB
DGtal::uint32_t getRGB() const
DGtal::Color::tikz
std::string tikz() const
Definition: Color.cpp:176
DGtal::Color::Magenta
static const Color Magenta
Definition: Color.h:397
DGtal::Color::Color
Color()
Definition: Color.h:129
DGtal::Color::myGreen
unsigned char myGreen
Definition: Color.h:409
DGtal::Color::None
static const Color None
Definition: Color.h:388
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:402
DGtal::Color::operator=
Color & operator=(const Color &pv)=default
DGtal::Color::postscript
std::string postscript() const
Definition: Color.cpp:150