DGtal 1.4.0
Loading...
Searching...
No Matches
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>
48#include <array>
50
51namespace DGtal
52{
53
55 // class Color
67 class Color
68 {
69 // ----------------------- Standard services ------------------------------
70 public:
71
75 ~Color() = default;
76
77 // ----------------------- Interface --------------------------------------
78 public:
79
87 Color( const unsigned int aRgb,
88 unsigned char aAlpha = 255 );
89
96 Color( const Color &aColor ) = default;
97
106 Color( const unsigned char aRedValue,
107 const unsigned char aGreenValue,
108 const unsigned char aBlueValue,
109 const unsigned char aAlphaValue = 255 )
110 : myRed(aRedValue),myGreen(aGreenValue),myBlue(aBlueValue),myAlpha(aAlphaValue) { }
111
112
120 Color( unsigned char aGrayValue,
121 unsigned char aAlphaValue = 255 )
122 : myRed(aGrayValue),myGreen(aGrayValue), myBlue(aGrayValue), myAlpha(aAlphaValue) { }
123
124
131 : myRed(0),myGreen(0),myBlue(0), myAlpha(255)
132 {
133 }
134
135
136 void red( const unsigned char aRedValue );
137
138 void green( const unsigned char aGreenValue );
139
140 void blue( const unsigned char aBlueValue );
141
142 void alpha( const unsigned char aAlphaValue );
143
144 unsigned char red() const ;
145
146 unsigned char green() const ;
147
148 unsigned char blue() const ;
149
150 unsigned char alpha() const ;
151
152
153 double r() const ;
154
155 double g() const ;
156
157 double b() const ;
158
159 double a() const ;
160
161
162 Color& setRGBi( const unsigned char aRedValue,
163 const unsigned char aGreenValue,
164 const unsigned char aBlueValue,
165 const unsigned char aAlphaValue = 255);
166
176 Color& setRGBA( DGtal::uint32_t aRGBA );
177
178
184 Color& setFromHSV( const double h, const double s, const double v)
185 {
186 double r,g,b;
187 Color::HSVtoRGB(r,g,b,h,s,v);
188 return this->setRGBf((float)r,(float)g,(float)b);
189 }
192 std::array<double, 3> getHSV() const
193 {
194 double h,s,v;
195 Color::RGBtoHSV(h,s,v, this->red(), this->green(), this->blue());
196 return {h,s,v};
197 }
198
205
212
213
214
215
216 bool valid() const;
217
218
223 void selfDisplay ( std::ostream & out ) const;
224
229 bool isValid() const;
230
231
232
233 Color & setRGBf( float red,
234 float green,
235 float blue,
236 float alpha = 1.0 );
237
238 bool operator==( const Color & aColor ) const;
239
240 bool operator!=( const Color & aColor ) const;
241
242 bool operator<( const Color & aColor ) const;
243
244 bool operator>( const Color & aColor ) const;
245
246 bool operator<=( const Color & aColor ) const;
247
248 bool operator>=( const Color & aColor ) const;
249
250
259 Color & operator+= ( const Color & v )
260 {
261 this->myRed = clamp((int)this->myRed + (int)v.myRed);
262 this->myBlue = clamp((int)this->myBlue + (int)v.myBlue);
263 this->myGreen = clamp((int)this->myGreen + (int)v.myGreen);
264#ifdef COLOR_WITH_ALPHA_ARITH
265 this->myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
266#endif
267 return *this;
268 }
269
279 Color operator+ ( const Color & v ) const
280 {
281 Color c;
282 c.myRed = clamp((int)this->myRed + (int)v.myRed);
283 c.myBlue =clamp((int)this->myBlue + (int)v.myBlue);
284 c.myGreen = clamp((int)this->myGreen + (int)v.myGreen);
285#ifdef COLOR_WITH_ALPHA_ARITH
286 c.myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
287#else
288 c.myAlpha = this->myAlpha ;
289#endif
290 return c;
291 }
292
302 Color & operator-= ( const Color & v )
303 {
304 this->myRed = clamp((int)this->myRed - (int)v.myRed);
305 this->myBlue = clamp((int)this->myBlue - (int)v.myBlue);
306 this->myGreen = clamp((int)this->myGreen - (int)v.myGreen);
307#ifdef COLOR_WITH_ALPHA_ARITH
308 this->myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
309#endif
310 return *this;
311 }
312
321 Color operator- ( const Color & v ) const
322 {
323 Color c;
324 c.myRed = clamp((int)this->myRed - (int)v.myRed);
325 c.myBlue = clamp((int)this->myBlue - (int)v.myBlue);
326 c.myGreen = clamp((int)this->myGreen - (int)v.myGreen);
327#ifdef COLOR_WITH_ALPHA_ARITH
328 c.myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
329#else
330 c.myAlpha = this->myAlpha ;
331#endif
332 return c;
333 }
334
345 Color &operator *= ( const double coeff)
346 {
347 this->myRed = clamp((double)this->myRed*coeff);
348 this->myBlue = clamp((double)this->myBlue*coeff);
349 this->myGreen = clamp((double)this->myGreen*coeff);
350#ifdef COLOR_WITH_ALPHA_ARITH
351 this->myAlpha = clamp((double)this->myAlpha*coeff);
352#endif
353 return *this;
354 }
355
364 Color operator * ( const double coeff) const
365 {
366 Color c;
367 c.myRed = clamp((double)this->myRed*coeff);
368 c.myBlue = clamp((double)this->myBlue*coeff);
369 c.myGreen = clamp((double)this->myGreen*coeff);
370#ifdef COLOR_WITH_ALPHA_ARITH
371 c.myAlpha = clamp((double)this->myAlpha*coeff);
372#else
373 c.myAlpha = this->myAlpha;
374#endif
375 return c;
376 }
377
384 Color & operator= ( const Color & pv ) = default;
385
386 void flushPostscript( std::ostream & ) const;
387
388 std::string svg() const;
389
398 std::string svgAlpha( const char * aPrefix ) const;
399
400 std::string postscript() const;
401
410 std::string tikz() const;
411
412 static const Color None;
413 static const Color Black;
414 static const Color Gray;
415 static const Color White;
416 static const Color Red;
417 static const Color Green;
418 static const Color Lime;
419 static const Color Blue;
420 static const Color Cyan;
421 static const Color Magenta;
422 static const Color Yellow;
423 static const Color Silver;
424 static const Color Purple;
425 static const Color Navy;
426 static const Color Aqua;
427
428 // ------------------------- Protected Datas ------------------------------
429 private:
430
431 // ------------------------- Private Datas --------------------------------
432 private:
433 unsigned char myRed;
434 unsigned char myGreen;
435 unsigned char myBlue;
436 unsigned char myAlpha;
437 // ------------------------- Hidden services ------------------------------
438 protected:
439
440
441 private:
442
450 unsigned char clamp(const double value) const
451 {
452 return static_cast<unsigned char>(std::max( std::min(value, 255.0), 0.0));
453 }
454
455
456 // ----------------------- Static methods ---------------------------------
457 public:
469 static void HSVtoRGB(double &r, double &g, double &b,
470 double h, const double s, const double v);
471
482 static void RGBtoHSV( double & h, double & s, double & v,
483 const unsigned char r,
484 const unsigned char g,
485 const unsigned char b );
486
487
488 // ------------------------- Internals ------------------------------------
489 private:
490
491 }; // end of class Color
492
493
494
504 Color
505 operator*( const double coeff,
506 const Color &aColor );
507
508
509
516 std::ostream&
517 operator<< ( std::ostream & out, const Color & aColor );
518
519
520} // namespace DGtal
521
522
524// Includes inline functions/methods
525#include "DGtal/io/Color.ih"
526
527
528// //
530
531#endif // !defined Color_h
532
533#undef Color_RECURSES
534#endif // else defined(Color_RECURSES)
Structure representing an RGB triple with alpha component.
Definition Color.h:68
static const Color Purple
Definition Color.h:424
Color(const Color &aColor)=default
unsigned char myRed
Definition Color.h:433
double g() const
unsigned char blue() const
static const Color Aqua
Definition Color.h:426
std::string postscript() const
Definition Color.cpp:150
unsigned char red() const
Color operator+(const Color &v) const
Definition Color.h:279
unsigned char myGreen
Definition Color.h:434
Color & operator-=(const Color &v)
Definition Color.h:302
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
static const Color Yellow
Definition Color.h:422
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 & setRGBA(DGtal::uint32_t aRGBA)
Definition Color.cpp:54
DGtal::uint32_t getRGB() const
std::array< double, 3 > getHSV() const
Definition Color.h:192
bool valid() const
double b() const
bool operator<(const Color &aColor) const
Definition Color.cpp:105
Color operator-(const Color &v) const
Definition Color.h:321
static const Color None
Definition Color.h:412
static const Color Cyan
Definition Color.h:420
void green(const unsigned char aGreenValue)
bool operator==(const Color &aColor) const
Definition Color.cpp:87
static void RGBtoHSV(double &h, double &s, double &v, const unsigned char r, const unsigned char g, const unsigned char b)
unsigned char myBlue
Definition Color.h:435
Color & setFromHSV(const double h, const double s, const double v)
Definition Color.h:184
Color & operator+=(const Color &v)
Definition Color.h:259
void selfDisplay(std::ostream &out) const
Definition Color.cpp:225
static const Color Green
Definition Color.h:417
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 void HSVtoRGB(double &r, double &g, double &b, double h, const double s, const double v)
static const Color Navy
Definition Color.h:425
Color operator*(const double coeff) const
Definition Color.h:364
static const Color Gray
Definition Color.h:414
std::string tikz() const
Definition Color.cpp:176
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition Color.h:120
static const Color Silver
Definition Color.h:423
void red(const unsigned char aRedValue)
static const Color Red
Definition Color.h:416
void alpha(const unsigned char aAlphaValue)
unsigned char clamp(const double value) const
Definition Color.h:450
~Color()=default
static const Color Lime
Definition Color.h:418
static const Color White
Definition Color.h:415
unsigned char green() const
double r() const
static const Color Blue
Definition Color.h:419
static const Color Black
Definition Color.h:413
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
Color & operator*=(const double coeff)
Definition Color.h:345
double a() const
std::string svgAlpha(const char *aPrefix) const
Definition Color.cpp:167
unsigned char myAlpha
Definition Color.h:436
Color & operator=(const Color &pv)=default
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:106
void blue(const unsigned char aBlueValue)
static const Color Magenta
Definition Color.h:421
DGtal::uint32_t getRGBA() const
DGtal is the top-level namespace which contains all DGtal functions and types.
KForm< Calculus, order, duality > operator*(const typename Calculus::Scalar &scalar, const KForm< Calculus, order, duality > &form)
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition BasicTypes.h:63
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)