Qualpal
Repositories

qualpal::colors::RGB

C++ · cpp · 4.1.0

class qualpal::colors::RGB

RGB color representation with values in [0,1] range.

Represents a color in the standard Red-Green-Blue (RGB) color space. All channel values are normalized to the range [0, 1].

Provides conversion constructors for HSL, XYZ, Lab, and LCHab color spaces. Also supports construction from hexadecimal color strings in "#RRGGBB" or "#RGB" format.

// Create RGB from normalized values
RGB red(1.0, 0.0, 0.0);

// Create from hex string (6-digit and 3-digit)
RGB blue("#0000ff");
RGB magenta("#f0f");

// Convert to hex
std::string hex = red.hex(); // "#ff0000"

Throws

std::invalid_argument

if hex string format is invalid.

See also

HSL, XYZ, Lab, LCHab for conversions.

Methods

RGB

RGB

RGB()

Default constructor initializes RGB to black (0,0,0).

RGB

RGB(const HSL & hsl)

Construct RGB from HSL color.

Parameters

hsl

HSL color to convert from

RGB

RGB(const LCHab & lch)

Construct RGB from LCHab color.

Parameters

lch

LCHab color to convert from

RGB

RGB(const Lab & lab)

Construct RGB from Lab color.

Parameters

lab

Lab color to convert from

RGB

RGB(const XYZ & xyz)

Construct RGB from XYZ color.

Parameters

xyz

XYZ color to convert from

RGB

inline RGB(const char * hex)

Construct RGB from hexadecimal color string literal.

Parameters

hex

Hex color string in format "#RRGGBB" or "#RGB"

Throws

std::invalid_argument

if hex format is invalid

RGB

RGB(const double r, const double g, const double b)

Construct RGB from normalized values.

Parameters

r

Red component [0,1]

g

Green component [0,1]

b

Blue component [0,1]

RGB

RGB(const std::array<double, 3> rgb)

Construct RGB from array of normalized values.

Parameters

rgb

Array containing [r, g, b] values in [0,1]

RGB

RGB(const std::string & hex)

Construct RGB from hexadecimal color string.

Parameters

hex

Hex color string in format "#RRGGBB" or "#RGB"

Throws

std::invalid_argument

if hex format is invalid

RGB red("#ff0000");     // 6-digit format
RGB blue("#00f");       // 3-digit format

b

inline double b() const

Get blue component [0,1].

g

inline double g() const

Get green component [0,1].

hex

std::string hex() const

Convert RGB to hexadecimal string.

Returns

Hex color string in format "#rrggbb"

RGB red(1.0, 0.0, 0.0);
std::string hex = red.hex(); // "#ff0000"

operator!=

inline bool operator!=(const RGB & other) const

Inequality operator for RGB colors.

Parameters

other

Other RGB color to compare with

Returns

true if RGB colors are not equal, false otherwise

operator==

inline bool operator==(const RGB & other) const

Equality operator for RGB colors.

Parameters

other

Other RGB color to compare with

Returns

true if both RGB colors are equal, false otherwise

r

inline double r() const

Get red component [0,1].