Board 0.9.6
Color.h
Go to the documentation of this file.
1/* -*- mode: c++ -*- */
26#ifndef BOARD_COLOR_H
27#define BOARD_COLOR_H
28
29#include <board/Tools.h>
30#include <cstddef>
31#include <cstdio>
32#include <ostream>
33#include <string>
34
35namespace LibBoard
36{
37
43class Color {
44
45public:
46 inline Color();
47 inline Color(const unsigned int rgb, unsigned char alpha = 255);
48 inline Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255);
49 inline Color(unsigned char gray, unsigned char alpha = 255);
50
51 static inline Color fromRGBf(float red, float green, float blue, float alpha = 1.0f);
52 static inline Color gray(unsigned char grayLevel, unsigned char alpha = 255);
58 inline Color(const std::string & htmlColor, unsigned char alpha);
59
64 inline Color(const std::string & htmlColor);
65
66 inline Color(const char * htmlColor, unsigned char alpha = 255);
67
71 inline Color(std::nullptr_t);
72
73 inline void red(unsigned char red);
74 inline void green(unsigned char green);
75 inline void blue(unsigned char blue);
76 inline void alpha(unsigned char alpha);
77
78 inline unsigned char red() const;
79 inline unsigned char green() const;
80 inline unsigned char blue() const;
81 inline unsigned char alpha() const;
82
91 inline Color & setRGBi(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha = 255);
92
101 Color & setHSV(float hue, float saturation, float value, float alpha = 1.0);
102
103 Color & setRGBf(float red, float green, float blue, float alpha = 1.0);
104
105 bool operator==(const Color & other) const;
106
107 bool operator!=(const Color & other) const;
108
109 bool operator<(const Color & other) const;
110
111 void flushPostscript(std::ostream &) const;
112
113 std::string svg() const;
114
115 inline bool isNull() const;
116
117 inline bool isValid() const;
118
126 std::string svgAlpha(const char * prefix) const;
127
128 std::string postscript() const;
129
138 std::string tikz() const;
139
140 void toHSV(float & hue, float & saturation, float & value, float & alpha) const;
141
142 inline bool valid() const { return (*this) != Color::Null; }
143
144 static Color fromHSV(float hue, float saturation, float value, float alpha = 1.0f);
145
146 static Color mixRGB(const Color & a, const Color & b, float t);
147
148 static Color mixHSV(const Color & a, const Color & b, float t);
149
150 static Color fromHueColormap(float t);
151
152 std::ostream & flush(std::ostream & out) const;
153
154public:
155 static const Color Null;
156 static const Color Black;
157 static const Color Brown;
158 static const Color Pink;
159 static const Color Gray;
160 static const Color White;
161 static const Color Red;
162 static const Color Green;
163 static const Color DarkGreen;
164 static const Color Lime;
165 static const Color Blue;
166 static const Color Cyan;
167 static const Color DarkCyan;
168 static const Color Magenta;
169 static const Color Yellow;
170 static const Color Silver;
171 static const Color Purple;
172 static const Color Navy;
173 static const Color Aqua;
174
175private:
176 int _red;
177 int _green;
178 int _blue;
179 int _alpha;
180};
181
182std::ostream & operator<<(std::ostream & out, const Color & color);
183
184// Inline methods
185
186Color & Color::setRGBi(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha)
187{
188 _red = red;
189 _green = green;
190 _blue = blue;
191 _alpha = alpha;
192 return *this;
193}
194
195bool Color::isNull() const
196{
197 return _red == -1 && _green == -1 && _blue == -1;
198}
199
200bool Color::isValid() const
201{
202 return !isNull();
203}
204
205Color::Color() : _red(0), _green(0), _blue(0), _alpha(255) {}
206
207Color::Color(std::nullptr_t)
208{
209 _red = _green = _blue = -1;
210 _alpha = 255;
211}
212
213Color::Color(const unsigned int rgb, unsigned char alpha) : _alpha(alpha)
214{
215 _red = (rgb & 0xFF0000u) >> 16;
216 _green = (rgb & 0xFF00u) >> 8;
217 _blue = rgb & 0xFF;
218}
219
220Color::Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha) : _red(red), _green(green), _blue(blue), _alpha(alpha) {}
221
222Color::Color(unsigned char gray, unsigned char alpha) : _red(gray), _green(gray), _blue(gray), _alpha(alpha) {}
223
224Color Color::fromRGBf(float red, float green, float blue, float alpha)
225{
226 return Color(static_cast<unsigned char>(red * 255), static_cast<unsigned char>(green * 255), static_cast<unsigned char>(blue * 255), static_cast<unsigned char>(alpha * 255));
227}
228
229Color Color::gray(unsigned char grayLevel, unsigned char alpha)
230{
231 return Color(grayLevel, grayLevel, grayLevel, alpha);
232}
233
234// FIXME : Make alpha parameter take precedence over AA in html string
235Color::Color(const std::string & htmlColor, unsigned char alpha) : Color(htmlColor.c_str(), alpha) {}
236
237Color::Color(const std::string & htmlColor) : Color(htmlColor.c_str(), 255) {}
238
239Color::Color(const char * htmlColor, unsigned char alpha) : _alpha(alpha)
240{
241 unsigned int r, g, b, a = alpha;
242 if (!htmlColor || *htmlColor == '\0') {
243 *this = Color::Null;
244 return;
245 }
246 if (strlen(htmlColor) == 7 && sscanf(htmlColor, "#%2x%2x%2x", &r, &g, &b) == 3) {
247 _red = (int)r;
248 _green = (int)g;
249 _blue = (int)b;
250 _alpha = (int)a;
251 } else if (strlen(htmlColor) == 9 && sscanf(htmlColor, "#%2x%2x%2x%2x", &r, &g, &b, &a) == 4) {
252 _red = (int)r;
253 _green = (int)g;
254 _blue = (int)b;
255 _alpha = (int)a;
256 } else {
257 *this = Color::Null;
258 Tools::error << "Color::Color(htmlcolor): cannot parse color string\n";
259 }
260}
261
262void Color::red(const unsigned char red)
263{
264 _red = red;
265}
266
267void Color::green(unsigned char green)
268{
269 _green = green;
270}
271
272void Color::blue(unsigned char blue)
273{
274 _blue = blue;
275}
276
277void Color::alpha(unsigned char alpha)
278{
279 _alpha = alpha;
280}
281
282unsigned char Color::red() const
283{
284 return static_cast<unsigned char>(_red);
285}
286
287unsigned char Color::green() const
288{
289 return static_cast<unsigned char>(_green);
290}
291
292unsigned char Color::blue() const
293{
294 return static_cast<unsigned char>(_blue);
295}
296
297unsigned char Color::alpha() const
298{
299 return static_cast<unsigned char>(_alpha);
300}
301} // namespace LibBoard
302
303#endif // BOARD_COLOR_H
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
Structure representing an RGB triple.
Definition Color.h:43
static Color fromRGBf(float red, float green, float blue, float alpha=1.0f)
Definition Color.h:224
static Color fromHueColormap(float t)
Definition Color.cpp:229
static const Color DarkGreen
Definition Color.h:163
static const Color Navy
Definition Color.h:172
static const Color Lime
Definition Color.h:164
bool isNull() const
Definition Color.h:195
unsigned char blue() const
Definition Color.h:292
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition Color.cpp:71
static const Color Blue
Definition Color.h:165
bool isValid() const
Definition Color.h:200
unsigned char red() const
Definition Color.h:282
static Color gray(unsigned char grayLevel, unsigned char alpha=255)
Definition Color.h:229
static const Color Null
Definition Color.h:155
std::string postscript() const
Definition Color.cpp:116
static const Color Pink
Definition Color.h:158
static const Color DarkCyan
Definition Color.h:167
static const Color Aqua
Definition Color.h:173
static const Color Purple
Definition Color.h:171
std::string svgAlpha(const char *prefix) const
Definition Color.cpp:132
static Color mixRGB(const Color &a, const Color &b, float t)
Definition Color.cpp:208
static const Color Brown
Definition Color.h:157
static const Color Silver
Definition Color.h:170
static const Color Gray
Definition Color.h:159
unsigned char green() const
Definition Color.h:287
static const Color Black
Definition Color.h:156
std::ostream & flush(std::ostream &out) const
Definition Color.cpp:234
std::string tikz() const
Definition Color.cpp:142
static Color fromHSV(float hue, float saturation, float value, float alpha=1.0f)
Definition Color.cpp:201
static const Color Magenta
Definition Color.h:168
static const Color Cyan
Definition Color.h:166
Color & setRGBi(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha=255)
setRGBi
Definition Color.h:186
bool operator!=(const Color &other) const
Definition Color.cpp:89
bool valid() const
Definition Color.h:142
std::string svg() const
Definition Color.cpp:123
void toHSV(float &hue, float &saturation, float &value, float &alpha) const
Definition Color.cpp:172
bool operator==(const Color &other) const
Definition Color.cpp:84
static Color mixHSV(const Color &a, const Color &b, float t)
Definition Color.cpp:217
void flushPostscript(std::ostream &) const
Definition Color.cpp:111
static const Color Yellow
Definition Color.h:169
Color & setHSV(float hue, float saturation, float value, float alpha=1.0)
setHSV
Definition Color.cpp:56
Color()
Definition Color.h:205
static const Color White
Definition Color.h:160
unsigned char alpha() const
Definition Color.h:297
bool operator<(const Color &other) const
Definition Color.cpp:94
static const Color Red
Definition Color.h:161
static const Color Green
Definition Color.h:162
MessageStream error
Definition Board.h:55
std::ostream & operator<<(std::ostream &out, const Color &color)
Definition Color.cpp:245