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 
19 namespace 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;
34  static CColor MAGENTA;
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 
143  inline void Set(const std::string& str_color) {
144  try {
145  if (str_color == "black") *this = CColor::BLACK;
146  else if (str_color == "white") *this = CColor::WHITE;
147  else if (str_color == "red") *this = CColor::RED;
148  else if (str_color == "green") *this = CColor::GREEN;
149  else if (str_color == "blue") *this = CColor::BLUE;
150  else if (str_color == "magenta") *this = CColor::MAGENTA;
151  else if (str_color == "cyan") *this = CColor::CYAN;
152  else if (str_color == "yellow") *this = CColor::YELLOW;
153  else if (str_color == "orange") *this = CColor::ORANGE;
154  else if (str_color == "brown") *this = CColor::BROWN;
155  else if (str_color == "purple") *this = CColor::PURPLE;
156  else if (str_color == "gray10") *this = CColor::GRAY10;
157  else if (str_color == "gray20") *this = CColor::GRAY20;
158  else if (str_color == "gray30") *this = CColor::GRAY30;
159  else if (str_color == "gray40") *this = CColor::GRAY40;
160  else if (str_color == "gray50") *this = CColor::GRAY50;
161  else if (str_color == "gray60") *this = CColor::GRAY60;
162  else if (str_color == "gray70") *this = CColor::GRAY70;
163  else if (str_color == "gray80") *this = CColor::GRAY80;
164  else if (str_color == "gray90") *this = CColor::GRAY90;
165  else {
166  UInt16 unValues[4];
167  ParseValues<UInt16>(str_color, 4, unValues, ',');
168  for(UInt16 i = 0; i < 4; ++i) {
169  if(unValues[i] > 255) {
170  THROW_ARGOSEXCEPTION("Color value " << unValues[i] << " is larger than 255.");
171  }
172  }
173  Set(unValues[0], unValues[1], unValues[2], unValues[3]);
174  }
175  }
176  catch(CARGoSException& ex) {
177  THROW_ARGOSEXCEPTION_NESTED("Error while parsing color input string", ex);
178  }
179  }
180 
185  inline operator UInt32() {
186  return *reinterpret_cast<UInt32*>(&m_tChannels);
187  }
188 
194  inline bool operator==(const CColor& c_color2) const throw() {
195  return m_tChannels == c_color2.m_tChannels;
196  }
197 
203  inline bool operator!=(const CColor& c_color2) const throw() {
204  return m_tChannels != c_color2.m_tChannels;
205  }
206 
218  friend std::ostream& operator<<(std::ostream& os,
219  const CColor& c_color) {
220  if (c_color == CColor::BLACK) os << "black";
221  else if (c_color == CColor::WHITE) os << "white";
222  else if (c_color == CColor::RED) os << "red";
223  else if (c_color == CColor::GREEN) os << "green";
224  else if (c_color == CColor::BLUE) os << "blue";
225  else if (c_color == CColor::MAGENTA) os << "magenta";
226  else if (c_color == CColor::CYAN) os << "cyan";
227  else if (c_color == CColor::YELLOW) os << "yellow";
228  else if (c_color == CColor::ORANGE) os << "orange";
229  else if (c_color == CColor::BROWN) os << "brown";
230  else if (c_color == CColor::PURPLE) os << "purple";
231  else if (c_color == CColor::GRAY10) os << "gray10";
232  else if (c_color == CColor::GRAY20) os << "gray20";
233  else if (c_color == CColor::GRAY30) os << "gray30";
234  else if (c_color == CColor::GRAY40) os << "gray40";
235  else if (c_color == CColor::GRAY50) os << "gray50";
236  else if (c_color == CColor::GRAY60) os << "gray60";
237  else if (c_color == CColor::GRAY70) os << "gray70";
238  else if (c_color == CColor::GRAY80) os << "gray80";
239  else if (c_color == CColor::GRAY90) os << "gray90";
240  else {
241  os << c_color.m_tChannels.m_unRed
242  << "," << c_color.m_tChannels.m_unGreen
243  << "," << c_color.m_tChannels.m_unBlue
244  << "," << c_color.m_tChannels.m_unAlpha;
245  }
246  return os;
247  }
248 
258  friend std::istream& operator>>(std::istream& is,
259  CColor& c_color) {
260  std::string strColor;
261  is >> strColor;
262  c_color.Set(strColor);
263  return is;
264  }
265 
266 
267  private:
268 
269  struct TChannels {
270  UInt8 m_unRed;
271  UInt8 m_unGreen;
272  UInt8 m_unBlue;
273  UInt8 m_unAlpha;
274 
275  TChannels() :
276  m_unRed(0),
277  m_unGreen(0),
278  m_unBlue(0),
279  m_unAlpha(255) {}
280 
281  TChannels(const UInt8 un_red,
282  const UInt8 un_green,
283  const UInt8 un_blue,
284  const UInt8 un_alpha = 255) :
285  m_unRed(un_red),
286  m_unGreen(un_green),
287  m_unBlue(un_blue),
288  m_unAlpha(un_alpha) {}
289 
290  inline bool operator==(const TChannels& t_channels) const {
291  return
292  (m_unRed == t_channels.m_unRed) &&
293  (m_unGreen == t_channels.m_unGreen) &&
294  (m_unBlue == t_channels.m_unBlue) &&
295  (m_unAlpha == t_channels.m_unAlpha);
296  }
297 
298  inline bool operator!=(const TChannels& t_channels) const {
299  return
300  (m_unRed != t_channels.m_unRed) ||
301  (m_unGreen != t_channels.m_unGreen) ||
302  (m_unBlue != t_channels.m_unBlue) ||
303  (m_unAlpha != t_channels.m_unAlpha);
304  }
305 
306  } m_tChannels;
307 
308  };
309 
310 }
311 
312 #endif
void SetAlpha(UInt8 un_alpha)
Sets the alpha channel of the color.
Definition: color.h:117
static CColor BLACK
Definition: color.h:29
friend std::ostream & operator<<(std::ostream &os, const CColor &c_color)
Output stream operator.
Definition: color.h:218
static CColor GRAY50
Definition: color.h:44
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
static CColor YELLOW
Definition: color.h:36
unsigned char UInt8
8-bit unsigned integer.
Definition: datatypes.h:60
static CColor WHITE
Definition: color.h:30
static CColor PURPLE
Definition: color.h:39
void SetRed(UInt8 un_red)
Sets the red channel of the color.
Definition: color.h:84
UInt8 GetGreen() const
Returns the green channel of the color.
Definition: color.h:90
static CColor CYAN
Definition: color.h:35
static CColor GRAY90
Definition: color.h:48
static CColor BROWN
Definition: color.h:38
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception...
static CColor MAGENTA
Definition: color.h:34
static CColor GRAY20
Definition: color.h:41
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
static CColor BLUE
Definition: color.h:33
friend std::istream & operator>>(std::istream &is, CColor &c_color)
Input stream operator.
Definition: color.h:258
static CColor GRAY70
Definition: color.h:46
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
UInt8 GetRed() const
Returns the red channel of the color.
Definition: color.h:79
void Set(const std::string &str_color)
Sets the RGBA values of the color from a string.
Definition: color.h:143
static CColor ORANGE
Definition: color.h:37
The exception that wraps all errors in ARGoS.
static CColor GRAY60
Definition: color.h:45
static CColor GREEN
Definition: color.h:32
Real ToGrayScale() const
Returns the color in grayscale.
Definition: color.h:68
CColor(UInt8 un_red, UInt8 un_green, UInt8 un_blue, UInt8 un_alpha=255)
Class constructor.
Definition: color.h:58
The basic color type.
Definition: color.h:25
UInt8 GetAlpha() const
Returns the alpha channel of the color.
Definition: color.h:112
static CColor RED
Definition: color.h:31
bool operator==(const CColor &c_color2) const
Returns true if the given color is identical to the current.
Definition: color.h:194
void SetBlue(UInt8 un_blue)
Sets the blue channel of the color.
Definition: color.h:106
static CColor GRAY30
Definition: color.h:42
UInt8 GetBlue() const
Returns the blue channel of the color.
Definition: color.h:101
unsigned short UInt16
16-bit unsigned integer.
Definition: datatypes.h:78
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
static CColor GRAY10
Definition: color.h:40
void SetGreen(UInt8 un_green)
Sets the green channel of the color.
Definition: color.h:95
CColor()
Class constructor.
Definition: color.h:53
static CColor GRAY40
Definition: color.h:43
bool operator!=(const CColor &c_color2) const
Returns true if the given color is different from the current.
Definition: color.h:203