SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
13 // Copyright (C) 2001-2014 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 <string>
36 #include <sstream>
38 #include <utils/common/ToString.h>
41 #include <utils/common/StdDefs.h>
42 #include "RGBColor.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // static member definitions
51 // ===========================================================================
52 const RGBColor RGBColor::RED = RGBColor(255, 0, 0, 255);
53 const RGBColor RGBColor::GREEN = RGBColor(0, 255, 0, 255);
54 const RGBColor RGBColor::BLUE = RGBColor(0, 0, 255, 255);
55 const RGBColor RGBColor::YELLOW = RGBColor(255, 255, 0, 255);
56 const RGBColor RGBColor::CYAN = RGBColor(0, 255, 255, 255);
57 const RGBColor RGBColor::MAGENTA = RGBColor(255, 0, 255, 255);
58 const RGBColor RGBColor::WHITE = RGBColor(255, 255, 255, 255);
59 const RGBColor RGBColor::BLACK = RGBColor(0, 0, 0, 255);
60 const RGBColor RGBColor::GREY = RGBColor(128, 128, 128, 255);
61 
63 const std::string RGBColor::DEFAULT_COLOR_STRING = toString(RGBColor::DEFAULT_COLOR);
64 
65 
66 // ===========================================================================
67 // method definitions
68 // ===========================================================================
70  : myRed(0), myGreen(0), myBlue(0), myAlpha(0) {}
71 
72 
73 RGBColor::RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
74  : myRed(red), myGreen(green), myBlue(blue), myAlpha(alpha) {}
75 
76 
78  : myRed(col.myRed), myGreen(col.myGreen), myBlue(col.myBlue), myAlpha(col.myAlpha) {}
79 
80 
82 
83 
84 void
85 RGBColor::set(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
86  myRed = r;
87  myGreen = g;
88  myBlue = b;
89  myAlpha = a;
90 }
91 
92 
93 std::ostream&
94 operator<<(std::ostream& os, const RGBColor& col) {
95  if (col == RGBColor::RED) {
96  return os << "red";
97  }
98  if (col == RGBColor::GREEN) {
99  return os << "green";
100  }
101  if (col == RGBColor::BLUE) {
102  return os << "blue";
103  }
104  if (col == RGBColor::YELLOW) {
105  return os << "yellow";
106  }
107  if (col == RGBColor::CYAN) {
108  return os << "cyan";
109  }
110  if (col == RGBColor::MAGENTA) {
111  return os << "magenta";
112  }
113  if (col == RGBColor::WHITE) {
114  return os << "white";
115  }
116  if (col == RGBColor::BLACK) {
117  return os << "black";
118  }
119  if (col == RGBColor::GREY) {
120  return os << "grey";
121  }
122  os << static_cast<int>(col.myRed) << ","
123  << static_cast<int>(col.myGreen) << ","
124  << static_cast<int>(col.myBlue);
125  if (col.myAlpha < 255) {
126  os << "," << static_cast<int>(col.myAlpha);
127  }
128  return os;
129 }
130 
131 
132 bool
134  return myRed == c.myRed && myGreen == c.myGreen && myBlue == c.myBlue && myAlpha == c.myAlpha;
135 }
136 
137 
138 bool
140  return myRed != c.myRed || myGreen != c.myGreen || myBlue != c.myBlue || myAlpha != c.myAlpha;
141 }
142 
143 
144 RGBColor
145 RGBColor::changedBrightness(const char change) {
146  const unsigned char red = static_cast<unsigned char>(MIN2(MAX2(myRed + change, 0), 255));
147  const unsigned char blue = static_cast<unsigned char>(MIN2(MAX2(myBlue + change, 0), 255));
148  const unsigned char green = static_cast<unsigned char>(MIN2(MAX2(myGreen + change, 0), 255));
149  return RGBColor(red, green, blue, myAlpha);
150 
151 }
152 
153 RGBColor
154 RGBColor::parseColor(std::string coldef) {
155  std::transform(coldef.begin(), coldef.end(), coldef.begin(), tolower);
156  if (coldef == "red") {
157  return RED;
158  }
159  if (coldef == "green") {
160  return GREEN;
161  }
162  if (coldef == "blue") {
163  return BLUE;
164  }
165  if (coldef == "yellow") {
166  return YELLOW;
167  }
168  if (coldef == "cyan") {
169  return CYAN;
170  }
171  if (coldef == "magenta") {
172  return MAGENTA;
173  }
174  if (coldef == "white") {
175  return WHITE;
176  }
177  if (coldef == "black") {
178  return BLACK;
179  }
180  if (coldef == "grey" || coldef == "gray") {
181  return GREY;
182  }
183  unsigned char r = 0;
184  unsigned char g = 0;
185  unsigned char b = 0;
186  unsigned char a = 255;
187  if (coldef[0] == '#') {
188  const int coldesc = TplConvert::_hex2int(coldef.c_str());
189  if (coldef.length() == 7) {
190  r = static_cast<unsigned char>((coldesc & 0xFF0000) >> 16);
191  g = static_cast<unsigned char>((coldesc & 0x00FF00) >> 8);
192  b = coldesc & 0xFF;
193  } else if (coldef.length() == 9) {
194  r = static_cast<unsigned char>((coldesc & 0xFF000000) >> 24);
195  g = static_cast<unsigned char>((coldesc & 0x00FF0000) >> 16);
196  b = static_cast<unsigned char>((coldesc & 0x0000FF00) >> 8);
197  a = coldesc & 0xFF;
198  } else {
199  throw EmptyData();
200  }
201  } else {
202  std::vector<std::string> st = StringTokenizer(coldef, ",").getVector();
203  if (st.size() == 3 || st.size() == 4) {
204  try {
205  r = static_cast<unsigned char>(TplConvert::_2int(st[0].c_str()));
206  g = static_cast<unsigned char>(TplConvert::_2int(st[1].c_str()));
207  b = static_cast<unsigned char>(TplConvert::_2int(st[2].c_str()));
208  if (st.size() == 4) {
209  a = static_cast<unsigned char>(TplConvert::_2int(st[3].c_str()));
210  }
211  if (r <= 1 && g <= 1 && b <= 1 && (st.size() == 3 || a <= 1)) {
212  throw NumberFormatException();
213  }
214  } catch (NumberFormatException&) {
215  r = static_cast<unsigned char>(TplConvert::_2SUMOReal(st[0].c_str()) * 255. + 0.5);
216  g = static_cast<unsigned char>(TplConvert::_2SUMOReal(st[1].c_str()) * 255. + 0.5);
217  b = static_cast<unsigned char>(TplConvert::_2SUMOReal(st[2].c_str()) * 255. + 0.5);
218  if (st.size() == 4) {
219  a = static_cast<unsigned char>(TplConvert::_2SUMOReal(st[3].c_str()) * 255. + 0.5);
220  }
221  }
222  } else {
223  throw EmptyData();
224  }
225  }
226  return RGBColor(r, g, b, a);
227 }
228 
229 
230 RGBColor
232  const std::string& coldef, const std::string& objecttype,
233  const char* objectid, bool report, bool& ok) {
234  UNUSED_PARAMETER(report);
235  try {
236  return parseColor(coldef);
237  } catch (NumberFormatException&) {
238  } catch (EmptyData&) {
239  }
240  ok = false;
241  std::ostringstream oss;
242  oss << "Attribute 'color' in definition of ";
243  if (objectid == 0) {
244  oss << "a ";
245  }
246  oss << objecttype;
247  if (objectid != 0) {
248  oss << " '" << objectid << "'";
249  }
250  oss << " is not a valid color.";
251  WRITE_ERROR(oss.str());
252  return RGBColor();
253 }
254 
255 
256 RGBColor
257 RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, SUMOReal weight) {
258  if (weight < 0) {
259  weight = 0;
260  }
261  if (weight > 1) {
262  weight = 1;
263  }
264  const unsigned char r = minColor.myRed + static_cast<char>((maxColor.myRed - minColor.myRed) * weight);
265  const unsigned char g = minColor.myGreen + static_cast<char>((maxColor.myGreen - minColor.myGreen) * weight);
266  const unsigned char b = minColor.myBlue + static_cast<char>((maxColor.myBlue - minColor.myBlue) * weight);
267  const unsigned char a = minColor.myAlpha + static_cast<char>((maxColor.myAlpha - minColor.myAlpha) * weight);
268  return RGBColor(r, g, b, a);
269 }
270 
271 
272 RGBColor
274  // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
275  // RGB are each returned on [0, 255].
276  //float h = HSV.H, s = HSV.S, v = HSV.V,
277  SUMOReal f;
278  h /= 60.;
279  int i;
280  //if (h == UNDEFINED) RETURN_RGB(v, v, v);
281  i = int(floor(h));
282  f = float(h - i);
283  if (!(i & 1)) {
284  f = 1 - f; // if i is even
285  }
286  const unsigned char m = static_cast<unsigned char>(v * (1 - s) * 255. + 0.5);
287  const unsigned char n = static_cast<unsigned char>(v * (1 - s * f) * 255. + 0.5);
288  const unsigned char vv = static_cast<unsigned char>(v * 255. + 0.5);
289  switch (i) {
290  case 6:
291  case 0:
292  return RGBColor(vv, n, m, 255);
293  case 1:
294  return RGBColor(n, vv, m, 255);
295  case 2:
296  return RGBColor(m, vv, n, 255);
297  case 3:
298  return RGBColor(m, n, vv, 255);
299  case 4:
300  return RGBColor(n, m, vv, 255);
301  case 5:
302  return RGBColor(vv, m, n, 255);
303  }
304  return RGBColor(255, 255, 255, 255);
305 }
306 
307 
308 /****************************************************************************/
309 
static int _hex2int(const E *const data)
Definition: TplConvert.h:128
static const RGBColor BLUE
Definition: RGBColor.h:190
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:154
~RGBColor()
Destructor.
Definition: RGBColor.cpp:81
static RGBColor fromHSV(SUMOReal h, SUMOReal s, SUMOReal v)
Converts the given hsv-triplet to rgb.
Definition: RGBColor.cpp:273
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
static const RGBColor WHITE
Definition: RGBColor.h:194
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:231
bool operator==(const RGBColor &c) const
Definition: RGBColor.cpp:133
T MAX2(T a, T b)
Definition: StdDefs.h:71
static RGBColor interpolate(const RGBColor &minColor, const RGBColor &maxColor, SUMOReal weight)
Interpolates between two colors.
Definition: RGBColor.cpp:257
static const RGBColor BLACK
Definition: RGBColor.h:195
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
bool operator!=(const RGBColor &c) const
Definition: RGBColor.cpp:139
static const RGBColor GREEN
Definition: RGBColor.h:189
static const RGBColor GREY
Definition: RGBColor.h:196
unsigned char myAlpha
Definition: RGBColor.h:207
unsigned char myRed
The color amounts.
Definition: RGBColor.h:207
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
assigns new values
Definition: RGBColor.cpp:85
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:198
std::ostream & operator<<(std::ostream &os, const RGBColor &col)
Definition: RGBColor.cpp:94
unsigned char myGreen
Definition: RGBColor.h:207
static const RGBColor MAGENTA
Definition: RGBColor.h:193
T MIN2(T a, T b)
Definition: StdDefs.h:65
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
static const RGBColor YELLOW
Definition: RGBColor.h:191
static int _2int(const E *const data)
Definition: TplConvert.h:114
static const RGBColor RED
Definition: RGBColor.h:188
static const RGBColor CYAN
Definition: RGBColor.h:192
RGBColor changedBrightness(const char change)
Returns a new color with altered brightness.
Definition: RGBColor.cpp:145
unsigned char myBlue
Definition: RGBColor.h:207
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:215
RGBColor()
Constructor.
Definition: RGBColor.cpp:69
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
static const std::string DEFAULT_COLOR_STRING
The string description of the default color.
Definition: RGBColor.h:202