DGtal  0.9.2
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 
178  DGtal::uint32_t getRGB() const;
179 
185  DGtal::uint32_t getRGBA() const;
186 
187 
188  bool valid() const;
189 
190 
195  void selfDisplay ( std::ostream & out ) const;
196 
201  bool isValid() const;
202 
203 
204 
205  Color & setRGBf( float red,
206  float green,
207  float blue,
208  float alpha = 1.0 );
209 
210  bool operator==( const Color & aColor ) const;
211 
212  bool operator!=( const Color & aColor ) const;
213 
214  bool operator<( const Color & aColor ) const;
215 
216 
225  Color & operator+= ( const Color & v )
226  {
227  this->myRed = clamp((int)this->myRed + (int)v.myRed);
228  this->myBlue = clamp((int)this->myBlue + (int)v.myBlue);
229  this->myGreen = clamp((int)this->myGreen + (int)v.myGreen);
230 #ifdef COLOR_WITH_ALPHA_ARITH
231  this->myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
232 #endif
233  return *this;
234  }
235 
245  Color operator+ ( const Color & v ) const
246  {
247  Color c;
248  c.myRed = clamp((int)this->myRed + (int)v.myRed);
249  c.myBlue =clamp((int)this->myBlue + (int)v.myBlue);
250  c.myGreen = clamp((int)this->myGreen + (int)v.myGreen);
251 #ifdef COLOR_WITH_ALPHA_ARITH
252  c.myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
253 #else
254  c.myAlpha = this->myAlpha ;
255 #endif
256  return c;
257  }
258 
268  Color & operator-= ( const Color & v )
269  {
270  this->myRed = clamp((int)this->myRed - (int)v.myRed);
271  this->myBlue = clamp((int)this->myBlue - (int)v.myBlue);
272  this->myGreen = clamp((int)this->myGreen - (int)v.myGreen);
273 #ifdef COLOR_WITH_ALPHA_ARITH
274  this->myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
275 #endif
276  return *this;
277  }
278 
287  Color operator- ( const Color & v ) const
288  {
289  Color c;
290  c.myRed = clamp((int)this->myRed - (int)v.myRed);
291  c.myBlue = clamp((int)this->myBlue - (int)v.myBlue);
292  c.myGreen = clamp((int)this->myGreen - (int)v.myGreen);
293 #ifdef COLOR_WITH_ALPHA_ARITH
294  c.myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
295 #else
296  c.myAlpha = this->myAlpha ;
297 #endif
298  return c;
299  }
300 
311  Color &operator *= ( const double coeff)
312  {
313  this->myRed = clamp((double)this->myRed*coeff);
314  this->myBlue = clamp((double)this->myBlue*coeff);
315  this->myGreen = clamp((double)this->myGreen*coeff);
316 #ifdef COLOR_WITH_ALPHA_ARITH
317  this->myAlpha = clamp((double)this->myAlpha*coeff);
318 #endif
319  return *this;
320  }
321 
330  Color operator * ( const double coeff) const
331  {
332  Color c;
333  c.myRed = clamp((double)this->myRed*coeff);
334  c.myBlue = clamp((double)this->myBlue*coeff);
335  c.myGreen = clamp((double)this->myGreen*coeff);
336 #ifdef COLOR_WITH_ALPHA_ARITH
337  c.myAlpha = clamp((double)this->myAlpha*coeff);
338 #else
339  c.myAlpha = this->myAlpha;
340 #endif
341  return c;
342  }
343 
350  Color & operator= ( const Color & pv )
351  {
352  this->myRed = pv.myRed;
353  this->myGreen = pv.myGreen;
354  this->myBlue = pv.myBlue;
355  this->myAlpha = pv.myAlpha;
356  return *this;
357  }
358 
359 
360 
361  void flushPostscript( std::ostream & ) const;
362 
363  std::string svg() const;
364 
373  std::string svgAlpha( const char * aPrefix ) const;
374 
375  std::string postscript() const;
376 
385  std::string tikz() const;
386 
387  static const Color None;
388  static const Color Black;
389  static const Color Gray;
390  static const Color White;
391  static const Color Red;
392  static const Color Green;
393  static const Color Lime;
394  static const Color Blue;
395  static const Color Cyan;
396  static const Color Magenta;
397  static const Color Yellow;
398  static const Color Silver;
399  static const Color Purple;
400  static const Color Navy;
401  static const Color Aqua;
402 
403  // ------------------------- Protected Datas ------------------------------
404  private:
405  // ------------------------- Private Datas --------------------------------
406  private:
407  unsigned char myRed;
408  unsigned char myGreen;
409  unsigned char myBlue;
410  unsigned char myAlpha;
411  // ------------------------- Hidden services ------------------------------
412  protected:
413 
414 
415  private:
416 
424  unsigned char clamp(const double value) const
425  {
426  return static_cast<unsigned char>(std::max( std::min(value, 255.0), 0.0));
427  }
428 
429 
430 
431  // ------------------------- Internals ------------------------------------
432  private:
433 
434  }; // end of class Color
435 
436 
437 
447  Color
448  operator*( const double coeff,
449  const Color &aColor );
450 
451 
452 
459  std::ostream&
460  operator<< ( std::ostream & out, const Color & aColor );
461 
462 
463 } // namespace DGtal
464 
465 
467 // Includes inline functions/methods
468 #include "DGtal/io/Color.ih"
469 
470 
471 // //
473 
474 #endif // !defined Color_h
475 
476 #undef Color_RECURSES
477 #endif // else defined(Color_RECURSES)
Color & operator+=(const Color &v)
Definition: Color.h:225
Color operator+(const Color &v) const
Definition: Color.h:245
static const Color Black
Definition: Color.h:388
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
unsigned char clamp(const double value) const
Definition: Color.h:424
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue)
Color & operator=(const Color &pv)
Definition: Color.h:350
static const Color Magenta
Definition: Color.h:396
unsigned char myGreen
Definition: Color.h:408
unsigned char myRed
Definition: Color.h:407
static const Color Navy
Definition: Color.h:400
Color(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
Definition: Color.h:110
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
static const Color Cyan
Definition: Color.h:395
bool valid() const
Color & setRGBA(DGtal::uint32_t aRGBA)
Definition: Color.cpp:54
static const Color Yellow
Definition: Color.h:397
void selfDisplay(std::ostream &out) const
Definition: Color.cpp:217
unsigned char myAlpha
Definition: Color.h:410
static const Color None
Definition: Color.h:387
static const Color Lime
Definition: Color.h:393
Color & operator-=(const Color &v)
Definition: Color.h:268
void flushPostscript(std::ostream &) const
Definition: Color.cpp:124
Color operator-(const Color &v) const
Definition: Color.h:287
unsigned char green() const
static const Color Blue
Definition: Color.h:394
static const Color Gray
Definition: Color.h:389
DGtal::uint32_t getRGBA() const
unsigned char myBlue
Definition: Color.h:409
Color(const Color &aColor)
Definition: Color.h:95
Color operator*(const double coeff) const
Definition: Color.h:330
std::string svg() const
Definition: Color.cpp:140
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
static const Color Silver
Definition: Color.h:398
bool isValid() const
Definition: Color.cpp:227
std::string tikz() const
Definition: Color.cpp:158
std::string postscript() const
Definition: Color.cpp:132
DGtal::uint32_t getRGB() const
static const Color Aqua
Definition: Color.h:401
Color & operator*=(const double coeff)
Definition: Color.h:311
DGtal is the top-level namespace which contains all DGtal functions and types.
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
static const Color White
Definition: Color.h:390
unsigned char blue() const
unsigned char alpha() const
static const Color Red
Definition: Color.h:391
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition: Color.cpp:65
KForm< Calculus, order, duality > operator*(const typename Calculus::Scalar &scalar, const KForm< Calculus, order, duality > &form)
static const Color Green
Definition: Color.h:392
unsigned char red() const
static const Color Purple
Definition: Color.h:399
std::string svgAlpha(const char *aPrefix) const
Definition: Color.cpp:149
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition: Color.h:124