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