ARGoS 3
A parallel, multi-engine simulator for swarm robotics
color.h
Go to the documentation of this file.
1
11#ifndef COLOR_H
12#define COLOR_H
13
14#include <argos3/core/utility/datatypes/datatypes.h>
15#include <argos3/core/utility/string_utilities.h>
16#include <string.h>
17#include <iostream>
18
19namespace argos {
20
25 class CColor {
26
27 public:
28
29 static CColor BLACK;
30 static CColor WHITE;
31 static CColor RED;
32 static CColor GREEN;
33 static CColor BLUE;
35 static CColor CYAN;
36 static CColor YELLOW;
37 static CColor ORANGE;
38 static CColor BROWN;
39 static CColor PURPLE;
40 static CColor GRAY10;
41 static CColor GRAY20;
42 static CColor GRAY30;
43 static CColor GRAY40;
44 static CColor GRAY50;
45 static CColor GRAY60;
46 static CColor GRAY70;
47 static CColor GRAY80;
48 static CColor GRAY90;
49
53 CColor() {}
54
58 explicit CColor(UInt8 un_red,
59 UInt8 un_green,
60 UInt8 un_blue,
61 UInt8 un_alpha = 255) throw() :
62 m_tChannels(un_red, un_green, un_blue, un_alpha) {}
63
68 inline Real ToGrayScale() const throw() {
69 return
70 0.299f * m_tChannels.m_unRed +
71 0.587f * m_tChannels.m_unGreen +
72 0.114f * m_tChannels.m_unBlue;
73 }
74
79 inline UInt8 GetRed() const throw() { return m_tChannels.m_unRed; }
84 inline void SetRed(UInt8 un_red) throw() { m_tChannels.m_unRed = un_red; }
85
90 inline UInt8 GetGreen() const throw() { return m_tChannels.m_unGreen; }
95 inline void SetGreen(UInt8 un_green) throw() { m_tChannels.m_unGreen = un_green; }
96
101 inline UInt8 GetBlue() const throw() { return m_tChannels.m_unBlue; }
106 inline void SetBlue(UInt8 un_blue) throw() { m_tChannels.m_unBlue = un_blue; }
107
112 inline UInt8 GetAlpha() const throw() { return m_tChannels.m_unAlpha; }
117 inline void SetAlpha(UInt8 un_alpha) throw() { m_tChannels.m_unAlpha = un_alpha; }
118
126 inline void Set(UInt8 un_red,
127 UInt8 un_green,
128 UInt8 un_blue,
129 UInt8 un_alpha = 255) throw() {
130 SetRed(un_red);
131 SetGreen(un_green);
132 SetBlue(un_blue);
133 SetAlpha(un_alpha);
134 }
135
144 inline CColor Blend(const CColor& c_background) const throw() {
145 Real fAlpha = m_tChannels.m_unAlpha / 255.0;
146 return CColor(
147 static_cast<UInt8>(fAlpha * GetRed() + (1.0-fAlpha) * c_background.GetRed()),
148 static_cast<UInt8>(fAlpha * GetGreen() + (1.0-fAlpha) * c_background.GetGreen()),
149 static_cast<UInt8>(fAlpha * GetBlue() + (1.0-fAlpha) * c_background.GetBlue())
150 );
151 }
152
160 inline void Set(const std::string& str_color) {
161 try {
162 if (str_color == "black") *this = CColor::BLACK;
163 else if (str_color == "white") *this = CColor::WHITE;
164 else if (str_color == "red") *this = CColor::RED;
165 else if (str_color == "green") *this = CColor::GREEN;
166 else if (str_color == "blue") *this = CColor::BLUE;
167 else if (str_color == "magenta") *this = CColor::MAGENTA;
168 else if (str_color == "cyan") *this = CColor::CYAN;
169 else if (str_color == "yellow") *this = CColor::YELLOW;
170 else if (str_color == "orange") *this = CColor::ORANGE;
171 else if (str_color == "brown") *this = CColor::BROWN;
172 else if (str_color == "purple") *this = CColor::PURPLE;
173 else if (str_color == "gray10") *this = CColor::GRAY10;
174 else if (str_color == "gray20") *this = CColor::GRAY20;
175 else if (str_color == "gray30") *this = CColor::GRAY30;
176 else if (str_color == "gray40") *this = CColor::GRAY40;
177 else if (str_color == "gray50") *this = CColor::GRAY50;
178 else if (str_color == "gray60") *this = CColor::GRAY60;
179 else if (str_color == "gray70") *this = CColor::GRAY70;
180 else if (str_color == "gray80") *this = CColor::GRAY80;
181 else if (str_color == "gray90") *this = CColor::GRAY90;
182 else {
183 UInt16 unValues[4];
184 ParseValues<UInt16>(str_color, 4, unValues, ',');
185 for(UInt16 i = 0; i < 4; ++i) {
186 if(unValues[i] > 255) {
187 THROW_ARGOSEXCEPTION("Color value " << unValues[i] << " is larger than 255.");
188 }
189 }
190 Set(unValues[0], unValues[1], unValues[2], unValues[3]);
191 }
192 }
193 catch(CARGoSException& ex) {
194 THROW_ARGOSEXCEPTION_NESTED("Error while parsing color input string", ex);
195 }
196 }
197
202 inline operator UInt32() {
203 return *reinterpret_cast<UInt32*>(&m_tChannels);
204 }
205
211 inline bool operator==(const CColor& c_color2) const throw() {
212 return m_tChannels == c_color2.m_tChannels;
213 }
214
220 inline bool operator!=(const CColor& c_color2) const throw() {
221 return m_tChannels != c_color2.m_tChannels;
222 }
223
235 friend std::ostream& operator<<(std::ostream& os,
236 const CColor& c_color) {
237 if (c_color == CColor::BLACK) os << "black";
238 else if (c_color == CColor::WHITE) os << "white";
239 else if (c_color == CColor::RED) os << "red";
240 else if (c_color == CColor::GREEN) os << "green";
241 else if (c_color == CColor::BLUE) os << "blue";
242 else if (c_color == CColor::MAGENTA) os << "magenta";
243 else if (c_color == CColor::CYAN) os << "cyan";
244 else if (c_color == CColor::YELLOW) os << "yellow";
245 else if (c_color == CColor::ORANGE) os << "orange";
246 else if (c_color == CColor::BROWN) os << "brown";
247 else if (c_color == CColor::PURPLE) os << "purple";
248 else if (c_color == CColor::GRAY10) os << "gray10";
249 else if (c_color == CColor::GRAY20) os << "gray20";
250 else if (c_color == CColor::GRAY30) os << "gray30";
251 else if (c_color == CColor::GRAY40) os << "gray40";
252 else if (c_color == CColor::GRAY50) os << "gray50";
253 else if (c_color == CColor::GRAY60) os << "gray60";
254 else if (c_color == CColor::GRAY70) os << "gray70";
255 else if (c_color == CColor::GRAY80) os << "gray80";
256 else if (c_color == CColor::GRAY90) os << "gray90";
257 else {
258 os << c_color.m_tChannels.m_unRed
259 << "," << c_color.m_tChannels.m_unGreen
260 << "," << c_color.m_tChannels.m_unBlue
261 << "," << c_color.m_tChannels.m_unAlpha;
262 }
263 return os;
264 }
265
275 friend std::istream& operator>>(std::istream& is,
276 CColor& c_color) {
277 std::string strColor;
278 is >> strColor;
279 c_color.Set(strColor);
280 return is;
281 }
282
283
284 private:
285
286 struct TChannels {
287 UInt8 m_unRed;
288 UInt8 m_unGreen;
289 UInt8 m_unBlue;
290 UInt8 m_unAlpha;
291
292 TChannels() :
293 m_unRed(0),
294 m_unGreen(0),
295 m_unBlue(0),
296 m_unAlpha(255) {}
297
298 TChannels(const UInt8 un_red,
299 const UInt8 un_green,
300 const UInt8 un_blue,
301 const UInt8 un_alpha = 255) :
302 m_unRed(un_red),
303 m_unGreen(un_green),
304 m_unBlue(un_blue),
305 m_unAlpha(un_alpha) {}
306
307 inline bool operator==(const TChannels& t_channels) const {
308 return
309 (m_unRed == t_channels.m_unRed) &&
310 (m_unGreen == t_channels.m_unGreen) &&
311 (m_unBlue == t_channels.m_unBlue) &&
312 (m_unAlpha == t_channels.m_unAlpha);
313 }
314
315 inline bool operator!=(const TChannels& t_channels) const {
316 return
317 (m_unRed != t_channels.m_unRed) ||
318 (m_unGreen != t_channels.m_unGreen) ||
319 (m_unBlue != t_channels.m_unBlue) ||
320 (m_unAlpha != t_channels.m_unAlpha);
321 }
322
323 } m_tChannels;
324
325 };
326
327}
328
329#endif
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
unsigned char UInt8
8-bit unsigned integer.
Definition datatypes.h:60
float Real
Collects all ARGoS code.
Definition datatypes.h:39
unsigned short UInt16
16-bit unsigned integer.
Definition datatypes.h:78
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
void ParseValues(std::istream &str_input, UInt32 un_num_fields, T *pt_field_buffer, const char ch_delimiter='\n')
The exception that wraps all errors in ARGoS.
The basic color type.
Definition color.h:25
CColor()
Class constructor.
Definition color.h:53
Real ToGrayScale() const
Returns the color in grayscale.
Definition color.h:68
static CColor GRAY80
Definition color.h:47
void Set(UInt8 un_red, UInt8 un_green, UInt8 un_blue, UInt8 un_alpha=255)
Sets the RGBA values of the color.
Definition color.h:126
static CColor BLACK
Definition color.h:29
static CColor GRAY20
Definition color.h:41
void Set(const std::string &str_color)
Sets the RGBA values of the color from a string.
Definition color.h:160
static CColor YELLOW
Definition color.h:36
static CColor GRAY30
Definition color.h:42
static CColor CYAN
Definition color.h:35
static CColor GRAY50
Definition color.h:44
bool operator==(const CColor &c_color2) const
Returns true if the given color is identical to the current.
Definition color.h:211
static CColor PURPLE
Definition color.h:39
static CColor GRAY90
Definition color.h:48
bool operator!=(const CColor &c_color2) const
Returns true if the given color is different from the current.
Definition color.h:220
void SetGreen(UInt8 un_green)
Sets the green channel of the color.
Definition color.h:95
UInt8 GetBlue() const
Returns the blue channel of the color.
Definition color.h:101
void SetBlue(UInt8 un_blue)
Sets the blue channel of the color.
Definition color.h:106
void SetAlpha(UInt8 un_alpha)
Sets the alpha channel of the color.
Definition color.h:117
UInt8 GetGreen() const
Returns the green channel of the color.
Definition color.h:90
friend std::ostream & operator<<(std::ostream &os, const CColor &c_color)
Output stream operator.
Definition color.h:235
static CColor GRAY10
Definition color.h:40
CColor Blend(const CColor &c_background) const
Blends the current color with a background color.
Definition color.h:144
static CColor RED
Definition color.h:31
static CColor GRAY70
Definition color.h:46
friend std::istream & operator>>(std::istream &is, CColor &c_color)
Input stream operator.
Definition color.h:275
static CColor BLUE
Definition color.h:33
UInt8 GetRed() const
Returns the red channel of the color.
Definition color.h:79
static CColor GREEN
Definition color.h:32
static CColor GRAY40
Definition color.h:43
static CColor GRAY60
Definition color.h:45
UInt8 GetAlpha() const
Returns the alpha channel of the color.
Definition color.h:112
CColor(UInt8 un_red, UInt8 un_green, UInt8 un_blue, UInt8 un_alpha=255)
Class constructor.
Definition color.h:58
void SetRed(UInt8 un_red)
Sets the red channel of the color.
Definition color.h:84
static CColor ORANGE
Definition color.h:37
static CColor BROWN
Definition color.h:38
static CColor MAGENTA
Definition color.h:34
static CColor WHITE
Definition color.h:30