Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // A RGB-color definition
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <cmath>
26 #include <cassert>
27 #include <string>
28 #include <sstream>
31 #include <utils/common/ToString.h>
34 #include <utils/common/StdDefs.h>
35 
36 #include "RGBColor.h"
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
42 
43 const RGBColor RGBColor::RED = RGBColor(255, 0, 0, 255);
44 const RGBColor RGBColor::GREEN = RGBColor(0, 255, 0, 255);
45 const RGBColor RGBColor::BLUE = RGBColor(0, 0, 255, 255);
46 const RGBColor RGBColor::YELLOW = RGBColor(255, 255, 0, 255);
47 const RGBColor RGBColor::CYAN = RGBColor(0, 255, 255, 255);
48 const RGBColor RGBColor::MAGENTA = RGBColor(255, 0, 255, 255);
49 const RGBColor RGBColor::ORANGE = RGBColor(255, 128, 0, 255);
50 const RGBColor RGBColor::WHITE = RGBColor(255, 255, 255, 255);
51 const RGBColor RGBColor::BLACK = RGBColor(0, 0, 0, 255);
52 const RGBColor RGBColor::GREY = RGBColor(128, 128, 128, 255);
53 const RGBColor RGBColor::INVISIBLE = RGBColor(0, 0, 0, 0);
54 
57 
58 // random colors do not affect the simulation. No initialization is necessary
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
64 
65 RGBColor::RGBColor(bool valid)
66  : myRed(0), myGreen(0), myBlue(0), myAlpha(0), myValid(valid) {}
67 
68 
69 RGBColor::RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
70  : myRed(red), myGreen(green), myBlue(blue), myAlpha(alpha), myValid(true) {}
71 
72 
73 unsigned char
74 RGBColor::red() const {
75  return myRed;
76 }
77 
78 
79 unsigned char
80 RGBColor::green() const {
81  return myGreen;
82 }
83 
84 
85 unsigned char
86 RGBColor::blue() const {
87  return myBlue;
88 }
89 
90 
91 unsigned char
92 RGBColor::alpha() const {
93  return myAlpha;
94 }
95 
96 
97 void
98 RGBColor::set(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
99  myRed = r;
100  myGreen = g;
101  myBlue = b;
102  myAlpha = a;
103  myValid = true;
104 }
105 
106 
107 void
108 RGBColor::setAlpha(unsigned char alpha) {
109  myAlpha = alpha;
110 }
111 
112 
113 void
114 RGBColor::setValid(const bool value) {
115  myValid = value;
116 }
117 
118 
119 bool
121  return myValid;
122 }
123 
124 
125 std::ostream&
126 operator<<(std::ostream& os, const RGBColor& col) {
127  if (col == RGBColor::RED) {
128  return os << "red";
129  }
130  if (col == RGBColor::GREEN) {
131  return os << "green";
132  }
133  if (col == RGBColor::BLUE) {
134  return os << "blue";
135  }
136  if (col == RGBColor::YELLOW) {
137  return os << "yellow";
138  }
139  if (col == RGBColor::CYAN) {
140  return os << "cyan";
141  }
142  if (col == RGBColor::MAGENTA) {
143  return os << "magenta";
144  }
145  if (col == RGBColor::ORANGE) {
146  return os << "orange";
147  }
148  if (col == RGBColor::WHITE) {
149  return os << "white";
150  }
151  if (col == RGBColor::BLACK) {
152  return os << "black";
153  }
154  if (col == RGBColor::GREY) {
155  return os << "grey";
156  }
157  os << static_cast<int>(col.myRed) << ","
158  << static_cast<int>(col.myGreen) << ","
159  << static_cast<int>(col.myBlue);
160  if (col.myAlpha < 255) {
161  os << "," << static_cast<int>(col.myAlpha);
162  }
163  return os;
164 }
165 
166 
167 bool
169  return (myRed == c.myRed) && (myGreen == c.myGreen) && (myBlue == c.myBlue) && (myAlpha == c.myAlpha) && (myValid == c.myValid);
170 }
171 
172 
173 bool
175  return (myRed != c.myRed) || (myGreen != c.myGreen) || (myBlue != c.myBlue) || (myAlpha != c.myAlpha) || (myValid != c.myValid);
176 }
177 
178 
179 RGBColor
181  // obtain inverse colors
182  const unsigned char r = (unsigned char)(255 - (int)myRed);
183  const unsigned char g = (unsigned char)(255 - (int)myGreen);
184  const unsigned char b = (unsigned char)(255 - (int)myBlue);
185  // return inverted RBColor
186  return RGBColor(r, g, b, myAlpha);
187 }
188 
189 
190 SumoRNG*
192  return &myRNG;
193 }
194 
195 
196 RGBColor
197 RGBColor::changedBrightness(int change, int toChange) const {
198  const unsigned char red = (unsigned char)(MIN2(MAX2(myRed + change, 0), 255));
199  const unsigned char blue = (unsigned char)(MIN2(MAX2(myBlue + change, 0), 255));
200  const unsigned char green = (unsigned char)(MIN2(MAX2(myGreen + change, 0), 255));
201  int changed = ((int)red - (int)myRed) + ((int)blue - (int)myBlue) + ((int)green - (int)myGreen);
202  const RGBColor result(red, green, blue, myAlpha);
203  if (changed == toChange * change) {
204  return result;
205  } else if (changed == 0) {
206  return result;
207  } else {
208  const int maxedColors = (red != myRed + change ? 1 : 0) + (blue != myBlue + change ? 1 : 0) + (green != myGreen + change ? 1 : 0);
209  if (maxedColors == 3) {
210  return result;
211  } else {
212  const int toChangeNext = 3 - maxedColors;
213  return result.changedBrightness((int)((toChange * change - changed) / toChangeNext), toChangeNext);
214  }
215  }
216 }
217 
218 
219 RGBColor
220 RGBColor::changedAlpha(int change) const {
221  int alpha = MIN2(MAX2((int)myAlpha + change, 0), 255);
222  return RGBColor(myRed, myGreen, myBlue, (unsigned char)alpha);
223 }
224 
225 
226 RGBColor
227 RGBColor::multiply(double factor) const {
228  const unsigned char red = (unsigned char)floor(MIN2(MAX2(myRed * factor, 0.0), 255.0) + 0.5);
229  const unsigned char blue = (unsigned char)floor(MIN2(MAX2(myBlue * factor, 0.0), 255.0) + 0.5);
230  const unsigned char green = (unsigned char)floor(MIN2(MAX2(myGreen * factor, 0.0), 255.0) + 0.5);
231  return RGBColor(red, green, blue, myAlpha);
232 }
233 
234 
235 RGBColor
236 RGBColor::parseColor(std::string coldef) {
237  coldef = StringUtils::to_lower_case(coldef);
238  if (coldef == "red") {
239  return RED;
240  }
241  if (coldef == "green") {
242  return GREEN;
243  }
244  if (coldef == "blue") {
245  return BLUE;
246  }
247  if (coldef == "yellow") {
248  return YELLOW;
249  }
250  if (coldef == "cyan") {
251  return CYAN;
252  }
253  if (coldef == "magenta") {
254  return MAGENTA;
255  }
256  if (coldef == "orange") {
257  return ORANGE;
258  }
259  if (coldef == "white") {
260  return WHITE;
261  }
262  if (coldef == "black") {
263  return BLACK;
264  }
265  if (coldef == "grey" || coldef == "gray") {
266  return GREY;
267  }
268  if (coldef == "invisible") {
269  return INVISIBLE;
270  }
271  if (coldef == "random") {
272  return fromHSV(RandHelper::rand(360, &myRNG),
273  // prefer more saturated colors
274  pow(RandHelper::rand(&myRNG), 0.3),
275  // prefer brighter colors
276  pow(RandHelper::rand(&myRNG), 0.3));
277  }
278  unsigned char r = 0;
279  unsigned char g = 0;
280  unsigned char b = 0;
281  unsigned char a = 255;
282  if (coldef[0] == '#') {
283  const int coldesc = StringUtils::hexToInt(coldef);
284  if (coldef.length() == 7) {
285  r = static_cast<unsigned char>((coldesc & 0xFF0000) >> 16);
286  g = static_cast<unsigned char>((coldesc & 0x00FF00) >> 8);
287  b = coldesc & 0xFF;
288  } else if (coldef.length() == 9) {
289  r = static_cast<unsigned char>((coldesc & 0xFF000000) >> 24);
290  g = static_cast<unsigned char>((coldesc & 0x00FF0000) >> 16);
291  b = static_cast<unsigned char>((coldesc & 0x0000FF00) >> 8);
292  a = coldesc & 0xFF;
293  } else {
294  throw EmptyData();
295  }
296  } else {
297  std::vector<std::string> st = StringTokenizer(coldef, ",").getVector();
298  if (st.size() == 3 || st.size() == 4) {
299  try {
300  r = static_cast<unsigned char>(StringUtils::toInt(st[0]));
301  g = static_cast<unsigned char>(StringUtils::toInt(st[1]));
302  b = static_cast<unsigned char>(StringUtils::toInt(st[2]));
303  if (st.size() == 4) {
304  a = static_cast<unsigned char>(StringUtils::toInt(st[3]));
305  }
306  if (r <= 1 && g <= 1 && b <= 1 && (st.size() == 3 || a <= 1)) {
307  throw NumberFormatException("(color component) " + coldef);
308  }
309  } catch (NumberFormatException&) {
310  r = static_cast<unsigned char>(StringUtils::toDouble(st[0]) * 255. + 0.5);
311  g = static_cast<unsigned char>(StringUtils::toDouble(st[1]) * 255. + 0.5);
312  b = static_cast<unsigned char>(StringUtils::toDouble(st[2]) * 255. + 0.5);
313  if (st.size() == 4) {
314  a = static_cast<unsigned char>(StringUtils::toDouble(st[3]) * 255. + 0.5);
315  }
316  }
317  } else {
318  throw InvalidArgument("Invalid color definition '" + coldef + "'");
319  }
320  }
321  return RGBColor(r, g, b, a);
322 }
323 
324 
325 RGBColor
327  const std::string& coldef, const std::string& objecttype,
328  const char* objectid, bool report, bool& ok) {
329  UNUSED_PARAMETER(report);
330  try {
331  return parseColor(coldef);
332  } catch (NumberFormatException&) {
333  } catch (EmptyData&) {
334  }
335  ok = false;
336  std::ostringstream oss;
337  oss << "Attribute 'color' in definition of ";
338  if (objectid == nullptr) {
339  oss << "a ";
340  }
341  oss << objecttype;
342  if (objectid != nullptr) {
343  oss << " '" << objectid << "'";
344  }
345  oss << " is not a valid color.";
346  WRITE_ERROR(oss.str());
347  return RGBColor();
348 }
349 
350 
351 RGBColor
352 RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, double weight) {
353  if (weight < 0) {
354  weight = 0;
355  }
356  if (weight > 1) {
357  weight = 1;
358  }
359  const unsigned char r = (unsigned char)((int)minColor.myRed + (((int)maxColor.myRed - (int)minColor.myRed) * weight));
360  const unsigned char g = (unsigned char)((int)minColor.myGreen + (((int)maxColor.myGreen - (int)minColor.myGreen) * weight));
361  const unsigned char b = (unsigned char)((int)minColor.myBlue + (((int)maxColor.myBlue - (int)minColor.myBlue) * weight));
362  const unsigned char a = (unsigned char)((int)minColor.myAlpha + (((int)maxColor.myAlpha - (int)minColor.myAlpha) * weight));
363  return RGBColor(r, g, b, a);
364 }
365 
366 
367 RGBColor
368 RGBColor::fromHSV(double h, double s, double v) {
369  h /= 60.;
370  const int i = int(floor(h));
371  double f = h - i;
372  if (i % 2 == 0) {
373  f = 1. - f;
374  }
375  const unsigned char m = static_cast<unsigned char>(v * (1 - s) * 255. + 0.5);
376  const unsigned char n = static_cast<unsigned char>(v * (1 - s * f) * 255. + 0.5);
377  const unsigned char vv = static_cast<unsigned char>(v * 255. + 0.5);
378  switch (i) {
379  case 6:
380  case 0:
381  return RGBColor(vv, n, m, 255);
382  case 1:
383  return RGBColor(n, vv, m, 255);
384  case 2:
385  return RGBColor(m, vv, n, 255);
386  case 3:
387  return RGBColor(m, n, vv, 255);
388  case 4:
389  return RGBColor(n, m, vv, 255);
390  case 5:
391  return RGBColor(vv, m, n, 255);
392  }
393  return RGBColor(255, 255, 255, 255);
394 }
395 
396 RGBColor
397 RGBColor::randomHue(double s, double v) {
398  return fromHSV(RandHelper::rand(360, &myRNG), s, v);
399 }
400 
401 
402 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
std::ostream & operator<<(std::ostream &os, const RGBColor &col)
Definition: RGBColor.cpp:126
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
T MIN2(T a, T b)
Definition: StdDefs.h:74
T MAX2(T a, T b)
Definition: StdDefs.h:80
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
bool myValid
flag to check if color is valid
Definition: RGBColor.h:209
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, double weight)
Interpolates between two colors.
Definition: RGBColor.cpp:352
unsigned char myRed
The color amounts.
Definition: RGBColor.h:206
void setValid(const bool value)
set valid
Definition: RGBColor.cpp:114
void setAlpha(unsigned char alpha)
Sets a new alpha value.
Definition: RGBColor.cpp:108
static const RGBColor WHITE
Definition: RGBColor.h:192
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.cpp:74
static const std::string DEFAULT_COLOR_STRING
The string description of the default color.
Definition: RGBColor.h:202
static const RGBColor BLUE
Definition: RGBColor.h:187
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.cpp:92
static const RGBColor GREY
Definition: RGBColor.h:194
static const RGBColor YELLOW
Definition: RGBColor.h:188
static SumoRNG myRNG
A random number generator to generate random colors independent of other randomness.
Definition: RGBColor.h:212
static const RGBColor INVISIBLE
Definition: RGBColor.h:195
RGBColor(bool valid=true)
Constructor.
Definition: RGBColor.cpp:65
static SumoRNG * getColorRNG()
get color RNG
Definition: RGBColor.cpp:191
RGBColor multiply(double factor) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:227
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:236
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:326
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.cpp:80
static const RGBColor ORANGE
Definition: RGBColor.h:191
static const RGBColor CYAN
Definition: RGBColor.h:189
RGBColor invertedColor() const
obtain inverted of current RGBColor
Definition: RGBColor.cpp:180
bool isValid() const
check if RGBColor is valid
Definition: RGBColor.cpp:120
static const RGBColor GREEN
Definition: RGBColor.h:186
static RGBColor fromHSV(double h, double s, double v)
Converts the given hsv-triplet to rgb, inspired by http://alvyray.com/Papers/CG/hsv2rgb....
Definition: RGBColor.cpp:368
unsigned char myBlue
Definition: RGBColor.h:206
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.cpp:86
static const RGBColor BLACK
Definition: RGBColor.h:193
RGBColor changedBrightness(int change, int toChange=3) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:197
bool operator!=(const RGBColor &c) const
Definition: RGBColor.cpp:174
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
assigns new values
Definition: RGBColor.cpp:98
RGBColor changedAlpha(int change) const
Returns a new color with altered opacity.
Definition: RGBColor.cpp:220
unsigned char myGreen
Definition: RGBColor.h:206
static const RGBColor MAGENTA
Definition: RGBColor.h:190
bool operator==(const RGBColor &c) const
Definition: RGBColor.cpp:168
unsigned char myAlpha
Definition: RGBColor.h:206
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:199
static const RGBColor RED
named colors
Definition: RGBColor.h:185
static RGBColor randomHue(double s=1, double v=1)
Return color with random hue.
Definition: RGBColor.cpp:397
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:119
std::vector< std::string > getVector()
return vector of strings
static int hexToInt(const std::string &sData)
converts a string with a hex value into the integer value described by it by calling the char-type co...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:59
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...