|
commons 0.1.5
Header-only C++23 library of common/shared types for the C++ libraries
|
A tiny RGBA color container with rich, mostly-constexpr color manipulation, plus the Hsl/Hsv model structs and the CSS named colors.
More...
#include <commons/types.hpp>#include <array>#include <cstddef>#include <format>#include <optional>#include <ostream>#include <stdexcept>#include <string>#include <string_view>

Go to the source code of this file.
Classes | |
| struct | comms::detail::ParsedNum |
A number parsed out of a CSS-functional component: its value, whether it carried a % suffix, and whether the parse succeeded at all. More... | |
| struct | comms::Hsl |
| Hue / saturation / lightness, plus unit alpha. More... | |
| struct | comms::Hsv |
| Hue / saturation / value (brightness), plus unit alpha. More... | |
| struct | comms::Color |
| An 8-bit-per-channel RGBA color. Default is opaque black. More... | |
| struct | comms::CssColors |
The CSS named colors as static constexpr Color values (the single source of truth for their hex), plus a case-insensitive name lookup via parse. More... | |
| struct | comms::MuiColors |
| The Material UI palette (2014 Material Design). More... | |
| struct | comms::Colors |
Top-level collection of named-color sets: the CSS named colors (comms::Colors::css::red) and the Material UI palette (comms::Colors::mui::red_500). More... | |
| struct | std::formatter< comms::Color > |
Formats comms::Color. More... | |
| struct | std::formatter< comms::Hsl > |
Formats comms::Hsl as comms::to_string does. Takes no format spec. More... | |
| struct | std::formatter< comms::Hsv > |
Formats comms::Hsv as comms::to_string does. Takes no format spec. More... | |
Functions | |
| constexpr usize | comms::detail::find_char (std::string_view s, char c) |
Index of the first c in s, or npos. | |
| constexpr f64 | comms::detail::round_nonneg (const f64 x) |
| Round a non-negative value to the nearest integer. | |
| constexpr f64 | comms::detail::wrap_hue (f64 h) |
Wrap a hue in degrees into the canonical [0, 360) range. | |
| constexpr u8 | comms::detail::round_u8 (const f64 v) |
Map a unit value [0, 1] to a u8 channel, clamping then rounding. | |
| constexpr u8 | comms::detail::round_channel (const f64 v) |
Round an already-[0, 255]-scaled value to a u8, clamping first. | |
| constexpr f64 | comms::detail::log_ (f64 x) |
Natural log for x > 0, via power-of-two range reduction and the atanh-style series; error well below 1e-12 over the reduced interval. | |
| constexpr f64 | comms::detail::exp_ (const f64 x) |
e^x, via reduction to x = k*ln2 + r with |r| <= ln2/2 and a Taylor series for e^r, then an exact integer scale by 2^k. | |
| constexpr f64 | comms::detail::pow_ (const f64 base, const f64 exp) |
base^exp for base >= 0. Used only for the sRGB gamma pow(x, 2.4). | |
| constexpr f64 | comms::detail::srgb_to_linear (const f64 cs) |
One sRGB channel (unit [0, 1]) to linear light, per the WCAG definition. | |
| constexpr ParsedNum | comms::detail::parse_num (std::string_view s) |
Parse a single decimal number (optional sign, optional fraction, optional trailing %). | |
| constexpr int | comms::detail::split_components (const std::string_view s, std::array< std::string_view, 4 > &out) |
| Split a CSS-functional argument list into at most four tokens. | |
| std::string | comms::to_string (const Color &c) |
Color as its canonical hex string (#RRGGBB, or #RRGGBBAA when not opaque). | |
| std::string | comms::to_string (const Hsl &v) |
Hsl as hsl(h, s, l) — or hsla(h, s, l, a) when alpha != 1 — with components printed at full round-trip precision. | |
| std::string | comms::to_string (const Hsv &v) |
Hsv as hsv(h, s, v) — or hsva(h, s, v, a) when alpha != 1. | |
A tiny RGBA color container with rich, mostly-constexpr color manipulation, plus the Hsl/Hsv model structs and the CSS named colors.
comms::Color is four u8 channels (r, g, b, a). Everything that can be is constexpr: packed-int conversion, HSL/HSV conversion, channel and alpha tweaks, the HSL transforms (lighten/darken/…), WCAG luminance and contrast, palette generation, and parsing of hex / CSS-functional / CSS named colors. Only the std::string producers are non-constexpr.
Everything color-related lives in this one header on purpose: Color::parse resolves color names through CssColors::parse, and the named colors are themselves Color values, so splitting them would create an include cycle.
Serialization (in commons/json.hpp, gated by COMMONS_WITH_NLOHMANN_JSON): Color travels as a hex string (#RRGGBB, or #RRGGBBAA when not opaque); Hsl/Hsv travel as JSON objects.
Text output (always available): to_string, operator<<, and std::format support for all three types. Color's formatter accepts h (lowercase hex, the default), H (uppercase hex), and r (CSS rgb(...)).
|
constexpr |
Index of the first c in s, or npos.
A manual scan rather than std::string_view::find, whose libstdc++ implementation is not usable in a constant expression here (it routes through a non-constexpr pointer path).
|
constexpr |
Parse a single decimal number (optional sign, optional fraction, optional trailing %).
No exponent form — CSS colors never need it.
|
constexpr |
Round a non-negative value to the nearest integer.
Routed through trunc_ so it stays valid for the clamped (non-negative) inputs we feed it.
|
constexpr |
Split a CSS-functional argument list into at most four tokens.
Components are separated by commas, whitespace, or the / that precedes a modern alpha. Returns the token count, or -1 if there are more than four.