Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::css::Color Struct Reference

Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor keyword. More...

#include "donner/css/Color.h"

Classes

struct  CurrentColor
 Represents the currentColor keyword. More...
 

Public Types

using Type = std::variant<RGBA, CurrentColor, HSLA>
 A variant for supported color types.
 

Public Member Functions

constexpr Color (Type value)
 Construct a new color object from a supported color type.
 
bool operator== (const Color &other) const
 Equality operator.
 
bool isCurrentColor () const
 Returns true if the color is currentcolor.
 
bool hasRGBA () const
 Returns true if the color is an RGBA color.
 
RGBA rgba () const
 Returns the RGBA color value, if this object stores an RGBA color.
 
bool hasHSLA () const
 Returns true if the color is an HSLA color.
 
HSLA hsla () const
 Returns the HSLA color value, if this object stores an HSLA color.
 
RGBA asRGBA () const
 Returns the color as RGBA.
 
RGBA resolve (RGBA currentColor, float opacity) const
 Resolves the current value of this color to RGBA, by using the current rendering state, such as the currentColor and opacity.
 

Static Public Member Functions

static std::optional< ColorByName (std::string_view name)
 Parse a named color, such as red or steelblue.
 

Public Attributes

Type value
 The color value.
 

Friends

std::ostream & operator<< (std::ostream &os, const Color &color)
 Ostream output operator.
 

Detailed Description

Represents a CSS color value, like a RGBA color from a #rrggbb or #rgb hex value, or the currentcolor keyword.

Colors are parsed using donner::css::parser::ColorParser.

Note that non-RGB colors, such as HSL are not yet supported, see bug https://github.com/jwmcglynn/donner/issues/6.

Constructor & Destructor Documentation

◆ Color()

constexpr donner::css::Color::Color ( Type value)
inlineconstexpr

Construct a new color object from a supported color type.

For example:

Color(RGBA::RGB(0xFF, 0x00, 0x00));
constexpr Color(Type value)
Construct a new color object from a supported color type.
Definition Color.h:140
static constexpr RGBA RGB(uint8_t r, uint8_t g, uint8_t b)
Constructor, for RGB colors, which are fully opaque.
Definition Color.h:37
Parameters
valueThe color value.

Member Function Documentation

◆ asRGBA()

RGBA donner::css::Color::asRGBA ( ) const

Returns the color as RGBA.

Note that isCurrentColor() colors cannot be converted to RGBA and will assert if called.

◆ ByName()

std::optional< Color > donner::css::Color::ByName ( std::string_view name)
static

Parse a named color, such as red or steelblue.

All colors on the CSS named color list are supported, https://www.w3.org/TR/css-color-4/#named-colors, plus two special colors, transparent and currentcolor.

Parameters
nameThe color name, such as red or steelblue.
Returns
The parsed color, or std::nullopt if the color could not be parsed.

◆ hsla()

HSLA donner::css::Color::hsla ( ) const
inline

Returns the HSLA color value, if this object stores an HSLA color.

Precondition
hasHSLA() returns true.

◆ resolve()

RGBA donner::css::Color::resolve ( RGBA currentColor,
float opacity ) const

Resolves the current value of this color to RGBA, by using the current rendering state, such as the currentColor and opacity.

Parameters
currentColorThe current color, used if this color is currentcolor.
opacityThe current opacity, used to multiply the alpha channel.

◆ rgba()

RGBA donner::css::Color::rgba ( ) const
inline

Returns the RGBA color value, if this object stores an RGBA color.

Precondition
hasRGBA() returns true.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Color & color )
friend

Ostream output operator.

Example output:

Color(currentColor)

or

Color(rgba(0, 255, 128, 255))
RGBA rgba() const
Returns the RGBA color value, if this object stores an RGBA color.
Definition Color.h:168
Parameters
osThe output stream.
colorThe color to output.

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