/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file RGBColor.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Sept 200218///19// A RGB-color definition20/****************************************************************************/21#pragma once22#include <config.h>23#include <iostream>24#include <random>25#include <utils/common/RandHelper.h>26#include <utils/common/UtilExceptions.h>272829// ===========================================================================30// class definitions31// ===========================================================================32/**33* @class RGBColor34* The definition of a color in the RGB-space with an alpha channel.35* The cube is meant to lie between (0, 0, 0) and (255, 255, 255)36*/37class RGBColor {38public:39/** @brief Constructor40*/41RGBColor(bool valid = true);4243/** @brief Constructor44* @param[in] red The red component's value45* @param[in] green The green component's value46* @param[in] blue The blue component's value47*/48RGBColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255);4950/** @brief Returns the red-amount of the color51* @return The red component's value52*/53unsigned char red() const;5455/** @brief Returns the green-amount of the color56* @return The green component's value57*/58unsigned char green() const;5960/** @brief Returns the blue-amount of the color61* @return The blue component's value62*/63unsigned char blue() const;6465/** @brief Returns the alpha-amount of the color66* @return The alpha component's value67*/68unsigned char alpha() const;6970/** @brief assigns new values71* @param[in] r The red component's value72* @param[in] g The green component's value73* @param[in] b The blue component's value74* @param[in] a The alpha component's value75*/76void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a);7778/** @brief Sets a new alpha value79* @param[in] alpha The new value to use80*/81void setAlpha(unsigned char alpha);8283/// @brief set valid84void setValid(const bool value);8586/// @brief check if RGBColor is valid87bool isValid() const;8889/** @brief Returns a new color with altered brightness90* @param[in] change The absolute change applied to all channels (within bounds)91* @param[in] change The number of colors to change92* @return The new color93*/94RGBColor changedBrightness(int change, int toChange = 3) const;9596/// @brief Returns a new color with altered opacity97RGBColor changedAlpha(int change) const;9899/** @brief Returns a new color with altered brightness100* @param[in] factor The multiplicative change applied to all color channels (within bounds)101* @return The new color102*/103RGBColor multiply(double factor) const;104105/// @brief obtain inverted of current RGBColor106RGBColor invertedColor() const;107108/// @brief get color RNG109static SumoRNG* getColorRNG();110111/** @brief Parses a color information112*113* It is assumed that the color is stored as "<RED>,<GREEN>,<BLUE>"114* and each color is represented as a double.115* Alternatively the color can be stored as "<RED>,<GREEN>,<BLUE>,<ALPHA>"116* and each color is represented as an unsigned byte.117* @param[in] coldef The color definition to parse118* @return The parsed color119* @exception EmptyData If the definition has less than three entries120* @exception NumberFormatException If one of the components is not numeric121*/122static RGBColor parseColor(std::string coldef);123124/// @brief check if the given string can be parsed to color125static bool isColor(std::string coldef);126127/** @brief Parses a color information128*129* It is assumed that the color is stored as "<RED>,<GREEN>,<BLUE>"130* and each color is represented as a double.131* Alternatively the color can be stored as "<RED>,<GREEN>,<BLUE>,<ALPHA>"132* and each color is represented as an unsigned byte.133* @param[in] coldef The color definition to parse134* @param[in] objecttype The type of the currently parsed object135* @param[in] objectid The id of the currently parsed object136* @param[in] report Whether errors shall be reported137* @param[in, out] ok Whether parsing was successful138* @return The parsed color139* @exception EmptyData If the definition has less than three entries140* @exception NumberFormatException If one of the components is not numeric141*/142static RGBColor parseColorReporting(const std::string& coldef, const std::string& objecttype,143const char* objectid, bool report, bool& ok);144145/** @brief Interpolates between two colors146*147* The interpolated color is calculated as a weighted average of148* the RGB values of minColor and maxColor, giving weight to maxColor149* and 1-weight to minColor.150* @param[in] minColor The color to interpolate from151* @param[in] maxColor The color to interpolate to152* @param[in] weight The weight of the first color153* @return The interpolated color154*/155static RGBColor interpolate(const RGBColor& minColor, const RGBColor& maxColor, double weight);156157/** @brief Converts the given hsv-triplet to rgb, inspired by http://alvyray.com/Papers/CG/hsv2rgb.htm158* @param[in] h Hue (0-360)159* @param[in] s Saturation (0-1)160* @param[in] v Value (0-1)161* @return The color as RGB162*/163static RGBColor fromHSV(double h, double s, double v);164165/** @brief Return color with random hue166* @param[in] s Saturation (0-1)167* @param[in] v Value (0-1)168* @return The color as RGB169*/170static RGBColor randomHue(double s = 1, double v = 1);171172/** @brief Writes the color to the given stream173* @param[out] os The stream to write to174* @param[in] col The color to write175* @return The stream176*/177friend std::ostream& operator<<(std::ostream& os, const RGBColor& col);178179// @brief Equality operator180bool operator==(const RGBColor& c) const;181182// @brief Inequality operator183bool operator!=(const RGBColor& c) const;184185/// @brief named colors186/// @{187static const RGBColor RED;188static const RGBColor GREEN;189static const RGBColor BLUE;190static const RGBColor YELLOW;191static const RGBColor CYAN;192static const RGBColor MAGENTA;193static const RGBColor ORANGE;194static const RGBColor WHITE;195static const RGBColor BLACK;196static const RGBColor GREY;197static const RGBColor INVISIBLE;198/// @}199200/// @brief The default color (for vehicle types and vehicles)201static const RGBColor DEFAULT_COLOR;202203/// @brief The string description of the default color204static const std::string DEFAULT_COLOR_STRING;205206private:207/// @brief The color amounts208unsigned char myRed, myGreen, myBlue, myAlpha;209210/// @brief flag to check if color is valid211bool myValid;212213/// @brief A random number generator to generate random colors independent of other randomness214static SumoRNG myRNG;215};216217218