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.sourceforge.net/
13 // Copyright (C) 2001-2012 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>
40 #include <utils/common/StdDefs.h>
41 #include "RGBColor.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static member definitions
50 // ===========================================================================
51 const std::string RGBColor::DEFAULT_COLOR_STRING = "1,1,0";
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
59  : myRed(-1), myGreen(-1), myBlue(-1) {}
60 
61 
63  : myRed(red), myGreen(green), myBlue(blue) {}
64 
65 
67  : myRed(col.myRed), myGreen(col.myGreen), myBlue(col.myBlue) {}
68 
69 
71 
72 
73 void
75  myRed = r;
76  myGreen = g;
77  myBlue = b;
78 }
79 
80 
81 
82 std::ostream&
83 operator<<(std::ostream& os, const RGBColor& col) {
84  os
85  << col.myRed << ","
86  << col.myGreen << ","
87  << col.myBlue;
88  return os;
89 }
90 
91 
92 bool
93 RGBColor::operator==(const RGBColor& c) const {
94  return fabs(myRed - c.myRed) < 0.1 && fabs(myGreen - c.myGreen) < 0.1 && fabs(myBlue - c.myBlue) < 0.1;
95  //return myRed==c.myRed&&myGreen==c.myGreen&&myBlue==c.myBlue;
96 }
97 
98 
99 bool
101  return fabs(myRed - c.myRed) > 0.1 || fabs(myGreen - c.myGreen) > 0.1 || fabs(myBlue - c.myBlue) > 0.1;
102  //return myRed!=c.myRed||myGreen!=c.myGreen||myBlue!=c.myBlue;
103 }
104 
105 
106 RGBColor
108  SUMOReal red = MIN2(MAX2(myRed + change, (SUMOReal)0), (SUMOReal)1);
109  SUMOReal blue = MIN2(MAX2(myBlue + change, (SUMOReal)0), (SUMOReal)1);
110  SUMOReal green = MIN2(MAX2(myGreen + change, (SUMOReal)0), (SUMOReal)1);
111  return RGBColor(red, green, blue);
112 
113 }
114 
115 RGBColor
116 RGBColor::parseColor(const std::string& coldef) throw(EmptyData, NumberFormatException) {
117  StringTokenizer st(coldef, ",");
118  if (st.size() < 3) {
119  throw EmptyData();
120  }
121  SUMOReal r = TplConvert<char>::_2SUMOReal(st.next().c_str());
122  SUMOReal g = TplConvert<char>::_2SUMOReal(st.next().c_str());
123  SUMOReal b = TplConvert<char>::_2SUMOReal(st.next().c_str());
124  return RGBColor(r, g, b);
125 }
126 
127 
128 RGBColor
130  const std::string& coldef, const std::string& objecttype,
131  const char* objectid, bool report, bool& ok) {
132  UNUSED_PARAMETER(report);
133  try {
134  return parseColor(coldef);
135  } catch (NumberFormatException&) {
136  } catch (EmptyData&) {
137  }
138  ok = false;
139  std::ostringstream oss;
140  oss << "Attribute 'color' in definition of ";
141  if (objectid == 0) {
142  oss << "a ";
143  }
144  oss << objecttype;
145  if (objectid != 0) {
146  oss << " '" << objectid << "'";
147  }
148  oss << " is not a valid color.";
149  WRITE_ERROR(oss.str());
150  return RGBColor();
151 }
152 
153 
154 RGBColor
157 }
158 
159 
160 RGBColor
161 RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, SUMOReal weight) {
162  if (weight < 0) {
163  weight = 0;
164  }
165  if (weight > 1) {
166  weight = 1;
167  }
168  SUMOReal r = minColor.myRed + (maxColor.myRed - minColor.myRed) * weight;
169  SUMOReal g = minColor.myGreen + (maxColor.myGreen - minColor.myGreen) * weight;
170  SUMOReal b = minColor.myBlue + (maxColor.myBlue - minColor.myBlue) * weight;
171  return RGBColor(r, g, b);
172 }
173 
174 
175 RGBColor
177  // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
178  // RGB are each returned on [0, 1].
179  //float h = HSV.H, s = HSV.S, v = HSV.V,
180  float m, n, f;
181  h /= 60.;
182  int i;
183  //if (h == UNDEFINED) RETURN_RGB(v, v, v);
184  i = int(floor(h));
185  f = float(h - i);
186  if (!(i & 1)) {
187  f = 1 - f; // if i is even
188  }
189  m = float(v * (1 - s));
190  n = float(v * (1 - s * f));
191  switch (i) {
192  case 6:
193  case 0:
194  return RGBColor(v, n, m);
195  case 1:
196  return RGBColor(n, v, m);
197  case 2:
198  return RGBColor(m, v, n);
199  case 3:
200  return RGBColor(m, n, v);
201  case 4:
202  return RGBColor(n, m, v);
203  case 5:
204  return RGBColor(v, m, n);
205  }
206  return RGBColor(1, 1, 1);
207 }
208 
209 
210 /****************************************************************************/
211