SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUIColorScheme.h
Go to the documentation of this file.
1 /****************************************************************************/
8 //
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef GUIColorScheme_h
22 #define GUIColorScheme_h
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 <cassert>
35 #include <vector>
36 #include <utils/common/RGBColor.h>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
48 public:
50  GUIColorScheme(const std::string& name, const RGBColor& baseColor,
51  const std::string& colName = "", const bool isFixed = false)
53  addColor(baseColor, 0, colName);
54  }
55 
56  void setThreshold(const size_t pos, const SUMOReal threshold) {
57  myThresholds[pos] = threshold;
58  }
59 
60  void setColor(const size_t pos, const RGBColor& color) {
61  myColors[pos] = color;
62  }
63 
64  bool setColor(const std::string& name, const RGBColor& color) {
65  std::vector<std::string>::iterator nameIt = myNames.begin();
66  std::vector<RGBColor>::iterator colIt = myColors.begin();
67  for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
68  if (*nameIt == name) {
69  (*colIt) = color;
70  return true;
71  }
72  }
73  return false;
74  }
75 
76  unsigned int addColor(const RGBColor& color, const SUMOReal threshold, const std::string& name = "") {
77  std::vector<RGBColor>::iterator colIt = myColors.begin();
78  std::vector<SUMOReal>::iterator threshIt = myThresholds.begin();
79  std::vector<std::string>::iterator nameIt = myNames.begin();
80  unsigned int pos = 0;
81  while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
82  ++threshIt;
83  ++colIt;
84  ++nameIt;
85  pos++;
86  }
87  myColors.insert(colIt, color);
88  myThresholds.insert(threshIt, threshold);
89  myNames.insert(nameIt, name);
90  return pos;
91  }
92 
93  void removeColor(const size_t pos) {
94  assert(pos < myColors.size());
95  myColors.erase(myColors.begin() + pos);
96  myThresholds.erase(myThresholds.begin() + pos);
97  myNames.erase(myNames.begin() + pos);
98  }
99 
100  void clear() {
101  myColors.clear();
102  myThresholds.clear();
103  myNames.clear();
104  }
105 
106  const RGBColor getColor(const SUMOReal value) const {
107  if (myColors.size() == 1 || value < myThresholds.front()) {
108  return myColors.front();
109  }
110  std::vector<RGBColor>::const_iterator colIt = myColors.begin() + 1;
111  std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin() + 1;
112  while (threshIt != myThresholds.end() && (*threshIt) <= value) {
113  ++threshIt;
114  ++colIt;
115  }
116  if (threshIt == myThresholds.end()) {
117  return myColors.back();
118  }
119  if (!myIsInterpolated) {
120  return *(colIt - 1);
121  }
122  SUMOReal lowVal = *(threshIt - 1);
123  return RGBColor::interpolate(*(colIt - 1), *colIt, (value - lowVal) / ((*threshIt) - lowVal));
124  }
125 
126  void setInterpolated(const bool interpolate, SUMOReal interpolationStart = 0.f) {
127  myIsInterpolated = interpolate;
128  if (interpolate) {
129  myThresholds[0] = interpolationStart;
130  }
131  }
132 
133  const std::string& getName() const {
134  return myName;
135  }
136 
137  const std::vector<RGBColor>& getColors() const {
138  return myColors;
139  }
140 
141  const std::vector<SUMOReal>& getThresholds() const {
142  return myThresholds;
143  }
144 
145  bool isInterpolated() const {
146  return myIsInterpolated;
147  }
148 
149  const std::vector<std::string>& getNames() const {
150  return myNames;
151  }
152 
153  bool isFixed() const {
154  return myIsFixed;
155  }
156 
157  bool allowsNegativeValues() const {
158  return myAllowNegativeValues;
159  }
160 
161  void setAllowsNegativeValues(bool value) {
162  myAllowNegativeValues = value;
163  }
164 
165  void save(OutputDevice& dev) const {
166  dev << " <colorScheme name=\"" << myName;
167  if (!myIsFixed) {
168  dev << "\" interpolated=\"" << myIsInterpolated;
169  }
170  dev << "\">\n";
171  std::vector<RGBColor>::const_iterator colIt = myColors.begin();
172  std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin();
173  std::vector<std::string>::const_iterator nameIt = myNames.begin();
174  while (threshIt != myThresholds.end()) {
175  dev << " <entry color=\"" << (*colIt);
176  if (!myIsFixed) {
177  dev << "\" threshold=\"" << (*threshIt);
178  }
179  if ((*nameIt) != "") {
180  dev << "\" name=\"" << (*nameIt);
181  }
182  dev << "\"/>\n";
183  ++threshIt;
184  ++colIt;
185  ++nameIt;
186  }
187  dev << " </colorScheme>\n";
188  }
189 
190  bool operator==(const GUIColorScheme& c) const {
192  }
193 
194 private:
195  std::string myName;
196  std::vector<RGBColor> myColors;
197  std::vector<SUMOReal> myThresholds;
199  std::vector<std::string> myNames;
200  bool myIsFixed;
202 
203 };
204 
205 #endif
206 
207 /****************************************************************************/