Struct CssColor
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
rfloatRed, 0 to 1.
gfloatGreen, 0 to 1.
bfloatBlue, 0 to 1.
afloatAlpha, 0 (transparent) to 1 (opaque).
Properties
A
Gets alpha, 0 (transparent) to 1 (opaque).
public float A { get; }
Property Value
B
Gets blue, 0 to 1 (gamma-encoded sRGB).
public float B { get; }
Property Value
CurrentColor
Gets currentcolor.
public static CssColor CurrentColor { get; }
Property Value
G
Gets green, 0 to 1 (gamma-encoded sRGB).
public float G { get; }
Property Value
IsCurrentColor
Gets whether the color is currentcolor: the consumer substitutes the element's color.
public bool IsCurrentColor { get; }
Property Value
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
R
Gets red, 0 to 1 (gamma-encoded sRGB).
public float R { get; }
Property Value
Transparent
Gets transparent: black with zero alpha.
public static CssColor Transparent { get; }
Property Value
Methods
Equals(CssColor)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(CssColor other)
Parameters
otherCssColorAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand 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
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
valueCssComponentValueThe component value: an identifier, a hash or a color function.
colorCssColorThe 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
valuesIReadOnlyList<CssComponentValue>The component values.
colorCssColorThe 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
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
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
Returns
- bool
True when they differ.