DGtal  1.2.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)
36 #define Color_RECURSES
37 
38 #if !defined Color_h
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 
183 
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)
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
static const Color Purple
Definition: Color.h:400
Color(const Color &aColor)=default
Color & operator*=(const double coeff)
Definition: Color.h:321
unsigned char myRed
Definition: Color.h:408
double g() const
unsigned char blue() const
static const Color Aqua
Definition: Color.h:402
std::string postscript() const
Definition: Color.cpp:150
unsigned char red() const
Color operator+(const Color &v) const
Definition: Color.h:255
unsigned char myGreen
Definition: Color.h:409
static const Color Yellow
Definition: Color.h:398
bool operator>(const Color &aColor) const
Definition: Color.cpp:123
bool isValid() const
Definition: Color.cpp:235
void flushPostscript(std::ostream &) const
Definition: Color.cpp:142
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
Color & setRGBA(DGtal::uint32_t aRGBA)
Definition: Color.cpp:54
DGtal::uint32_t getRGB() const
bool valid() const
double b() const
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
Color & operator-=(const Color &v)
Definition: Color.h:278
Color operator-(const Color &v) const
Definition: Color.h:297
static const Color None
Definition: Color.h:388
static const Color Cyan
Definition: Color.h:396
void green(const unsigned char aGreenValue)
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
unsigned char myBlue
Definition: Color.h:410
Color & operator=(const Color &pv)=default
void selfDisplay(std::ostream &out) const
Definition: Color.cpp:225
static const Color Green
Definition: Color.h:393
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
std::string svg() const
Definition: Color.cpp:158
bool operator<=(const Color &aColor) const
Definition: Color.cpp:129
static const Color Navy
Definition: Color.h:401
Color operator*(const double coeff) const
Definition: Color.h:340
static const Color Gray
Definition: Color.h:390
std::string tikz() const
Definition: Color.cpp:176
Color & operator+=(const Color &v)
Definition: Color.h:235
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition: Color.h:119
static const Color Silver
Definition: Color.h:399
void red(const unsigned char aRedValue)
static const Color Red
Definition: Color.h:392
void alpha(const unsigned char aAlphaValue)
unsigned char clamp(const double value) const
Definition: Color.h:425
~Color()=default
static const Color Lime
Definition: Color.h:394
static const Color White
Definition: Color.h:391
unsigned char green() const
double r() const
static const Color Blue
Definition: Color.h:395
static const Color Black
Definition: Color.h:389
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition: Color.cpp:65
bool operator>=(const Color &aColor) const
Definition: Color.cpp:135
double a() const
std::string svgAlpha(const char *aPrefix) const
Definition: Color.cpp:167
unsigned char myAlpha
Definition: Color.h:411
unsigned char alpha() const
Color(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
Definition: Color.h:105
void blue(const unsigned char aBlueValue)
static const Color Magenta
Definition: Color.h:397
DGtal::uint32_t getRGBA() const
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
KForm< Calculus, order, duality > operator*(const typename Calculus::Scalar &scalar, const KForm< Calculus, order, duality > &form)
int max(int a, int b)