Table of Contents

Struct CssColor

Namespace
Egami.Css
Assembly
Egami.Css.dll

A CSS color (CSS Color 4) converted to sRGB: red, green, blue and alpha from 0 to 1, gamma-encoded, clipped to the sRGB gamut; or currentcolor.

public readonly struct CssColor : IEquatable<CssColor>
Implements
Inherited Members

Examples

// Every CSS Color 4 syntax, converted to sRGB (0 to 1) and clipped to its gamut.
bool isColor = CssColor.TryParse("oklch(62.8% 0.258 29.2 / 50%)", out CssColor red);
(byte r, byte g, byte b, byte a) = red.ToRgba8();         // about (255, 0, 0, 128)
bool newer = red.Level == 4;                             // SVG 1.1 knows only level 3

bool isCurrent = CssColor.TryParse("currentColor", out CssColor current);  // current.IsCurrentColor: use the 'color' property

Remarks

Level says which CSS Color level's syntax the text used, so a consumer that follows an older specification (such as SVG 1.1, which knows only CSS Color 3) can refuse the newer forms.

Constructors

CssColor(float, float, float, float)

Creates an sRGB color.

public CssColor(float r, float g, float b, float a = 1)

Parameters

r float

Red, 0 to 1.

g float

Green, 0 to 1.

b float

Blue, 0 to 1.

a float

Alpha, 0 (transparent) to 1 (opaque).

Properties

A

Gets alpha, 0 (transparent) to 1 (opaque).

public float A { get; }

Property Value

float

B

Gets blue, 0 to 1 (gamma-encoded sRGB).

public float B { get; }

Property Value

float

CurrentColor

Gets currentcolor.

public static CssColor CurrentColor { get; }

Property Value

CssColor

G

Gets green, 0 to 1 (gamma-encoded sRGB).

public float G { get; }

Property Value

float

IsCurrentColor

Gets whether the color is currentcolor: the consumer substitutes the element's color.

public bool IsCurrentColor { get; }

Property Value

bool

Level

Gets the CSS Color level whose syntax the color was written in: 3 for keywords, #rgb and #rrggbb, comma-separated rgb(), rgba(), hsl() and hsla() and the CSS2 system colors; 4 for #rgba, #rrggbbaa, rebeccapurple, space-separated or percentage-alpha rgb() and hsl(), hwb(), lab(), lch(), oklab(), oklch(), color() and the CSS Color 4 system colors.

public int Level { get; }

Property Value

int

R

Gets red, 0 to 1 (gamma-encoded sRGB).

public float R { get; }

Property Value

float

Transparent

Gets transparent: black with zero alpha.

public static CssColor Transparent { get; }

Property Value

CssColor

Methods

Equals(CssColor)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(CssColor other)

Parameters

other CssColor

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

ToRgba8()

Converts to 8-bit sRGB components, rounded.

public (byte R, byte G, byte B, byte A) ToRgba8()

Returns

(byte R, byte G, byte B, byte A)

Red, green, blue and alpha, 0 to 255.

ToString()

Serializes the color as CSS Color 4 does for sRGB: rgb(r, g, b) or rgba(r, g, b, a), or currentcolor.

public override string ToString()

Returns

string

The text.

TryParse(CssComponentValue, out CssColor)

Parses one component value as a color.

public static bool TryParse(CssComponentValue value, out CssColor color)

Parameters

value CssComponentValue

The component value: an identifier, a hash or a color function.

color CssColor

The color.

Returns

bool

True when the value is a valid color.

TryParse(IReadOnlyList<CssComponentValue>, out CssColor)

Parses component values as one color (whitespace around it is ignored).

public static bool TryParse(IReadOnlyList<CssComponentValue> values, out CssColor color)

Parameters

values IReadOnlyList<CssComponentValue>

The component values.

color CssColor

The color.

Returns

bool

True when the values are one valid color.

TryParse(string, out CssColor)

Parses a color, such as red, #ff000080, rgb(255 0 0 / 50%) or oklch(62.8% 0.26 29).

public static bool TryParse(string text, out CssColor color)

Parameters

text string

The CSS text (ASCII case-insensitive).

color CssColor

The color.

Returns

bool

True when the text is one valid color.

Operators

operator ==(CssColor, CssColor)

Compares two colors by their components; the syntax level is not compared.

public static bool operator ==(CssColor left, CssColor right)

Parameters

left CssColor

The first color.

right CssColor

The second color.

Returns

bool

True when they are equal.

operator !=(CssColor, CssColor)

Compares two colors by their components; the syntax level is not compared.

public static bool operator !=(CssColor left, CssColor right)

Parameters

left CssColor

The first color.

right CssColor

The second color.

Returns

bool

True when they differ.