Skip to content

Color ​

Represents an RGB color with floating-point components.

This class stores color values as three floating-point channels (red, green, blue), normalized to the range . It does not include an alpha component. Color supports construction from floats, hexadecimal values, and spans, as well as basic arithmetic operations.

Construction ​

Constructors ​


Color() constexpr ​

Constructs a color from individual RGB components.

The components are assumed to be in linear color space.

cpp
Color::Color(float r, float g, float b);
ParameterDescription
rRed component.
gGreen component.
bBlue component.

Color() constexpr ​

Constructs a color from a span of three float values.

cpp
Color::Color(std::span<float> color);
ParameterDescription
colorSpan containing red, green, and blue components.

Color() constexpr ​

Constructs a color from a hexadecimal value.

The format is 0xRRGGBB. The hexadecimal value is interpreted as an sRGB color (as commonly used in CSS and color pickers) and is converted to linear RGB for storage in this Color instance.

cpp
Color::Color(unsigned int hex);
ParameterDescription
hexHexadecimal color code in 0xRRGGBB format (sRGB).

Properties ​

b float ​

Blue channel in .

cpp
float b {1.0f};

g float ​

Green channel in .

cpp
float g {1.0f};

r float ​

Red channel in .

cpp
float r {1.0f};

Functions ​

operator*=() Color& ​

Multiplies the color component-wise by another color in-place.

cpp
Color& Color::operator* =(const Color& c);
ParameterDescription
cColor to multiply.

operator*=() Color& ​

Multiplies the color by a scalar in-place.

cpp
Color& Color::operator* =(float n);
ParameterDescription
nScalar value.

operator=() Color& ​

Assigns a new color from a hexadecimal value.

cpp
Color& Color::operator=(unsigned int hex);
ParameterDescription
hexHexadecimal color code in 0xRRGGBB format.

operator[]() float& ​

Accesses a channel by index.

cpp
float& Color::operator[](int i);
ParameterDescription
iIndex: 0 → r, 1 → g, 2 → b.

operator[]() float ​

Accesses a channel by index.

cpp
float Color::operator[](int i) const;
ParameterDescription
iIndex: 0 → r, 1 → g, 2 → b.

Lerp() Color ​

Linearly interpolates between two colors.

cpp
Color Lerp(const Color& a, const Color& b, float f);
ParameterDescription
aStart color.
bEnd color.
fInterpolation factor in .

operator*() Color ​

Multiplies two colors component-wise.

cpp
Color operator* (const Color& a, const Color& b);
ParameterDescription
aFirst color.
bSecond color.

operator*() Color ​

Multiplies a color by a scalar.

cpp
Color operator* (const Color& v, float n);
ParameterDescription
vInput color.
nScalar value.

operator*() Color ​

Multiplies a scalar by a color.

cpp
Color operator* (float n, const Color& v);
ParameterDescription
nScalar value.
vInput color.

operator+() Color ​

Adds two colors component-wise.

cpp
Color operator+(const Color& a, const Color& b);
ParameterDescription
aFirst color.
bSecond color.

operator-() Color ​

Subtracts one color from another, clamped at zero.

cpp
Color operator-(const Color& a, const Color& b);
ParameterDescription
aFirst color.
bColor to subtract.

Released under the MIT License.