commons 0.1.5
Header-only C++23 library of common/shared types for the C++ libraries
Loading...
Searching...
No Matches
comms::Color Struct Reference

An 8-bit-per-channel RGBA color. Default is opaque black. More...

#include <color.hpp>

Public Member Functions

constexpr Color fade (const f64 opacity_unit) const
 Set the alpha channel to the given [0, 1] opacity.
 
constexpr Color opacity (const f64 opacity_unit) const
 Alias for fade — set the alpha channel to a [0, 1] opacity.
 
constexpr Color lighten (const f64 amount) const
 Increase lightness by amount (unit delta, clamped).
 
constexpr Color darken (const f64 amount) const
 Decrease lightness by amount (unit delta, clamped).
 
constexpr Color saturate (const f64 amount) const
 Increase saturation by amount (unit delta, clamped).
 
constexpr Color desaturate (const f64 amount) const
 Decrease saturation by amount (unit delta, clamped).
 
constexpr Color rotate_hue (const f64 degrees) const
 Rotate the hue by degrees around the color wheel.
 
constexpr Color recolor (const Color other) const
 Adopt the hue and saturation of other, keeping this color's lightness and alpha.
 
constexpr Color tint (const f64 amount) const
 Alias for lighten.
 
constexpr Color shade (const f64 amount) const
 Alias for darken.
 
constexpr Color tone (const f64 amount) const
 Alias for desaturate.
 
constexpr Color mix (const Color other, f64 t) const
 Linearly interpolate toward other by t (clamped to [0, 1]), including the alpha channel.
 
constexpr Color blend_over (const Color bg) const
 Alpha-composite this color (source) over bg using the source-over operator.
 
constexpr Color invert () const
 Invert the RGB channels, keeping alpha.
 
constexpr Color grayscale () const
 Convert to gray using the Rec.601 luma weights (0.299 R, 0.587 G, 0.114 B), keeping alpha.
 
constexpr Color complement () const
 The hue-opposite color (hue rotated 180°).
 
constexpr f64 relative_luminance () const
 Relative luminance per WCAG 2.x (sRGB linearized, then the 0.2126 / 0.7152 / 0.0722 weights).
 
constexpr f64 contrast_ratio (const Color other) const
 WCAG contrast ratio against other, in [1, 21]. Symmetric.
 
constexpr bool is_light () const
 True when black text is at least as readable on this color as white.
 
constexpr Color readable_text_color () const
 Black or white, whichever has the higher contrast against this color — the better foreground text color.
 
constexpr bool has_readable_contrast (const Color text) const
 True when text over this background meets WCAG AA for normal text (contrast ratio >= 4.5).
 
constexpr std::array< Color, 2 > split_complementary () const
 The two split-complementary colors (hue ±150°).
 
constexpr std::array< Color, 3 > triadic () const
 This color plus the two triadic colors (hue +120°, +240°).
 
constexpr std::array< Color, 3 > analogous (const f64 angle=30.0) const
 This color flanked by its two analogous neighbors (hue ∓angle).
 
std::string to_hex_string () const
 Canonical hex: #RRGGBB when opaque, #RRGGBBAA otherwise.
 
std::string to_css_rgb_string () const
 CSS space-separated form, e.g. "rgb(99 102 241)".
 
std::string to_css_rgba_string () const
 CSS form with alpha, e.g.
 

Static Public Member Functions

static constexpr Color from_rgb_int (const u32 v)
 0xRRGGBB (alpha forced opaque).
 
static constexpr Color from_rgba_int (const u32 v)
 0xRRGGBBAA.
 
static constexpr Color from_argb_int (const u32 v)
 0xAARRGGBB.
 
static constexpr std::optional< Colorparse_hex (std::string_view s)
 Parse a hex color: #rgb, #rgba, #rrggbb, or #rrggbbaa (the leading # is optional).
 
static constexpr std::optional< Colorparse (std::string_view s)
 Parse any supported textual color: hex (see parse_hex), CSS-functional rgb()/rgba()/hsl()/hsla(), or a CSS named color.
 

Public Attributes

u8 r = 0
 Red channel, [0, 255].
 
u8 g = 0
 Green channel, [0, 255].
 
u8 b = 0
 Blue channel, [0, 255].
 
u8 a = 255
 Alpha channel, [0, 255] (255 = opaque).
 

Detailed Description

An 8-bit-per-channel RGBA color. Default is opaque black.

Member Function Documentation

◆ blend_over()

constexpr Color comms::Color::blend_over ( const Color  bg) const
inlineconstexpr

Alpha-composite this color (source) over bg using the source-over operator.

The result's alpha is the combined coverage.

◆ mix()

constexpr Color comms::Color::mix ( const Color  other,
f64  t 
) const
inlineconstexpr

Linearly interpolate toward other by t (clamped to [0, 1]), including the alpha channel.

t == 0 is this color, t == 1 is other.

◆ parse()

constexpr std::optional< Color > comms::Color::parse ( std::string_view  s)
staticconstexpr

Parse any supported textual color: hex (see parse_hex), CSS-functional rgb()/rgba()/hsl()/hsla(), or a CSS named color.

Returns nullopt if none match. Defined out-of-line below, once CssColors is complete.

◆ parse_hex()

static constexpr std::optional< Color > comms::Color::parse_hex ( std::string_view  s)
inlinestaticconstexpr

Parse a hex color: #rgb, #rgba, #rrggbb, or #rrggbbaa (the leading # is optional).

Returns nullopt on any malformed input.

◆ relative_luminance()

constexpr f64 comms::Color::relative_luminance ( ) const
inlineconstexpr

Relative luminance per WCAG 2.x (sRGB linearized, then the 0.2126 / 0.7152 / 0.0722 weights).

Alpha is ignored.

◆ to_css_rgba_string()

std::string comms::Color::to_css_rgba_string ( ) const
inline

CSS form with alpha, e.g.

"rgb(99 102 241 / 1.0)" (alpha to one decimal place).


The documentation for this struct was generated from the following file: