Class CssColor
- Namespace
- BlazOrbit.Components
- Assembly
- BlazOrbit.Core.dll
The Css color representation.
public sealed class CssColor
- Inheritance
-
objectCssColor
Constructors
CssColor(byte, byte, byte, byte)
Constructs a CssColor from RGBA bytes
public CssColor(byte r, byte g, byte b, byte a)
Parameters
rbytegbytebbyteabyte
CssColor(byte, byte, byte, byte, CssColorVariant?)
Constructs a CssColor from RGBA and optional variant
public CssColor(byte r, byte g, byte b, byte a, CssColorVariant? colorVariant = null)
Parameters
rbytegbytebbyteabytecolorVariantCssColorVariant
CssColor(double, double, double, double)
Constructs a CssColor from HSL and alpha values (double alpha)
public CssColor(double h, double s, double l, double a)
Parameters
hdoublesdoubleldoubleadouble
CssColor(double, double, double, int)
Constructs a CssColor from HSL and alpha values (int alpha)
public CssColor(double h, double s, double l, int a)
Parameters
hdoublesdoubleldoubleaint
CssColor(int, int, int, double)
Constructs a CssColor from RGBA integers and double alpha
public CssColor(int r, int g, int b, double alpha)
Parameters
rintgintbintalphadouble
CssColor(int, int, int, int)
Constructs a CssColor from RGBA integers and int alpha
public CssColor(int r, int g, int b, int alpha)
Parameters
rintgintbintalphaint
CssColor(string)
Constructs a CssColor from string representation (RGB/RGBA/HEX)
public CssColor(string value)
Parameters
valuestring
Properties
A
The Alpha value.
public byte A { get; }
Property Value
- byte
APercentage
The Alpha value percentage.
public double APercentage { get; }
Property Value
- double
B
The Blue value.
public byte B { get; }
Property Value
- byte
G
The Green Value.
public byte G { get; }
Property Value
- byte
H
The Hue Value.
public double H { get; }
Property Value
- double
L
The Luminosity Value.
public double L { get; }
Property Value
- double
R
The Red Value.
public byte R { get; }
Property Value
- byte
S
The Saturation Value.
public double S { get; }
Property Value
- double
Value
The Color Value.
public string Value { get; }
Property Value
- string
Methods
APercentageString(char?)
Gets the alpha percentage as string.
public string APercentageString(char? floatCharacter = '.')
Parameters
floatCharacterchar?
Returns
- string
ChangeLightness(double)
Modify Lightness value.
public CssColor ChangeLightness(double amount)
Parameters
amountdouble
Returns
ColorDarken(double)
Get darken color from current instance.
public CssColor ColorDarken(double amount)
Parameters
amountdouble
Returns
ColorLighten(double)
Get lighten color from current instance.
public CssColor ColorLighten(double amount)
Parameters
amountdouble
Returns
Equals(CssColor?)
public bool Equals(CssColor? other)
Parameters
otherCssColor
Returns
- bool
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
- bool
GetBestContrast(CssColor, CssColor)
Returns the best contrast color (black or white) for this color. Uses CSS variables with fallback if no custom values are set.
public CssColor GetBestContrast(CssColor contrastBlack, CssColor contrastWhite)
Parameters
Returns
GetHashCode()
public override int GetHashCode()
Returns
- int
GetRelativeLuminance()
Returns the relative luminance of the current color (0 = dark, 1 = light) using the WCAG 2.1 formula.
public double GetRelativeLuminance()
Returns
- double
Parse(string)
Parses a CSS color string. Throws System.ArgumentException on malformed input.
public static CssColor Parse(string value)
Parameters
valuestring
Returns
SetAlpha(double)
Sets the alpha value for current instance.
public CssColor SetAlpha(double a)
Parameters
adouble
Returns
SetAlpha(int)
Sets the alpha value for current instance.
public CssColor SetAlpha(int a)
Parameters
aint
Returns
ToString()
public override string ToString()
Returns
- string
ToString(ColorOutputFormats)
public string ToString(ColorOutputFormats format)
Parameters
formatColorOutputFormats
Returns
- string
TryParse(string?, out CssColor?)
Tries to parse a CSS color string. Returns true on success and
sets result; returns false for null / empty
/ unrecognized formats without throwing.
public static bool TryParse(string? value, out CssColor? result)
Parameters
valuestringresultCssColor
Returns
- bool
Operators
operator ==(CssColor?, CssColor?)
public static bool operator ==(CssColor? lhs, CssColor? rhs)
Parameters
Returns
- bool
explicit operator string(CssColor?)
Renders the color as a CSS string. Explicit because the conversion
is lossy (the format defaults to #rrggbb) and the implicit
version would silently absorb null into string.Empty
in unexpected contexts. Prefer ToString() directly when
the call site is obvious.
public static explicit operator string(CssColor? color)
Parameters
colorCssColor
Returns
- string
explicit operator CssColor(string)
Parses a CSS color string (hex / rgb / rgba). Explicit because the constructor throws on malformed input and the implicit version made it easy to feed unvalidated user data straight into a parse. Use TryParse(string?, out CssColor?) when the input might fail; use Parse(string) or this cast when you've already validated it.
public static explicit operator CssColor(string input)
Parameters
inputstring
Returns
operator !=(CssColor?, CssColor?)
public static bool operator !=(CssColor? lhs, CssColor? rhs)
Parameters
Returns
- bool