1.8
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
Color Class Reference

32 bit RGBA color. More...

Public Types

using ComponentType = uint32_t
 Single component of Red/Green/Blue/Alpha type. More...
 
using RGBAType = uint32_t
 Red/Green/Blue/Alpha type. More...
 
using RGBType = uint32_t
 Red/Green/Blue type. More...
 

Public Member Functions

constexpr Color ()=default
 
constexpr Color (ComponentType r, ComponentType g, ComponentType b, ComponentType a=255) noexcept
 Create a color with the specified RGBA component values. More...
 
constexpr Color (const Color &color, ComponentType alpha) noexcept
 Create a color from an existing color, but with the specified alpha value. More...
 
constexpr Color (RGBAType c) noexcept
 Create a color with the specified RGBA value. More...
 
constexpr EGT_NODISCARD ComponentType alpha () const
 
constexpr void alpha (ComponentType a)
 
constexpr EGT_NODISCARD float alphaf () const
 
constexpr void alphaf (float v)
 
constexpr EGT_NODISCARD ComponentType blue () const
 
constexpr void blue (ComponentType b)
 
constexpr EGT_NODISCARD float bluef () const
 
constexpr void bluef (float v)
 
void get_hslf (float &h, float &s, float &l, float *alpha=nullptr)
 Get the hue, saturation, lightness, and alpha channel components of the color. More...
 
void get_hsvf (float &h, float &s, float &v, float *alpha=nullptr)
 Get the hue, saturation, value, and alpha channel components of the color. More...
 
void get_rgbaf (float &r, float &g, float &b, float *alpha=nullptr)
 Get the red, green, blue, and alpha channel components of the color. More...
 
constexpr EGT_NODISCARD ComponentType green () const
 
constexpr void green (ComponentType g)
 
constexpr EGT_NODISCARD float greenf () const
 
constexpr void greenf (float v)
 
EGT_NODISCARD std::string hex () const
 Return a string hex representation of the color. More...
 
EGT_NODISCARD Color hue (float h) const
 Create a new color by applying a hue value. More...
 
Color operator* (const Color &rhs) const
 Color operator. More...
 
template<class T >
constexpr Color operator* (T scalar) const
 Color operator. More...
 
constexpr Color operator+ (const Color &rhs) const
 Color operator. More...
 
template<class T >
constexpr Color operator+ (T scalar) const
 Color operator. More...
 
constexpr Color operator- (const Color &rhs) const
 Color operator. More...
 
template<class T >
constexpr Color operator- (T scalar) const
 Color operator. More...
 
constexpr Color operator/ (const Color &rhs) const
 Color operator. More...
 
template<class T >
constexpr Color operator/ (T scalar) const
 Color operator. More...
 
Coloroperator= (RGBAType c)
 Assign an RGBA value. More...
 
constexpr EGT_NODISCARD uint16_t pixel16 () const
 Get a 16 bit pixel representation of the Color. More...
 
constexpr EGT_NODISCARD RGBAType pixel24 () const
 Get a 24 bit pixel representation of the Color. More...
 
constexpr EGT_NODISCARD RGBAType pixel32 () const
 Get a 32 bit pixel representation of the Color. More...
 
constexpr EGT_NODISCARD ComponentType red () const
 Get RGBA component value as value from 0 to 255. More...
 
constexpr void red (ComponentType r)
 Set RGBA component value individually from 0 to 255. More...
 
constexpr EGT_NODISCARD float redf () const
 Get RGBA component value as a float from 0.0 to 1.0. More...
 
constexpr void redf (float v)
 Set RGBA component value as a float from 0.0 to 1.0. More...
 
constexpr EGT_NODISCARD Color shade (float factor) const
 Create a shade (darker) color based off this color given a factor. More...
 
constexpr EGT_NODISCARD Color tint (float factor) const
 Create a tint (lighter) color based off this color given a factor. More...
 

Static Public Member Functions

constexpr static EGT_NODISCARD Color css (const char *hex)
 
static EGT_NODISCARD Color css (const std::string &hex)
 Create a Color with a hex CSS string. More...
 
