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