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

32 bit RGBA color. More...

#include <color.h>

Public Types

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

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

Static Public Member Functions

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

Protected Attributes

ComponentType m_rgba [4] {}
 RGBA value.
 

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]

EGT_NODISCARD constexpr ComponentType alpha ( ) const
inlineconstexpr

◆ alpha() [2/2]

constexpr void alpha ( ComponentType  a)
inlineconstexpr

◆ alphaf() [1/2]

EGT_NODISCARD constexpr float alphaf ( ) const
inlineconstexpr

◆ alphaf() [2/2]

constexpr void alphaf ( float  v)
inlineconstexpr

◆ blue() [1/2]

EGT_NODISCARD constexpr ComponentType blue ( ) const
inlineconstexpr

◆ blue() [2/2]

constexpr void blue ( ComponentType  b)
inlineconstexpr

◆ bluef() [1/2]

EGT_NODISCARD constexpr float bluef ( ) const
inlineconstexpr

◆ bluef() [2/2]

constexpr void bluef ( float  v)
inlineconstexpr

◆ css() [1/2]

EGT_NODISCARD static constexpr 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]

EGT_NODISCARD constexpr ComponentType green ( ) const
inlineconstexpr

◆ green() [2/2]

constexpr void green ( ComponentType  g)
inlineconstexpr

◆ greenf() [1/2]

EGT_NODISCARD constexpr 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]

template<class T >
constexpr Color operator* ( scalar) const
inlineconstexpr

Color operator.

◆ operator+() [1/2]

constexpr Color operator+ ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator+() [2/2]

template<class T >
constexpr Color operator+ ( scalar) const
inlineconstexpr

Color operator.

◆ operator-() [1/2]

constexpr Color operator- ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator-() [2/2]

template<class T >
constexpr Color operator- ( scalar) const
inlineconstexpr

Color operator.

◆ operator/() [1/2]

constexpr Color operator/ ( const Color rhs) const
inlineconstexpr

Color operator.

◆ operator/() [2/2]

template<class T >
constexpr Color operator/ ( scalar) const
inlineconstexpr

Color operator.

◆ operator=()

Color & operator= ( RGBAType  c)
inline

Assign an RGBA value.

Example

Color a = 0xRRGGBBAA;
32 bit RGBA color.
Definition color.h:41
Parameters
[in]cRGBA value.

◆ pixel16() [1/2]

EGT_NODISCARD constexpr uint16_t pixel16 ( ) const
inlineconstexpr

Get a 16 bit pixel representation of the Color.

◆ pixel16() [2/2]

static EGT_NODISCARD constexpr Color pixel16 ( uint16_t  c)
inlinestaticconstexpr

Create a Color from a 16 bit pixel representation.

◆ pixel24() [1/2]

EGT_NODISCARD constexpr RGBAType pixel24 ( ) const
inlineconstexpr

Get a 24 bit pixel representation of the Color.

◆ pixel24() [2/2]

static EGT_NODISCARD constexpr Color pixel24 ( RGBAType  c)
inlinestaticconstexpr

Create a Color from a 24 bit pixel representation.

◆ pixel32() [1/2]

EGT_NODISCARD constexpr RGBAType pixel32 ( ) const
inlineconstexpr

Get a 32 bit pixel representation of the Color.

◆ pixel32() [2/2]

static EGT_NODISCARD constexpr Color pixel32 ( RGBAType  c)
inlinestaticconstexpr

Create a Color from a 24 bit pixel representation.

◆ red() [1/2]

EGT_NODISCARD constexpr 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]

EGT_NODISCARD constexpr 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 EGT_NODISCARD constexpr 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 EGT_NODISCARD constexpr 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 EGT_NODISCARD constexpr 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()

EGT_NODISCARD constexpr 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()

EGT_NODISCARD constexpr 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.