docs
Loading...
Searching...
No Matches
Color

#include <Ultralight/Color.h>

Overview

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:

Color green = Color(0.0f, 1.0f, 0.0f); // opaque green
Color semi = Color(1.0f, 1.0f, 1.0f, 0.1f); // semi-transparent white
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"; // compile-time literal
Color alpaca = "rgb(255, 240, 220)"; // compile-time literal

Runtime CSS strings (supports the full CSS color grammar, including named colors):

Color themed = Color::Parse(user_text); // a String, std::string, std::string_view, ...
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)).

Public Types

enum  Space : uint8_t { kSRGB = 0 }
 The color space of the channels. More...
enum  Flags : uint8_t { kSet = 1 << 0 , kInvalid = 1 << 1 }
 State bits carried in flags. More...

Static Public Member Functions

static Color Parse (const String &css)
 Parse a CSS color string at runtime: the full CSS color grammar (named colors, hex, rgb()/hsl()/hwb()/lab()/oklch()/color(), transparent).
template<detail::Utf8StringLike S>
static Color Parse (const S &css)
 Parse CSS color text held in any UTF-8 string type with data() and size() (eg, std::string or std::string_view), the same as Parse(const String&).

Public Member Functions

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.

Public Attributes

float r = 0.0f
 Red, 0..1, in the space named by space.
float g = 0.0f
 Green, 0..1.
float b = 0.0f
 Blue, 0..1.
float a = 0.0f
 Alpha, 0..1, linear (1 is fully opaque).
uint8_t space = kSRGB
 A Space value.
uint8_t flags = 0
 A Flags combination (see is_set() and invalid()).

Friends

constexpr bool operator== (const Color &, const Color &)=default

Member Enumeration Documentation

◆ Flags

enum Flags : uint8_t

State bits carried in flags.

Enumerator
kSet 

Explicitly set (unset means the receiving field's default applies).

kInvalid 

Came from a string that failed to parse; ignored with a warning.

◆ Space

enum Space : uint8_t

The color space of the channels.

Enumerator
kSRGB 

sRGB (the only value currently produced or accepted).

Constructor & Destructor Documentation

◆ Color() [1/4]

Color ( )
constexprdefault

Create an unset Color (the receiving field's default applies).

◆ Color() [2/4]

Color ( float red,
float green,
float blue,
float alpha = 1.0f )
inlineconstexpr

Create a set sRGB color from float channels (0..1; alpha 1 is fully opaque).

◆ Color() [3/4]

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().

◆ Color() [4/4]

Color ( const ULColor & c)
inlineconstexpr

Create a Color from its C mirror (implicit; the two carry identical state).

Member Function Documentation

◆ invalid()

bool invalid ( ) const
inlineconstexpr

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).

Invalid colors are inert– whatever you pass one to ignores it and logs a warning.

◆ is_set()

bool is_set ( ) const
inlineconstexpr

Whether or not this color was explicitly set.

An unset color in any options struct or setter means the built-in default (writing one to a style property removes the property).

◆ operator bool()

operator bool ( ) const
inlineexplicitconstexpr

Whether or not this color is set and valid.

◆ operator ULColor()

operator ULColor ( ) const
inlineconstexpr

Convert to the C mirror (implicit; the two carry identical state).

◆ Parse() [1/2]

template<detail::Utf8StringLike S>
Color Parse ( const S & css)
inlinestatic

Parse CSS color text held in any UTF-8 string type with data() and size() (eg, std::string or std::string_view), the same as Parse(const String&).

Parameters
cssThe CSS color text to parse, as UTF-8.
Returns
Returns the parsed color (set) on success, or an invalid Color when the string does not parse (see invalid()).

◆ Parse() [2/2]

Color Parse ( const String & css)
static

Parse a CSS color string at runtime: the full CSS color grammar (named colors, hex, rgb()/hsl()/hwb()/lab()/oklch()/color(), transparent).

Wide-gamut input narrows to sRGB.

Parameters
cssThe CSS color text to parse.
Returns
Returns the parsed color (set) on success, or an invalid Color when the string does not parse (see invalid()).

◆ ToHexString()

String ToHexString ( ) const

Serialize this color as CSS hex text: lowercase #rrggbb, or #rrggbbaa when the alpha channel is not fully opaque.

Float channels quantize to 8-bit here (the hex wire form is 8-bit), so Parse(ToHexString()) round-trips to the quantized channels.

Returns
Returns the hex text, or an empty string when this color is unset or invalid (see is_set() and invalid()).
See also
Parse()

◆ operator==

bool operator== ( const Color & ,
const Color &  )
friend

Member Data Documentation

◆ a

float a = 0.0f

Alpha, 0..1, linear (1 is fully opaque).

◆ b

float b = 0.0f

Blue, 0..1.

◆ flags

uint8_t flags = 0

A Flags combination (see is_set() and invalid()).

◆ g

float g = 0.0f

Green, 0..1.

◆ r

float r = 0.0f

Red, 0..1, in the space named by space.

◆ space

uint8_t space = kSRGB

A Space value.


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