static EGT_NODISCARD Color hslf (float h, float s, float l, float alpha=1.0)
 Create a color from HSL values. More...
 
static EGT_NODISCARD Color hsvf (float h, float s, float v, float alpha=1.0)
 Create a color from HSV values. More...
 
static Color interp_hsl (const Color &a, const Color &b, float t)
 Perform linear interpolation between two colors in the HSL color space. More...
 
static Color interp_hsv (const Color &a, const Color &b, float t)
 Perform linear interpolation between two colors in the HSV color space. More...
 
static Color interp_rgba (const Color &a, const Color &b, float t)
 Perform linear interpolation between two colors in the RGB color space. More...
 
static constexpr EGT_NODISCARD Color pixel16 (uint16_t c)
 Create a Color from a 16 bit pixel representation. More...
 
static constexpr EGT_NODISCARD Color pixel24 (RGBAType c)
 Create a Color from a 24 bit pixel representation. More...
 
static constexpr EGT_NODISCARD Color pixel32 (RGBAType c)
 Create a Color from a 24 bit pixel representation. More...
 
static constexpr EGT_NODISCARD Color rgb (RGBType c, ComponentType alpha=255) noexcept
 Create a color from only a RGB value with separate alpha channel component. More...
 
static constexpr EGT_NODISCARD Color rgbaf (float r, float g, float b, float a=1.0)
 Create a color from float values. More...
 

Protected Attributes

ComponentType m_rgba [4] {}
 RGBA value. More...
 

Detailed Description

32 bit RGBA color.

This manages the definition of a color, internally stored as separate red, green, blue, and alpha components.

Colors

Member Typedef Documentation

◆ ComponentType

using ComponentType = uint32_t

Single component of Red/Green/Blue/Alpha type.

◆ RGBAType

using RGBAType = uint32_t

Red/Green/Blue/Alpha type.

◆ RGBType

using RGBType = uint32_t

Red/Green/Blue type.

Constructor & Destructor Documentation

◆ Color() [1/4]

constexpr Color ( )
constexprdefault

◆ Color() [2/4]

constexpr Color ( RGBAType  c)
inlineexplicitconstexprnoexcept

Create a color with the specified RGBA value.

Example

auto a = Color(0xRRGGBBAA);
constexpr Color()=default
Parameters
[in]cRGBA value.

◆ Color() [3/4]

constexpr Color ( const Color color,
ComponentType  alpha 
)
inlineconstexprnoexcept

Create a color from an existing color, but with the specified alpha value.

Parameters
[in]colorPre-existing color.
[in]alphaSpecific alpha value.

◆ Color() [4/4]

constexpr Color ( ComponentType  r,
ComponentType  g,
ComponentType  b,
ComponentType  a = 255 
)
inlineexplicitconstexprnoexcept

Create a color with the specified RGBA component values.

Parameters
[in]rRed component in range 0 - 255.
[in]gGreen component in range 0 - 255.
[in]bBlue component in range 0 - 255.
[in]aAlpha component in range 0 - 255.

Member Function Documentation

◆ alpha() [1/2]

constexpr EGT_NODISCARD ComponentType alpha ( ) const
inlineconstexpr

◆ alpha() [2/2]

constexpr void alpha ( ComponentType  a)
inlineconstexpr

◆ alphaf() [1/2]

constexpr EGT_NODISCARD float alphaf ( ) const
inlineconstexpr

◆ alphaf() [2/2]

constexpr void alphaf ( float  v)
inlineconstexpr

◆ blue() [1/2]

constexpr EGT_NODISCARD ComponentType blue ( ) const
inlineconstexpr

◆ blue() [2/2]

constexpr void blue ( ComponentType  b)
inlineconstexpr

◆ bluef() [1/2]

constexpr EGT_NODISCARD float bluef ( ) const
inlineconstexpr

◆ bluef() [2/2]

constexpr void bluef ( float  v)
inlineconstexpr

◆ css() [1/2]

constexpr static EGT_NODISCARD Color css ( const char *  hex)
inlinestaticconstexpr

◆ css() [2/2]

static EGT_NODISCARD Color css ( const std::string &  hex)
inlinestatic

