Board  0.9.5
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 <cstddef>
30 #include <cstdio>
31 #include <ostream>
32 #include <string>
33 #include "Tools.h"
34 
35 namespace LibBoard
36 {
37 
43 class Color {
44 
45 public:
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 
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 midRGB(const Color & a, const Color & b, float t);
147 
148  static Color midHSV(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 
154 public:
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 
175 private:
176  int _red;
177  int _green;
178  int _blue;
179  int _alpha;
180 };
181 
182 std::ostream & operator<<(std::ostream & out, const Color & color);
183 
184 // Inline methods
185 
186 Color & 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 
195 bool Color::isNull() const
196 {
197  return _red == -1 && _green == -1 && _blue == -1;
198 }
199 
200 bool Color::isValid() const
201 {
202  return !isNull();
203 }
204 
205 Color::Color() : _red(0), _green(0), _blue(0), _alpha(255) {}
206 
207 Color::Color(std::nullptr_t)
208 {
209  _red = _green = _blue = -1;
210  _alpha = 255;
211 }
212 
213 Color::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 
220 Color::Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha) : _red(red), _green(green), _blue(blue), _alpha(alpha) {}
221 
222 Color::Color(unsigned char gray, unsigned char alpha) : _red(gray), _green(gray), _blue(gray), _alpha(alpha) {}
223 
224 Color 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 
229 // FIXME : Make alpha parameter take precedence over AA in html string
230 Color::Color(const std::string & htmlColor, unsigned char alpha) : Color(htmlColor.c_str(), alpha) {}
231 
232 Color::Color(const std::string & htmlColor) : Color(htmlColor.c_str(), 255) {}
233 
234 Color::Color(const char * htmlColor, unsigned char alpha) : _alpha(alpha)
235 {
236  unsigned int r, g, b, a = alpha;
237  if (!htmlColor || *htmlColor == '\0') {
238  *this = Color::Null;
239  return;
240  }
241  if (strlen(htmlColor) == 7 && sscanf(htmlColor, "#%2x%2x%2x", &r, &g, &b) == 3) {
242  _red = (int)r;
243  _green = (int)g;
244  _blue = (int)b;
245  _alpha = (int)a;
246  } else if (strlen(htmlColor) == 9 && sscanf(htmlColor, "#%2x%2x%2x%2x", &r, &g, &b, &a) == 4) {
247  _red = (int)r;
248  _green = (int)g;
249  _blue = (int)b;
250  _alpha = (int)a;
251  } else {
252  *this = Color::Null;
253  Tools::error << "Color::Color(htmlcolor): cannot parse color string\n";
254  }
255 }
256 
257 void Color::red(const unsigned char red)
258 {
259  _red = red;
260 }
261 
262 void Color::green(unsigned char green)
263 {
264  _green = green;
265 }
266 
267 void Color::blue(unsigned char blue)
268 {
269  _blue = blue;
270 }
271 
272 void Color::alpha(unsigned char alpha)
273 {
274  _alpha = alpha;
275 }
276 
277 unsigned char Color::red() const
278 {
279  return static_cast<unsigned char>(_red);
280 }
281 
282 unsigned char Color::green() const
283 {
284  return static_cast<unsigned char>(_green);
285 }
286 
287 unsigned char Color::blue() const
288 {
289  return static_cast<unsigned char>(_blue);
290 }
291 
292 unsigned char Color::alpha() const
293 {
294  return static_cast<unsigned char>(_alpha);
295 }
296 } // namespace LibBoard
297 
298 #endif // BOARD_COLOR_H
LibBoard::Color::setHSV
Color & setHSV(float hue, float saturation, float value, float alpha=1.0)
setHSV
Definition: Color.cpp:56
LibBoard::Color::svgAlpha
std::string svgAlpha(const char *prefix) const
Definition: Color.cpp:132
LibBoard::Color::tikz
std::string tikz() const
Definition: Color.cpp:142
Tools.h
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
LibBoard::Color::setRGBi
Color & setRGBi(const unsigned char red, const unsigned char green, const unsigned char blue, const unsigned char alpha=255)
setRGBi
Definition: Color.h:186
LibBoard::Color
Structure representing an RGB triple.
Definition: Color.h:43