SUMO - Simulation of Urban MObility
RGBColor.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A RGB-color definition
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cmath>
28 #include <cassert>
29 #include <string>
30 #include <sstream>
32 #include <utils/common/ToString.h>
35 #include <utils/common/StdDefs.h>
36 #include "RGBColor.h"
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
42 const RGBColor RGBColor::RED = RGBColor(255, 0, 0, 255);
43 const RGBColor RGBColor::GREEN = RGBColor(0, 255, 0, 255);
44 const RGBColor RGBColor::BLUE = RGBColor(0, 0, 255, 255);
45 const RGBColor RGBColor::YELLOW = RGBColor(255, 255, 0, 255);
46 const RGBColor RGBColor::CYAN = RGBColor(0, 255, 255, 255);
47 const RGBColor RGBColor::MAGENTA = RGBColor(255, 0, 255, 255);
48 const RGBColor RGBColor::ORANGE = RGBColor(255, 128, 0, 255);
49 const RGBColor RGBColor::WHITE = RGBColor(255, 255, 255, 255);
50 const RGBColor RGBColor::BLACK = RGBColor(0, 0, 0, 255);
51 const RGBColor RGBColor::GREY = RGBColor(128, 128, 128, 255);
52 
54 const std::string RGBColor::DEFAULT_COLOR_STRING = toString(RGBColor::DEFAULT_COLOR);
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
61  : myRed(0), myGreen(0), myBlue(0), myAlpha(0) {}
62 
63 
64 RGBColor::RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
65  : myRed(red), myGreen(green), myBlue(blue), myAlpha(alpha) {}
66 
67 
69  : myRed(col.myRed), myGreen(col.myGreen), myBlue(col.myBlue), myAlpha(col.myAlpha) {}
70 
71 
73 
74 
75 void
76 RGBColor::set(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
77  myRed = r;
78  myGreen = g;
79  myBlue = b;
80  myAlpha = a;
81 }
82 
83 
84 std::ostream&
85 operator<<(std::ostream& os, const RGBColor& col) {
86  if (col == RGBColor::RED) {
87  return os << "red";
88  }
89  if (col == RGBColor::GREEN) {
90  return os << "green";
91  }
92  if (col == RGBColor::BLUE) {
93  return os << "blue";
94  }
95  if (col == RGBColor::YELLOW) {
96  return os << "yellow";
97  }
98  if (col == RGBColor::CYAN) {
99  return os << "cyan";
100  }
101  if (col == RGBColor::MAGENTA) {
102  return os << "magenta";
103  }
104  if (col == RGBColor::ORANGE) {
105  return os << "orange";
106  }
107  if (col == RGBColor::WHITE) {
108  return os << "white";
109  }
110  if (col == RGBColor::BLACK) {
111  return os << "black";
112  }
113  if (col == RGBColor::GREY) {
114  return os << "grey";
115  }
116  os << static_cast<int>(col.myRed) << ","
117  << static_cast<int>(col.myGreen) << ","
118  << static_cast<int>(col.myBlue);
119  if (col.myAlpha < 255) {
120  os << "," << static_cast<int>(col.myAlpha);
121  }
122  return os;
123 }
124 
125 
126 bool
128  return myRed == c.myRed && myGreen == c.myGreen && myBlue == c.myBlue && myAlpha == c.myAlpha;
129 }
130 
131 
132 bool
134  return myRed != c.myRed || myGreen != c.myGreen || myBlue != c.myBlue || myAlpha != c.myAlpha;
135 }
136 
137 
138 RGBColor
140  // obtain inverse colors
141  const unsigned char r = (unsigned char)(255 - (int)myRed);
142  const unsigned char g = (unsigned char)(255 - (int)myGreen);
143  const unsigned char b = (unsigned char)(255 - (int)myBlue);
144  // return inverted RBColor
145  return RGBColor(r, g, b, myAlpha);
146 }
147 
148 
149 RGBColor
150 RGBColor::changedBrightness(int change, int toChange) const {
151  const unsigned char red = (unsigned char)(MIN2(MAX2(myRed + change, 0), 255));
152  const unsigned char blue = (unsigned char)(MIN2(MAX2(myBlue + change, 0), 255));
153  const unsigned char green = (unsigned char)(MIN2(MAX2(myGreen + change, 0), 255));
154  int changed = ((int)red - (int)myRed) + ((int)blue - (int)myBlue) + ((int)green - (int)myGreen);
155  const RGBColor result(red, green, blue, myAlpha);
156  if (changed == toChange * change) {
157  return result;
158  } else if (changed == 0) {
159  return result;
160  } else {
161  const int maxedColors = (red != myRed + change ? 1 : 0) + (blue != myBlue + change ? 1 : 0) + (green != myGreen + change ? 1 : 0);
162  if (maxedColors == 3) {
163  return result;
164  } else {
165  const int toChangeNext = 3 - maxedColors;
166  return result.changedBrightness((int)((toChange * change - changed) / toChangeNext), toChangeNext);
167  }
168  }
169 }
170 
171 
172 RGBColor
173 RGBColor::parseColor(std::string coldef) {
174  std::transform(coldef.begin(), coldef.end(), coldef.begin(), tolower);
175  if (coldef == "red") {
176  return RED;
177  }
178  if (coldef == "green") {
179  return GREEN;
180  }
181  if (coldef == "blue") {
182  return BLUE;
183  }
184  if (coldef == "yellow") {
185  return YELLOW;
186  }
187  if (coldef == "cyan") {
188  return CYAN;
189  }
190  if (coldef == "magenta") {
191  return MAGENTA;
192  }
193  if (coldef == "orange") {
194  return ORANGE;
195  }
196  if (coldef == "white") {
197  return WHITE;
198  }
199  if (coldef == "black") {
200  return BLACK;
201  }
202  if (coldef == "grey" || coldef == "gray") {
203  return GREY;
204  }
205  unsigned char r = 0;
206  unsigned char g = 0;
207  unsigned char b = 0;
208  unsigned char a = 255;
209  if (coldef[0] == '#') {
210  const int coldesc = TplConvert::_hex2int(coldef.c_str());
211  if (coldef.length() == 7) {
212  r = static_cast<unsigned char>((coldesc & 0xFF0000) >> 16);
213  g = static_cast<unsigned char>((coldesc & 0x00FF00) >> 8);
214  b = coldesc & 0xFF;
215  } else if (coldef.length() == 9) {
216  r = static_cast<unsigned char>((coldesc & 0xFF000000) >> 24);
217  g = static_cast<unsigned char>((coldesc & 0x00FF0000) >> 16);
218  b = static_cast<unsigned char>((coldesc & 0x0000FF00) >> 8);
219  a = coldesc & 0xFF;
220  } else {
221  throw EmptyData();
222  }
223  } else {
224  std::vector<std::string> st = StringTokenizer(coldef, ",").getVector();
225  if (st.size() == 3 || st.size() == 4) {
226  try {
227  r = static_cast<unsigned char>(TplConvert::_2int(st[0].c_str()));
228  g = static_cast<unsigned char>(TplConvert::_2int(st[1].c_str()));
229  b = static_cast<unsigned char>(TplConvert::_2int(st[2].c_str()));
230  if (st.size() == 4) {
231  a = static_cast<unsigned char>(TplConvert::_2int(st[3].c_str()));
232  }
233  if (r <= 1 && g <= 1 && b <= 1 && (st.size() == 3 || a <= 1)) {
234  throw NumberFormatException();
235  }
236  } catch (NumberFormatException&) {
237  r = static_cast<unsigned char>(TplConvert::_2double(st[0].c_str()) * 255. + 0.5);
238  g = static_cast<unsigned char>(TplConvert::_2double(st[1].c_str()) * 255. + 0.5);
239  b = static_cast<unsigned char>(TplConvert::_2double(st[2].c_str()) * 255. + 0.5);
240  if (st.size() == 4) {
241  a = static_cast<unsigned char>(TplConvert::_2double(st[3].c_str()) * 255. + 0.5);
242  }
243  }
244  } else {
245  throw EmptyData();
246  }
247  }
248  return RGBColor(r, g, b, a);
249 }
250 
251 
252 RGBColor
254  const std::string& coldef, const std::string& objecttype,
255  const char* objectid, bool report, bool& ok) {
256  UNUSED_PARAMETER(report);
257  try {
258  return parseColor(coldef);
259  } catch (NumberFormatException&) {
260  } catch (EmptyData&) {
261  }
262  ok = false;
263  std::ostringstream oss;
264  oss << "Attribute 'color' in definition of ";
265  if (objectid == 0) {
266  oss << "a ";
267  }
268  oss << objecttype;
269  if (objectid != 0) {
270  oss << " '" << objectid << "'";
271  }
272  oss << " is not a valid color.";
273  WRITE_ERROR(oss.str());
274  return RGBColor();
275 }
276 
277 
278 RGBColor
279 RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, double weight) {
280  if (weight < 0) {
281  weight = 0;
282  }
283  if (weight > 1) {
284  weight = 1;
285  }
286  const unsigned char r = (unsigned char)((int)minColor.myRed + (((int)maxColor.myRed - (int)minColor.myRed) * weight));
287  const unsigned char g = (unsigned char)((int)minColor.myGreen + (((int)maxColor.myGreen - (int)minColor.myGreen) * weight));
288  const unsigned char b = (unsigned char)((int)minColor.myBlue + (((int)maxColor.myBlue - (int)minColor.myBlue) * weight));
289  const unsigned char a = (unsigned char)((int)minColor.myAlpha + (((int)maxColor.myAlpha - (int)minColor.myAlpha) * weight));
290  return RGBColor(r, g, b, a);
291 }
292 
293 
294 RGBColor
295 RGBColor::fromHSV(double h, double s, double v) {
296  // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
297  // RGB are each returned on [0, 255].
298  //float h = HSV.H, s = HSV.S, v = HSV.V,
299  double f;
300  h /= 60.;
301  int i;
302  //if (h == UNDEFINED) RETURN_RGB(v, v, v);
303  i = int(floor(h));
304  f = float(h - i);
305  if (!(i & 1)) {
306  f = 1 - f; // if i is even
307  }
308  const unsigned char m = static_cast<unsigned char>(v * (1 - s) * 255. + 0.5);
309  const unsigned char n = static_cast<unsigned char>(v * (1 - s * f) * 255. + 0.5);
310  const unsigned char vv = static_cast<unsigned char>(v * 255. + 0.5);
311  switch (i) {
312  case 6:
313  case 0:
314  return RGBColor(vv, n, m, 255);
315  case 1:
316  return RGBColor(n, vv, m, 255);
317  case 2:
318  return RGBColor(m, vv, n, 255);
319  case 3:
320  return RGBColor(m, n, vv, 255);
321  case 4:
322  return RGBColor(n, m, vv, 255);
323  case 5:
324  return RGBColor(vv, m, n, 255);
325  }
326  return RGBColor(255, 255, 255, 255);
327 }
328 
329 
330 /****************************************************************************/
331 
static int _hex2int(const E *const data)
converts a char-type array with a hex value into the integer value described by it ...
Definition: TplConvert.h:170
static const RGBColor BLUE
Definition: RGBColor.h:174
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:173
RGBColor changedBrightness(int change, int toChange=3) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:150
~RGBColor()
Destructor.
Definition: RGBColor.cpp:72
static RGBColor fromHSV(double h, double s, double v)
Converts the given hsv-triplet to rgb.
Definition: RGBColor.cpp:295
static const RGBColor WHITE
Definition: RGBColor.h:179
static RGBColor parseColorReporting(const std::string &coldef, const std::string &objecttype, const char *objectid, bool report, bool &ok)
Parses a color information.
Definition: RGBColor.cpp:253
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:83
static const RGBColor ORANGE
Definition: RGBColor.h:178
T MAX2(T a, T b)
Definition: StdDefs.h:68
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:76
friend std::ostream & operator<<(std::ostream &os, const RGBColor &col)
Writes the color to the given stream.
Definition: RGBColor.cpp:85
static const RGBColor BLACK
Definition: RGBColor.h:180
bool operator==(const RGBColor &c) const
Definition: RGBColor.cpp:127
RGBColor invertedColor() const
obtain inverted of current RGBColor
Definition: RGBColor.cpp:139
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
bool operator!=(const RGBColor &c) const
Definition: RGBColor.cpp:133
static const RGBColor GREEN
Definition: RGBColor.h:173
static const RGBColor GREY
Definition: RGBColor.h:181
unsigned char myAlpha
Definition: RGBColor.h:192
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
unsigned char myRed
The color amounts.
Definition: RGBColor.h:192
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
assigns new values
Definition: RGBColor.cpp:76
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:185
unsigned char myGreen
Definition: RGBColor.h:192
static const RGBColor MAGENTA
Definition: RGBColor.h:177
T MIN2(T a, T b)
Definition: StdDefs.h:62
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:248
static const RGBColor YELLOW
Definition: RGBColor.h:175
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
static const RGBColor RED
named colors
Definition: RGBColor.h:172
static const RGBColor CYAN
Definition: RGBColor.h:176
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:69
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:305
unsigned char myBlue
Definition: RGBColor.h:192
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:62
RGBColor()
Constructor.
Definition: RGBColor.cpp:60
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, double weight)
Interpolates between two colors.
Definition: RGBColor.cpp:279
static const std::string DEFAULT_COLOR_STRING
The string description of the default color.
Definition: RGBColor.h:188