Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
Color.h
1
16
17#pragma once
18
31
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 <DGtal/base/BasicTypes.h>
45#include <iostream>
46#include <algorithm>
47#include <array>
48#include <string>
50
51#ifndef secured_sprintf
52#if defined( WIN32 )
53#define secured_sprintf sprintf_s
54#else
55#include <stdio.h>
56#define secured_sprintf snprintf
57#endif // defined( WIN32 )
58#endif
59
60namespace DGtal
61{
62
64 // class Color
76 class Color
77 {
78 // ----------------------- Standard services ------------------------------
79 public:
80
84 ~Color() = default;
85
86 // ----------------------- Interface --------------------------------------
87 public:
88
95
96 Color( const unsigned int aRgb,
97 unsigned char aAlpha = 255 );
98
104
105 Color( const Color &aColor ) = default;
106
115 Color( const unsigned char aRedValue,
116 const unsigned char aGreenValue,
117 const unsigned char aBlueValue,
118 const unsigned char aAlphaValue = 255 )
119 : myRed(aRedValue),myGreen(aGreenValue),myBlue(aBlueValue),myAlpha(aAlphaValue) { }
120
121
128
129 Color( unsigned char aGrayValue,
130 unsigned char aAlphaValue = 255 )
131 : myRed(aGrayValue),myGreen(aGrayValue), myBlue(aGrayValue), myAlpha(aAlphaValue) { }
132
133
138
140 : myRed(0),myGreen(0),myBlue(0), myAlpha(255)
141 {
142 }
143
144
145 void red( const unsigned char aRedValue );
146
147 void green( const unsigned char aGreenValue );
148
149 void blue( const unsigned char aBlueValue );
150
151 void alpha( const unsigned char aAlphaValue );
152
153 unsigned char red() const ;
154
155 unsigned char green() const ;
156
157 unsigned char blue() const ;
158
159 unsigned char alpha() const ;
160
161
162 double r() const ;
163
164 double g() const ;
165
166 double b() const ;
167
168 double a() const ;
169
170
171 Color& setRGBi( const unsigned char aRedValue,
172 const unsigned char aGreenValue,
173 const unsigned char aBlueValue,
174 const unsigned char aAlphaValue = 255);
175
186
187
193 Color& setFromHSV( const double h, const double s, const double v)
194 {
195 double r,g,b;
196 Color::HSVtoRGB(r,g,b,h,s,v);
197 return this->setRGBf((float)r,(float)g,(float)b);
198 }
199
201 std::array<double, 3> getHSV() const
202 {
203 double h,s,v;
204 Color::RGBtoHSV(h,s,v, this->red(), this->green(), this->blue());
205 return {h,s,v};
206 }
207
212
214
219
221
222
223
224
225 bool valid() const;
226
227
232 void selfDisplay ( std::ostream & out ) const;
233
238 bool isValid() const;
239
240
241
242 Color & setRGBf( float red,
243 float green,
244 float blue,
245 float alpha = 1.0 );
246
247 bool operator==( const Color & aColor ) const;
248
249 bool operator!=( const Color & aColor ) const;
250
251 bool operator<( const Color & aColor ) const;
252
253 bool operator>( const Color & aColor ) const;
254
255 bool operator<=( const Color & aColor ) const;
256
257 bool operator>=( const Color & aColor ) const;
258
259
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
288 Color operator+ ( const Color & v ) const
289 {
290 Color c;
291 c.myRed = clamp((int)this->myRed + (int)v.myRed);
292 c.myBlue =clamp((int)this->myBlue + (int)v.myBlue);
293 c.myGreen = clamp((int)this->myGreen + (int)v.myGreen);
294#ifdef COLOR_WITH_ALPHA_ARITH
295 c.myAlpha = clamp((int)this->myAlpha + (int)v.myAlpha);
296#else
297 c.myAlpha = this->myAlpha ;
298#endif
299 return c;
300 }
301
311 Color & operator-= ( const Color & v )
312 {
313 this->myRed = clamp((int)this->myRed - (int)v.myRed);
314 this->myBlue = clamp((int)this->myBlue - (int)v.myBlue);
315 this->myGreen = clamp((int)this->myGreen - (int)v.myGreen);
316#ifdef COLOR_WITH_ALPHA_ARITH
317 this->myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
318#endif
319 return *this;
320 }
321
330 Color operator- ( const Color & v ) const
331 {
332 Color c;
333 c.myRed = clamp((int)this->myRed - (int)v.myRed);
334 c.myBlue = clamp((int)this->myBlue - (int)v.myBlue);
335 c.myGreen = clamp((int)this->myGreen - (int)v.myGreen);
336#ifdef COLOR_WITH_ALPHA_ARITH
337 c.myAlpha = clamp((int)this->myAlpha - (int)v.myAlpha);
338#else
339 c.myAlpha = this->myAlpha ;
340#endif
341 return c;
342 }
343
354 Color &operator *= ( const double coeff)
355 {
356 this->myRed = clamp((double)this->myRed*coeff);
357 this->myBlue = clamp((double)this->myBlue*coeff);
358 this->myGreen = clamp((double)this->myGreen*coeff);
359#ifdef COLOR_WITH_ALPHA_ARITH
360 this->myAlpha = clamp((double)this->myAlpha*coeff);
361#endif
362 return *this;
363 }
364
373 Color operator * ( const double coeff) const
374 {
375 Color c;
376 c.myRed = clamp((double)this->myRed*coeff);
377 c.myBlue = clamp((double)this->myBlue*coeff);
378 c.myGreen = clamp((double)this->myGreen*coeff);
379#ifdef COLOR_WITH_ALPHA_ARITH
380 c.myAlpha = clamp((double)this->myAlpha*coeff);
381#else
382 c.myAlpha = this->myAlpha;
383#endif
384 return c;
385 }
386
393 Color & operator= ( const Color & pv ) = default;
394
395 void flushPostscript( std::ostream & ) const;
396
397 std::string svg() const;
398
407 std::string svgAlpha( const char * aPrefix ) const;
408
409 std::string postscript() const;
410
419 std::string tikz() const;
420
421 static const Color None;
422 static const Color Black;
423 static const Color Gray;
424 static const Color White;
425 static const Color Red;
426 static const Color Green;
427 static const Color Lime;
428 static const Color Blue;
429 static const Color Cyan;
430 static const Color Magenta;
431 static const Color Yellow;
432 static const Color Silver;
433 static const Color Purple;
434 static const Color Navy;
435 static const Color Aqua;
436
437 // ------------------------- Protected Datas ------------------------------
438 private:
439
440 // ------------------------- Private Datas --------------------------------
441 private:
442 unsigned char myRed;
443 unsigned char myGreen;
444 unsigned char myBlue;
445 unsigned char myAlpha;
446 // ------------------------- Hidden services ------------------------------
447 protected:
448
449
450 private:
451
459 unsigned char clamp(const double value) const
460 {
461 return static_cast<unsigned char>(std::max( std::min(value, 255.0), 0.0));
462 }
463
464
465 // ----------------------- Static methods ---------------------------------
466 public:
478 static void HSVtoRGB(double &r, double &g, double &b,
479 double h, const double s, const double v);
480
491 static void RGBtoHSV( double & h, double & s, double & v,
492 const unsigned char r,
493 const unsigned char g,
494 const unsigned char b );
495
496
497 // ------------------------- Internals ------------------------------------
498 private:
499
500 }; // end of class Color
501
502
503
513 Color
514 operator*( const double coeff,
515 const Color &aColor );
516
517
518
525 std::ostream&
526 operator<< ( std::ostream & out, const Color & aColor );
527
528
529} // namespace DGtal
530
531
533// Includes inline functions/methods
534#include "DGtal/io/Color.ih"
535
536
537// //
539
540#endif // !defined Color_h
541
542#undef Color_RECURSES
543#endif // else defined(Color_RECURSES)
Structure representing an RGB triple with alpha component.
Definition Color.h:77
Color(const Color &aColor)=default
static const Color Navy
Definition Color.h:434
unsigned char myRed
Definition Color.h:442
double g() const
std::string tikz() const
unsigned char blue() const
std::string svgAlpha(const char *aPrefix) const
unsigned char red() const
Color operator+(const Color &v) const
Definition Color.h:288
unsigned char myGreen
Definition Color.h:443
Color & operator-=(const Color &v)
Definition Color.h:311
Color & setRGBi(const unsigned char aRedValue, const unsigned char aGreenValue, const unsigned char aBlueValue, const unsigned char aAlphaValue=255)
std::string postscript() const
static const Color Purple
Definition Color.h:433
bool operator>(const Color &aColor) const
bool isValid() const
void flushPostscript(std::ostream &) const
DGtal::uint32_t getRGB() const
std::array< double, 3 > getHSV() const
Definition Color.h:201
Color(const unsigned int aRgb, unsigned char aAlpha=255)
bool valid() const
double b() const
bool operator<(const Color &aColor) const
Color operator-(const Color &v) const
Definition Color.h:330
static const Color Silver
Definition Color.h:432
static const Color Aqua
Definition Color.h:435
Color & setRGBA(DGtal::uint32_t aRGBA)
void green(const unsigned char aGreenValue)
bool operator==(const Color &aColor) const
static const Color Lime
Definition Color.h:427
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:444
Color & setFromHSV(const double h, const double s, const double v)
Definition Color.h:193
static const Color Red
Definition Color.h:425
Color & operator+=(const Color &v)
Definition Color.h:268
void selfDisplay(std::ostream &out) const
bool operator!=(const Color &aColor) const
static const Color None
Definition Color.h:421
bool operator<=(const Color &aColor) const
std::string svg() const
static void HSVtoRGB(double &r, double &g, double &b, double h, const double s, const double v)
Color operator*(const double coeff) const
Definition Color.h:373
Color(unsigned char aGrayValue, unsigned char aAlphaValue=255)
Definition Color.h:129
static const Color Black
Definition Color.h:422
static const Color Blue
Definition Color.h:428
void red(const unsigned char aRedValue)
void alpha(const unsigned char aAlphaValue)
unsigned char clamp(const double value) const
Definition Color.h:459
~Color()=default
unsigned char green() const
double r() const
static const Color Yellow
Definition Color.h:431
bool operator>=(const Color &aColor) const
static const Color Cyan
Definition Color.h:429
Color & operator*=(const double coeff)
Definition Color.h:354
static const Color Gray
Definition Color.h:423
double a() const
unsigned char myAlpha
Definition Color.h:445
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
static const Color Green
Definition Color.h:426
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:115
void blue(const unsigned char aBlueValue)
static const Color White
Definition Color.h:424
static const Color Magenta
Definition Color.h:430
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)
std::uint32_t uint32_t
unsigned 32-bit integer.
Definition BasicTypes.h:62
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)