Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 : /****************************************************************************/
14 : /// @file RGBColor.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Laura Bieker
19 : /// @date Sept 2002
20 : ///
21 : // A RGB-color definition
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <cmath>
26 : #include <cassert>
27 : #include <string>
28 : #include <sstream>
29 : #include <utils/common/RandHelper.h>
30 : #include <utils/common/StringTokenizer.h>
31 : #include <utils/common/ToString.h>
32 : #include <utils/common/StringUtils.h>
33 : #include <utils/common/MsgHandler.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 :
55 : const RGBColor RGBColor::DEFAULT_COLOR = RGBColor::YELLOW;
56 : const std::string RGBColor::DEFAULT_COLOR_STRING = toString(RGBColor::DEFAULT_COLOR);
57 :
58 : // random colors do not affect the simulation. No initialization is necessary
59 : SumoRNG RGBColor::myRNG("color");
60 :
61 : // ===========================================================================
62 : // method definitions
63 : // ===========================================================================
64 :
65 68285426 : RGBColor::RGBColor(bool valid) :
66 68285426 : myValid(valid) {}
67 :
68 :
69 36905499 : RGBColor::RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha, bool random) :
70 36905499 : myRed(red),
71 36905499 : myGreen(green),
72 36905499 : myBlue(blue),
73 36905499 : myAlpha(alpha),
74 36905499 : myRandom(random) {}
75 :
76 :
77 : unsigned char
78 75918118 : RGBColor::red() const {
79 75918118 : return myRed;
80 : }
81 :
82 :
83 : unsigned char
84 75918118 : RGBColor::green() const {
85 75918118 : return myGreen;
86 : }
87 :
88 :
89 : unsigned char
90 75918118 : RGBColor::blue() const {
91 75918118 : return myBlue;
92 : }
93 :
94 :
95 : unsigned char
96 142047679 : RGBColor::alpha() const {
97 142047679 : return myAlpha;
98 : }
99 :
100 :
101 : void
102 185 : RGBColor::set(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
103 185 : myRed = r;
104 185 : myGreen = g;
105 185 : myBlue = b;
106 185 : myAlpha = a;
107 185 : myValid = true;
108 185 : }
109 :
110 :
111 : void
112 746 : RGBColor::setAlpha(unsigned char alpha) {
113 746 : myAlpha = alpha;
114 746 : }
115 :
116 :
117 : void
118 5230 : RGBColor::setValid(const bool value) {
119 5230 : myValid = value;
120 5230 : }
121 :
122 :
123 : bool
124 485059 : RGBColor::isValid() const {
125 485059 : return myValid;
126 : }
127 :
128 :
129 : bool
130 8688 : RGBColor::isRandom() const {
131 8688 : return myRandom;
132 : }
133 :
134 :
135 : std::ostream&
136 105210 : operator<<(std::ostream& os, const RGBColor& col) {
137 105210 : if (col == RGBColor::RED) {
138 14628 : return os << "red";
139 : }
140 90582 : if (col == RGBColor::GREEN) {
141 13478 : return os << "green";
142 : }
143 77104 : if (col == RGBColor::BLUE) {
144 768 : return os << "blue";
145 : }
146 76336 : if (col == RGBColor::YELLOW) {
147 60331 : return os << "yellow";
148 : }
149 16005 : if (col == RGBColor::CYAN) {
150 108 : return os << "cyan";
151 : }
152 15897 : if (col == RGBColor::MAGENTA) {
153 224 : return os << "magenta";
154 : }
155 15673 : if (col == RGBColor::ORANGE) {
156 105 : return os << "orange";
157 : }
158 15568 : if (col == RGBColor::WHITE) {
159 1244 : return os << "white";
160 : }
161 14324 : if (col == RGBColor::BLACK) {
162 2367 : return os << "black";
163 : }
164 11957 : if (col == RGBColor::GREY) {
165 34 : return os << "grey";
166 : }
167 11923 : if (col == RGBColor::INVISIBLE) {
168 68 : return os << "invisible";
169 : }
170 11855 : os << static_cast<int>(col.myRed) << ","
171 11855 : << static_cast<int>(col.myGreen) << ","
172 11855 : << static_cast<int>(col.myBlue);
173 11855 : if (col.myAlpha < 255) {
174 886 : os << "," << static_cast<int>(col.myAlpha);
175 : }
176 : return os;
177 : }
178 :
179 :
180 : bool
181 504231 : RGBColor::operator==(const RGBColor& c) const {
182 504231 : return (myRed == c.myRed) && (myGreen == c.myGreen) && (myBlue == c.myBlue) && (myAlpha == c.myAlpha) && (myValid == c.myValid);
183 : }
184 :
185 :
186 : bool
187 8897722 : RGBColor::operator!=(const RGBColor& c) const {
188 8897722 : return (myRed != c.myRed) || (myGreen != c.myGreen) || (myBlue != c.myBlue) || (myAlpha != c.myAlpha) || (myValid != c.myValid);
189 : }
190 :
191 :
192 : RGBColor
193 0 : RGBColor::invertedColor() const {
194 : // obtain inverse colors
195 0 : const unsigned char r = (unsigned char)(255 - (int)myRed);
196 0 : const unsigned char g = (unsigned char)(255 - (int)myGreen);
197 0 : const unsigned char b = (unsigned char)(255 - (int)myBlue);
198 : // return inverted RBColor
199 0 : return RGBColor(r, g, b, myAlpha);
200 : }
201 :
202 :
203 : int
204 483851 : RGBColor::getBrightness() const {
205 483851 : return (int)((299 * (int)myRed + 587 * (int)myGreen + 114 * (int)myBlue) / 1000);
206 : }
207 :
208 :
209 : RGBColor
210 483851 : RGBColor::getContrastingColor() const {
211 483851 : if (getBrightness() > 180) {
212 0 : RGBColor result = changedBrightness(-150);
213 0 : result.setAlpha(255);
214 0 : return result;
215 : } else {
216 483851 : return RGBColor::WHITE;
217 : }
218 : }
219 :
220 :
221 : SumoRNG*
222 0 : RGBColor::getColorRNG() {
223 0 : return &myRNG;
224 : }
225 :
226 :
227 : RGBColor
228 3301485 : RGBColor::changedBrightness(int change, int toChange) const {
229 3301485 : const unsigned char red = (unsigned char)(MIN2(MAX2(myRed + change, 0), 255));
230 3301485 : const unsigned char blue = (unsigned char)(MIN2(MAX2(myBlue + change, 0), 255));
231 3301485 : const unsigned char green = (unsigned char)(MIN2(MAX2(myGreen + change, 0), 255));
232 3301485 : int changed = ((int)red - (int)myRed) + ((int)blue - (int)myBlue) + ((int)green - (int)myGreen);
233 3301485 : const RGBColor result(red, green, blue, myAlpha);
234 3301485 : if (changed == toChange * change) {
235 1717035 : return result;
236 1584450 : } else if (changed == 0) {
237 0 : return result;
238 : } else {
239 3180291 : const int maxedColors = (red != myRed + change ? 1 : 0) + (blue != myBlue + change ? 1 : 0) + (green != myGreen + change ? 1 : 0);
240 1584450 : if (maxedColors == 3) {
241 12 : return result;
242 : } else {
243 1584438 : const int toChangeNext = 3 - maxedColors;
244 1584438 : return result.changedBrightness((int)((toChange * change - changed) / toChangeNext), toChangeNext);
245 : }
246 : }
247 : }
248 :
249 :
250 : RGBColor
251 0 : RGBColor::changedAlpha(int change) const {
252 0 : int alpha = MIN2(MAX2((int)myAlpha + change, 0), 255);
253 0 : return RGBColor(myRed, myGreen, myBlue, (unsigned char)alpha);
254 : }
255 :
256 :
257 : RGBColor
258 0 : RGBColor::multiply(double factor) const {
259 0 : const unsigned char red = (unsigned char)floor(MIN2(MAX2(myRed * factor, 0.0), 255.0) + 0.5);
260 0 : const unsigned char blue = (unsigned char)floor(MIN2(MAX2(myBlue * factor, 0.0), 255.0) + 0.5);
261 0 : const unsigned char green = (unsigned char)floor(MIN2(MAX2(myGreen * factor, 0.0), 255.0) + 0.5);
262 0 : return RGBColor(red, green, blue, myAlpha);
263 : }
264 :
265 :
266 : RGBColor
267 93099 : RGBColor::parseColor(std::string coldef) {
268 93099 : coldef = StringUtils::to_lower_case(coldef);
269 93099 : if (coldef == "red") {
270 12624 : return RED;
271 : }
272 80475 : if (coldef == "green") {
273 9491 : return GREEN;
274 : }
275 70984 : if (coldef == "blue") {
276 13529 : return BLUE;
277 : }
278 57455 : if (coldef == "yellow") {
279 13483 : return YELLOW;
280 : }
281 43972 : if (coldef == "cyan") {
282 1648 : return CYAN;
283 : }
284 42324 : if (coldef == "magenta") {
285 913 : return MAGENTA;
286 : }
287 41411 : if (coldef == "orange") {
288 779 : return ORANGE;
289 : }
290 40632 : if (coldef == "white") {
291 467 : return WHITE;
292 : }
293 40165 : if (coldef == "black") {
294 188 : return BLACK;
295 : }
296 39977 : if (coldef == "grey" || coldef == "gray") {
297 845 : return GREY;
298 : }
299 39132 : if (coldef == "invisible") {
300 32 : return INVISIBLE;
301 : }
302 39100 : if (coldef == "random") {
303 0 : return fromHSV(RandHelper::rand(360, &myRNG),
304 : // prefer more saturated colors
305 : pow(RandHelper::rand(&myRNG), 0.3),
306 : // prefer brighter colors
307 0 : pow(RandHelper::rand(&myRNG), 0.3), true);
308 : }
309 : unsigned char r = 0;
310 : unsigned char g = 0;
311 : unsigned char b = 0;
312 : unsigned char a = 255;
313 39100 : if (coldef[0] == '#') {
314 884 : const int coldesc = StringUtils::hexToInt(coldef);
315 884 : if (coldef.length() == 7) {
316 884 : r = static_cast<unsigned char>((coldesc & 0xFF0000) >> 16);
317 884 : g = static_cast<unsigned char>((coldesc & 0x00FF00) >> 8);
318 884 : b = coldesc & 0xFF;
319 0 : } else if (coldef.length() == 9) {
320 0 : r = static_cast<unsigned char>((coldesc & 0xFF000000) >> 24);
321 0 : g = static_cast<unsigned char>((coldesc & 0x00FF0000) >> 16);
322 0 : b = static_cast<unsigned char>((coldesc & 0x0000FF00) >> 8);
323 0 : a = coldesc & 0xFF;
324 : } else {
325 0 : throw EmptyData();
326 : }
327 : } else {
328 114648 : std::vector<std::string> st = StringTokenizer(coldef, ",").getVector();
329 38216 : if (st.size() == 3 || st.size() == 4) {
330 : try {
331 38203 : r = static_cast<unsigned char>(StringUtils::toInt(st[0]));
332 21000 : g = static_cast<unsigned char>(StringUtils::toInt(st[1]));
333 20927 : b = static_cast<unsigned char>(StringUtils::toInt(st[2]));
334 20902 : if (st.size() == 4) {
335 1026 : a = static_cast<unsigned char>(StringUtils::toInt(st[3]));
336 : }
337 20902 : if (r <= 1 && g <= 1 && b <= 1 && (st.size() == 3 || a <= 1)) {
338 8224 : throw NumberFormatException("(color component) " + coldef);
339 : }
340 21413 : } catch (NumberFormatException&) {
341 21413 : r = static_cast<unsigned char>(StringUtils::toDouble(st[0]) * 255. + 0.5);
342 21412 : g = static_cast<unsigned char>(StringUtils::toDouble(st[1]) * 255. + 0.5);
343 21412 : b = static_cast<unsigned char>(StringUtils::toDouble(st[2]) * 255. + 0.5);
344 21412 : if (st.size() == 4) {
345 130 : a = static_cast<unsigned char>(StringUtils::toDouble(st[3]) * 255. + 0.5);
346 : }
347 21413 : }
348 : } else {
349 39 : throw FormatException("Invalid color definition '" + coldef + "'");
350 : }
351 38216 : }
352 39086 : return RGBColor(r, g, b, a);
353 : }
354 :
355 :
356 : bool
357 0 : RGBColor::isColor(std::string coldef) {
358 : // check if is defined using a previous defined color
359 0 : coldef = StringUtils::to_lower_case(coldef);
360 0 : if ((coldef == "red") || (coldef == "green") || (coldef == "blue") ||
361 0 : (coldef == "yellow") || (coldef == "cyan") || (coldef == "magenta") ||
362 0 : (coldef == "orange") || (coldef == "white") || (coldef == "black") ||
363 0 : (coldef == "grey") || (coldef == "gray") || (coldef == "invisible") ||
364 0 : (coldef == "random")) {
365 : return true;
366 : }
367 : // check if is defined using an hexadecimal value
368 0 : if (coldef[0] == '#') {
369 0 : if (StringUtils::isHex(coldef)) {
370 0 : return ((coldef.length() == 7) || (coldef.length() == 9));
371 : } else {
372 : return false;
373 : }
374 : }
375 : // Check definition by tuple of rgb or rgba
376 0 : std::vector<std::string> st = StringTokenizer(coldef, ",").getVector();
377 0 : if (st.size() == 3) {
378 0 : return StringUtils::isDouble(st[0]) && StringUtils::isDouble(st[1]) && StringUtils::isDouble(st[2]);
379 0 : } else if (st.size() == 4) {
380 0 : return StringUtils::isDouble(st[0]) && StringUtils::isDouble(st[1]) &&
381 0 : StringUtils::isDouble(st[2]) && StringUtils::isDouble(st[3]);
382 : } else {
383 : return false;
384 : }
385 0 : }
386 :
387 :
388 : RGBColor
389 3162 : RGBColor::parseColorReporting(
390 : const std::string& coldef, const std::string& objecttype,
391 : const char* objectid, bool report, bool& ok) {
392 : UNUSED_PARAMETER(report);
393 : try {
394 6324 : return parseColor(coldef);
395 0 : } catch (NumberFormatException&) {
396 0 : } catch (EmptyData&) {
397 0 : }
398 0 : ok = false;
399 0 : std::ostringstream oss;
400 0 : oss << "Attribute 'color' in definition of ";
401 0 : if (objectid == nullptr) {
402 0 : oss << "a ";
403 : }
404 : oss << objecttype;
405 0 : if (objectid != nullptr) {
406 0 : oss << " '" << objectid << "'";
407 : }
408 0 : oss << " is not a valid color.";
409 0 : WRITE_ERROR(oss.str());
410 0 : return RGBColor();
411 0 : }
412 :
413 :
414 : RGBColor
415 101886 : RGBColor::interpolate(const RGBColor& minColor, const RGBColor& maxColor, double weight) {
416 101886 : if (weight < 0) {
417 : weight = 0;
418 : }
419 101885 : if (weight > 1) {
420 : weight = 1;
421 : }
422 101886 : const unsigned char r = (unsigned char)((int)minColor.myRed + (((int)maxColor.myRed - (int)minColor.myRed) * weight));
423 101886 : const unsigned char g = (unsigned char)((int)minColor.myGreen + (((int)maxColor.myGreen - (int)minColor.myGreen) * weight));
424 101886 : const unsigned char b = (unsigned char)((int)minColor.myBlue + (((int)maxColor.myBlue - (int)minColor.myBlue) * weight));
425 101886 : const unsigned char a = (unsigned char)((int)minColor.myAlpha + (((int)maxColor.myAlpha - (int)minColor.myAlpha) * weight));
426 101886 : return RGBColor(r, g, b, a);
427 : }
428 :
429 :
430 : RGBColor
431 0 : RGBColor::fromHSV(double h, double s, double v, bool random) {
432 : h = MIN2(MAX2(h, 0.), 360.);
433 : s = MIN2(MAX2(s, 0.), 1.);
434 : v = MIN2(MAX2(v, 0.), 1.);
435 0 : h /= 60.;
436 0 : const int i = int(floor(h));
437 0 : double f = h - i;
438 0 : if (i % 2 == 0) {
439 0 : f = 1. - f;
440 : }
441 0 : const unsigned char m = static_cast<unsigned char>(v * (1 - s) * 255. + 0.5);
442 0 : const unsigned char n = static_cast<unsigned char>(v * (1 - s * f) * 255. + 0.5);
443 0 : const unsigned char vv = static_cast<unsigned char>(v * 255. + 0.5);
444 0 : switch (i) {
445 0 : case 0:
446 : case 6:
447 0 : return RGBColor(vv, n, m, 255, random);
448 0 : case 1:
449 0 : return RGBColor(n, vv, m, 255, random);
450 0 : case 2:
451 0 : return RGBColor(m, vv, n, 255, random);
452 0 : case 3:
453 0 : return RGBColor(m, n, vv, 255, random);
454 0 : case 4:
455 0 : return RGBColor(n, m, vv, 255, random);
456 0 : case 5:
457 0 : return RGBColor(vv, m, n, 255, random);
458 : }
459 0 : return RGBColor(255, 255, 255, 255, random);
460 : }
461 :
462 :
463 : RGBColor
464 0 : RGBColor::randomHue(double s, double v, bool random) {
465 0 : return fromHSV(RandHelper::rand(360, &myRNG), s, v, random);
466 : }
467 :
468 :
469 : /****************************************************************************/
|