Create a Color with a hex CSS string.

For example, the string #0074D9 can be used to specify a blue-like color from red, green, blue values. Alpha is not included.

Example

auto a = Color::css("#0074D9");
auto b = Color::css("0074D9");
static EGT_NODISCARD Color css(const std::string &hex)
Create a Color with a hex CSS string.
Definition: color.h:272

◆ get_hslf()

void get_hslf ( float &  h,
float &  s,
float &  l,
float *  alpha = nullptr 
)

Get the hue, saturation, lightness, and alpha channel components of the color.

Parameters
[out]hHue component in range 0 - 1.
[out]sSaturation component in range 0 - 1.
[out]lLightness component in range 0 - 1.
[out]alphaAlpha component in range 0 - 1.

◆ get_hsvf()

void get_hsvf ( float &  h,
float &  s,
float &  v,
float *  alpha = nullptr 
)

Get the hue, saturation, value, and alpha channel components of the color.

Parameters
[out]hHue component in range 0 - 1.
[out]sSaturation component in range 0 - 1.
[out]vValue component in range 0 - 1.
[out]alphaAlpha component in range 0 - 1.

◆ get_rgbaf()

void get_rgbaf ( float &  r,
float &  g,
float &  b,
float *  alpha = nullptr 
)

Get the red, green, blue, and alpha channel components of the color.

Parameters
[out]rRed component in range 0 - 1.
[out]gGreen component in range 0 - 1.
[out]bBlue component in range 0 - 1.
[out]alphaAlpha component in range 0 - 1.

◆ green() [1/2]

constexpr EGT_NODISCARD ComponentType green ( ) const
inlineconstexpr

◆ green() [2/2]

constexpr void green ( ComponentType  g)
inlineconstexpr

◆ greenf() [1/2]

constexpr EGT_NODISCARD float greenf ( ) const
inlineconstexpr

◆ greenf() [2/2]

constexpr void greenf ( float  v)
inlineconstexpr

◆ hex()

EGT_NODISCARD std::string hex ( ) const

Return a string hex representation of the color.

◆ hslf()

static EGT_NODISCARD Color hslf ( float  h,
float  s,
float  l,
float  alpha = 1.0 
)
static

Create a color from HSL values.

Parameters
[in]hHue component in range 0 - 1.
[in]sSaturation component in range 0 - 1.
[in]lLightness component in range 0 - 1.
[in]alphaAlpha component in range 0 - 1.

◆ hsvf()

static EGT_NODISCARD Color hsvf ( float  h,
float  s,
float  v,
float  alpha = 1.0 
)
static

Create a color from HSV values.

Parameters
[in]hHue component in range 0 - 1.
[in]sSaturation component in range 0 - 1.
[in]vValue component in range 0 - 1.
[in]alphaAlpha component in range 0 - 1.

◆ hue()

EGT_NODISCARD Color hue ( float  h) const
inline

Create a new color by applying a hue value.

◆ interp_hsl()

static Color interp_hsl ( const Color a,
const Color b,
float  t 
)
static

Perform linear interpolation between two colors in the HSL color space.

Parameters
aThe first color.
bThe second color.
tValue from 0 to 1.

◆ interp_hsv()

static Color interp_hsv ( const Color a,
const Color b,
float  t 
)
static

Perform linear interpolation between two colors in the HSV color space.

Parameters
aThe first color.
bThe second color.
tValue from 0 to 1.

◆ interp_rgba()

static Color interp_rgba ( const Color a,
const Color b,
float  t 
)
static

Perform linear interpolation between two colors in the RGB color space.

Parameters
aThe first color.
bThe second color.
tValue from 0 to 1.
Note
The interpolation happens in the RGB color space with the formula c = a + (b - a) * t.

◆ operator*() [1/2]

Color operator* ( const Color rhs) const
inline

Color operator.

◆ operator*() [2/2]

constexpr Color operator* ( scalar) const
inlineconstexpr

Color operator.

◆ operator+() [1/2]

constexpr Color operator+ ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator+() [2/2]

constexpr Color operator+ ( scalar) const
inlineconstexpr

