An RGBA color value in a certain color space (with CSS parsing helpers).
Colors are stored as floats [0..1] in the sRGB color space with straight alpha.
Creating Colors
You can create Colors from float RGBA (0..1) channels:
constexpr Color()=default
Create an unset Color (the receiving field's default applies).
CSS string literals (parsed and validated at compile-time, see below for limitations):
Color ultramarine =
"#3f00ff";
Color alpaca =
"rgb(255, 240, 220)";
Runtime CSS strings (supports the full CSS color grammar, including named colors):
static Color Parse(const String &css)
Parse a CSS color string at runtime: the full CSS color grammar (named colors, hex,...
Limitations of Compile-Time CSS Literals
String literals accept hex, rgb() / rgba(), and transparent; anything else (for example, hsl() or named colors like purple) is not supported. Use Parse() if you need the full CSS color grammar.
- Note
- Default-constructed Colors are unset (test with is_set(), or if (color)).
|
| constexpr | Color ()=default |
| | Create an unset Color (the receiving field's default applies).
|
| constexpr | Color (float red, float green, float blue, float alpha=1.0f) |
| | Create a set sRGB color from float channels (0..1; alpha 1 is fully opaque).
|
| template<size_t N> |
| UL_CSS_CONSTEVAL | Color (const char(&s)[N]) |
| | Create a Color from a CSS string literal, parsed and validated at compile time.
|
| constexpr | Color (const ULColor &c) |
| | Create a Color from its C mirror (implicit; the two carry identical state).
|
| constexpr | operator ULColor () const |
| | Convert to the C mirror (implicit; the two carry identical state).
|
| String | ToHexString () const |
| | Serialize this color as CSS hex text: lowercase #rrggbb, or #rrggbbaa when the alpha channel is not fully opaque.
|
| constexpr bool | is_set () const |
| | Whether or not this color was explicitly set.
|
| constexpr bool | invalid () const |
| | Whether or not this color came from a string that failed to parse (Parse() at runtime, or a malformed literal on a compiler where literal validation degrades to a runtime flag).
|
| constexpr | operator bool () const |
| | Whether or not this color is set and valid.
|
template<size_t N>
| UL_CSS_CONSTEVAL Color |
( |
const char(&) | s[N] | ) |
|
|
inline |
Create a Color from a CSS string literal, parsed and validated at compile time.
Accepted forms are hex colors (eg, #ff00ff), rgb()/rgba(), and transparent. Anything else is a compile-time error naming the reason. Only string literals bind here, runtime strings go through Parse().