Table of Contents

Class CssColor

Namespace
BlazOrbit.Components
Assembly
BlazOrbit.Core.dll

The Css color representation.

public sealed class CssColor
Inheritance
object
CssColor

Constructors

CssColor(byte, byte, byte, byte)

Constructs a CssColor from RGBA bytes

public CssColor(byte r, byte g, byte b, byte a)

Parameters

r byte
g byte
b byte
a byte

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

r byte
g byte
b byte
a byte
colorVariant CssColorVariant

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

h double
s double
l double
a double

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

h double
s double
l double
a int

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

r int
g int
b int
alpha double

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

r int
g int
b int
alpha int

CssColor(string)

Constructs a CssColor from string representation (RGB/RGBA/HEX)

public CssColor(string value)

Parameters

value string

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

floatCharacter char?

Returns

string

ChangeLightness(double)

Modify Lightness value.

public CssColor ChangeLightness(double amount)

Parameters

amount double

Returns

CssColor

ColorDarken(double)

Get darken color from current instance.

public CssColor ColorDarken(double amount)

Parameters

amount double

Returns

CssColor

ColorLighten(double)

Get lighten color from current instance.

public CssColor ColorLighten(double amount)

Parameters

amount double

Returns

CssColor

Equals(CssColor?)

public bool Equals(CssColor? other)

Parameters

other CssColor

Returns

bool

Equals(object?)

public override bool Equals(object? obj)

Parameters

obj object

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

contrastBlack CssColor
contrastWhite CssColor

Returns

CssColor

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

value string

Returns

CssColor

SetAlpha(double)

Sets the alpha value for current instance.

public CssColor SetAlpha(double a)

Parameters

a double

Returns

CssColor

SetAlpha(int)

Sets the alpha value for current instance.

public CssColor SetAlpha(int a)

Parameters

a int

Returns

CssColor

ToString()

public override string ToString()

Returns

string

ToString(ColorOutputFormats)

public string ToString(ColorOutputFormats format)

Parameters

format ColorOutputFormats

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

value string
result CssColor

Returns

bool

Operators

operator ==(CssColor?, CssColor?)

public static bool operator ==(CssColor? lhs, CssColor? rhs)

Parameters

lhs CssColor
rhs CssColor

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

color CssColor

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

input string

Returns

CssColor

operator !=(CssColor?, CssColor?)

public static bool operator !=(CssColor? lhs, CssColor? rhs)

Parameters

lhs CssColor
rhs CssColor

Returns

bool