Color operator.

◆ operator-() [1/2]

constexpr Color operator- ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator-() [2/2]

constexpr Color operator- ( scalar) const
inlineconstexpr

Color operator.

◆ operator/() [1/2]

constexpr Color operator/ ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator/() [2/2]

constexpr Color operator/ ( scalar) const
inlineconstexpr

Color operator.

◆ operator=()

Color& operator= ( RGBAType  c)
inline

Assign an RGBA value.

Example

Color a = 0xRRGGBBAA;
Parameters
[in]cRGBA value.

◆ pixel16() [1/2]

constexpr EGT_NODISCARD uint16_t pixel16 ( ) const
inlineconstexpr

Get a 16 bit pixel representation of the Color.

◆ pixel16() [2/2]

static constexpr EGT_NODISCARD Color pixel16 ( uint16_t  c)
inlinestaticconstexpr

Create a Color from a 16 bit pixel representation.

◆ pixel24() [1/2]

constexpr EGT_NODISCARD RGBAType pixel24 ( ) const
inlineconstexpr

Get a 24 bit pixel representation of the Color.

◆ pixel24() [2/2]

static constexpr EGT_NODISCARD Color pixel24 ( RGBAType  c)
inlinestaticconstexpr

Create a Color from a 24 bit pixel representation.

◆ pixel32() [1/2]

constexpr EGT_NODISCARD RGBAType pixel32 ( ) const
inlineconstexpr

Get a 32 bit pixel representation of the Color.

◆ pixel32() [2/2]

static constexpr EGT_NODISCARD Color pixel32 ( RGBAType  c)
inlinestaticconstexpr

Create a Color from a 24 bit pixel representation.

◆ red() [1/2]

constexpr EGT_NODISCARD ComponentType red ( ) const
inlineconstexpr

Get RGBA component value as value from 0 to 255.

◆ red() [2/2]

constexpr void red ( ComponentType  r)
inlineconstexpr

Set RGBA component value individually from 0 to 255.

◆ redf() [1/2]

constexpr EGT_NODISCARD float redf ( ) const
inlineconstexpr

Get RGBA component value as a float from 0.0 to 1.0.

◆ redf() [2/2]

constexpr void redf ( float  v)
inlineconstexpr

Set RGBA component value as a float from 0.0 to 1.0.

◆ rgb()

static constexpr EGT_NODISCARD Color rgb ( RGBType  c,
ComponentType  alpha = 255 
)
inlinestaticconstexprnoexcept

Create a color from only a RGB value with separate alpha channel component.

Example

auto a = Color::rgb(0xRRGGBB);
static constexpr EGT_NODISCARD Color rgb(RGBType c, ComponentType alpha=255) noexcept
Create a color from only a RGB value with separate alpha channel component.
Definition: color.h:237
Parameters
[in]cRGB value.
[in]alphaAlpha component in range 0 - 255.

◆ rgbaf()

static constexpr EGT_NODISCARD Color rgbaf ( float  r,
float  g,
float  b,
float  a = 1.0 
)
inlinestaticconstexpr

Create a color from float values.

Parameters
[in]rComponent value as a float from 0.0 to 1.0.
[in]gComponent value as a float from 0.0 to 1.0.
[in]bComponent value as a float from 0.0 to 1.0.
[in]aComponent value as a float from 0.0 to 1.0.

◆ shade()

constexpr EGT_NODISCARD Color shade ( float  factor) const
inlineconstexpr

Create a shade (darker) color based off this color given a factor.

The larger the factor, the darker the shade.

Parameters
[in]factorValue from 0.0 to 1.0.

◆ tint()

constexpr EGT_NODISCARD Color tint ( float  factor) const
inlineconstexpr

Create a tint (lighter) color based off this color given a factor.

The larger the factor the lighter the tint.

Parameters
[in]factorValue from 0.0 to 1.0.

Member Data Documentation

◆ m_rgba

ComponentType m_rgba[4] {}
protected

RGBA value.

red[0], green[1], blue[2], alpha[3]

Note
std::valarray would have made more sense due to all the scalar operations, but we lose constexpr with it.