Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/RGBColor.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
20
// A RGB-color definition
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
#include <iostream>
25
#include <random>
26
#include <utils/common/RandHelper.h>
27
#include <utils/common/UtilExceptions.h>
28
29
30
// ===========================================================================
31
// class definitions
32
// ===========================================================================
33
/**
34
* @class RGBColor
35
* The definition of a color in the RGB-space with an alpha channel.
36
* The cube is meant to lie between (0, 0, 0) and (255, 255, 255)
37
*/
38
class RGBColor {
39
public:
40
/** @brief Constructor
41
*/
42
RGBColor(bool valid = true);
43
44
/** @brief Constructor
45
* @param[in] red The red component's value
46
* @param[in] green The green component's value
47
* @param[in] blue The blue component's value
48
*/
49
RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255);
50
51
/** @brief Returns the red-amount of the color
52
* @return The red component's value
53
*/
54
unsigned char red() const;
55
56
/** @brief Returns the green-amount of the color
57
* @return The green component's value
58
*/
59
unsigned char green() const;
60
61
/** @brief Returns the blue-amount of the color
62
* @return The blue component's value
63
*/
64
unsigned char blue() const;
65
66
/** @brief Returns the alpha-amount of the color
67
* @return The alpha component's value
68
*/
69
unsigned char alpha() const;
70
71
/** @brief assigns new values
72
* @param[in] r The red component's value
73
* @param[in] g The green component's value
74
* @param[in] b The blue component's value
75
* @param[in] a The alpha component's value
76
*/
77
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
78
79
/** @brief Sets a new alpha value
80
* @param[in] alpha The new value to use
81
*/
82
void setAlpha(unsigned char alpha);
83
84
/// @brief set valid
85
void setValid(const bool value);
86
87
/// @brief check if RGBColor is valid
88
bool isValid() const;
89
90
/** @brief Returns a new color with altered brightness
91
* @param[in] change The absolute change applied to all channels (within bounds)
92
* @param[in] change The number of colors to change
93
* @return The new color
94
*/
95
RGBColor changedBrightness(int change, int toChange = 3) const;
96
97
/// @brief Returns a new color with altered opacity
98
RGBColor changedAlpha(int change) const;
99
100
/** @brief Returns a new color with altered brightness
101
* @param[in] factor The multiplicative change applied to all color channels (within bounds)
102
* @return The new color
103
*/
104
RGBColor multiply(double factor) const;
105
106
/// @brief obtain inverted of current RGBColor
107
RGBColor invertedColor() const;
108
109
/// @brief get color RNG
110
static SumoRNG* getColorRNG();
111
112
/** @brief Parses a color information
113
*
114
* It is assumed that the color is stored as "<RED>,<GREEN>,<BLUE>"
115
* and each color is represented as a double.
116
* Alternatively the color can be stored as "<RED>,<GREEN>,<BLUE>,<ALPHA>"
117
* and each color is represented as an unsigned byte.
118
* @param[in] coldef The color definition to parse
119
* @return The parsed color
120
* @exception EmptyData If the definition has less than three entries
121
* @exception NumberFormatException If one of the components is not numeric
122
*/
123
static RGBColor parseColor(std::string coldef);
124
125
/// @brief check if the given string can be parsed to color
126
static bool isColor(std::string coldef);
127
128
/** @brief Parses a color information
129
*
130
* It is assumed that the color is stored as "<RED>,<GREEN>,<BLUE>"
131
* and each color is represented as a double.
132
* Alternatively the color can be stored as "<RED>,<GREEN>,<BLUE>,<ALPHA>"
133
* and each color is represented as an unsigned byte.
134
* @param[in] coldef The color definition to parse
135
* @param[in] objecttype The type of the currently parsed object
136
* @param[in] objectid The id of the currently parsed object
137
* @param[in] report Whether errors shall be reported
138
* @param[in, out] ok Whether parsing was successful
139
* @return The parsed color
140
* @exception EmptyData If the definition has less than three entries
141
* @exception NumberFormatException If one of the components is not numeric
142
*/
143
static RGBColor parseColorReporting(const std::string& coldef, const std::string& objecttype,
144
const char* objectid, bool report, bool& ok);
145
146
/** @brief Interpolates between two colors
147
*
148
* The interpolated color is calculated as a weighted average of
149
* the RGB values of minColor and maxColor, giving weight to maxColor
150
* and 1-weight to minColor.
151
* @param[in] minColor The color to interpolate from
152
* @param[in] maxColor The color to interpolate to
153
* @param[in] weight The weight of the first color
154
* @return The interpolated color
155
*/
156
static RGBColor interpolate(const RGBColor& minColor, const RGBColor& maxColor, double weight);
157
158
/** @brief Converts the given hsv-triplet to rgb, inspired by http://alvyray.com/Papers/CG/hsv2rgb.htm
159
* @param[in] h Hue (0-360)
160
* @param[in] s Saturation (0-1)
161
* @param[in] v Value (0-1)
162
* @return The color as RGB
163
*/
164
static RGBColor fromHSV(double h, double s, double v);
165
166
/** @brief Return color with random hue
167
* @param[in] s Saturation (0-1)
168
* @param[in] v Value (0-1)
169
* @return The color as RGB
170
*/
171
static RGBColor randomHue(double s = 1, double v = 1);
172
173
/** @brief Writes the color to the given stream
174
* @param[out] os The stream to write to
175
* @param[in] col The color to write
176
* @return The stream
177
*/
178
friend std::ostream& operator<<(std::ostream& os, const RGBColor& col);
179
180
// @brief Equality operator
181
bool operator==(const RGBColor& c) const;
182
183
// @brief Inequality operator
184
bool operator!=(const RGBColor& c) const;
185
186
/// @brief named colors
187
/// @{
188
static const RGBColor RED;
189
static const RGBColor GREEN;
190
static const RGBColor BLUE;
191
static const RGBColor YELLOW;
192
static const RGBColor CYAN;
193
static const RGBColor MAGENTA;
194
static const RGBColor ORANGE;
195
static const RGBColor WHITE;
196
static const RGBColor BLACK;
197
static const RGBColor GREY;
198
static const RGBColor INVISIBLE;
199
/// @}
200
201
/// @brief The default color (for vehicle types and vehicles)
202
static const RGBColor DEFAULT_COLOR;
203
204
/// @brief The string description of the default color
205
static const std::string DEFAULT_COLOR_STRING;
206
207
private:
208
/// @brief The color amounts
209
unsigned char myRed, myGreen, myBlue, myAlpha;
210
211
/// @brief flag to check if color is valid
212
bool myValid;
213
214
/// @brief A random number generator to generate random colors independent of other randomness
215
static SumoRNG myRNG;
216
};
217
218