Table of Contents

Class ColorConverter

Namespace
TwBlazor.Utilities
Assembly
TwBlazor.dll

Provides utility methods for converting between different color format representations (Hex, RGB, HSL).

public static class ColorConverter
Inheritance
ColorConverter
Inherited Members

Methods

ExpandShortHex(string)

Expands a 3 digit hex color (e.g. #abc) to 6 digits (#aabbcc); other lengths are returned unchanged.

public static string ExpandShortHex(string hex)

Parameters

hex string

A valid hex color.

Returns

string

The hex color with shorthand expanded.

GetContrastRatio((double R, double G, double B), (double R, double G, double B))

The WCAG contrast ratio (1 to 21) between two RGB colors.

public static double GetContrastRatio((double R, double G, double B) a, (double R, double G, double B) b)

Parameters

a (double h, double s, double l)

The first RGB color (0-255 per channel).

b (double h, double s, double l)

The second RGB color (0-255 per channel).

Returns

double

The contrast ratio, the same in either order.

GetRelativeLuminance((double R, double G, double B))

The WCAG relative luminance (0 to 1) of an RGB color.

public static double GetRelativeLuminance((double R, double G, double B) color)

Parameters

color (double h, double s, double l)

The RGB color (0-255 per channel).

Returns

double

The relative luminance.

HexToHsl(string, bool)

Converts a hexadecimal color string to an HSL or HSLA string representation.

public static string HexToHsl(string hex, bool includeAlpha = false)

Parameters

hex string

The hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").

includeAlpha bool

Whether to include alpha channel in the output if present in the hex string.

Returns

string

An HSL or HSLA string representation (e.g., "hsl(9, 100%, 65%)" or "hsla(9, 100%, 65%, 0.67)").

HexToRgb(string, bool)

Converts a hexadecimal color string to an RGB or RGBA string representation.

public static string HexToRgb(string hex, bool includeAlpha = false)

Parameters

hex string

The hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").

includeAlpha bool

Whether to include alpha channel in the output if present in the hex string.

Returns

string

An RGB or RGBA string representation (e.g., "rgb(255, 87, 51)" or "rgba(255, 87, 51, 0.67)").

HslToHex(string, bool, string)

Converts an HSL or HSLA string representation to a hexadecimal color string.

public static string HslToHex(string hsl, bool includeAlpha = false, string fallbackValue = "#000000")

Parameters

hsl string

The HSL or HSLA string (e.g., "hsl(9, 100%, 65%)" or "hsla(9, 100%, 65%, 0.67)").

includeAlpha bool

Whether to include alpha channel in the output if present in the HSL string.

fallbackValue string

The fallback value to return if parsing fails. Defaults to "#000000".

Returns

string

A hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").

HslToRgb(double, double, double)

Converts HSL color values to RGB color values.

public static (int r, int g, int b) HslToRgb(double h, double s, double l)

Parameters

h double

The hue value in degrees (0-360).

s double

The saturation value (0.0 to 1.0).

l double

The lightness value (0.0 to 1.0).

Returns

(int r, int g, int b)

A tuple containing the RGB values (0-255 for each component).

IsValidHex(string?)

Whether color is a well-formed 3, 6 or 8 digit hex color with a leading #.

public static bool IsValidHex(string? color)

Parameters

color string

The value to check.

Returns

bool

true when the value is a valid hex color.

OklchToRgb(double, double, double)

Converts OKLCH to sRGB, clamping colors outside the sRGB gamut to its edge.

public static (double R, double G, double B) OklchToRgb(double lightness, double chroma, double hue)

Parameters

lightness double

Lightness (0-1).

chroma double

Chroma (about 0-0.4).

hue double

Hue in degrees.

Returns

(double h, double s, double l)

The red, green and blue values (0-255, not rounded).

RgbToHex(string, bool, string)

Converts an RGB or RGBA string representation to a hexadecimal color string.

public static string RgbToHex(string rgb, bool includeAlpha = false, string fallbackValue = "#000000")

Parameters

rgb string

The RGB or RGBA string (e.g., "rgb(255, 87, 51)" or "rgba(255, 87, 51, 0.67)").

includeAlpha bool

Whether to include alpha channel in the output if present in the RGB string.

fallbackValue string

The fallback value to return if parsing fails. Defaults to "#000000".

Returns

string

A hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").

RgbToHsl(int, int, int)

Converts RGB color values to HSL color values.

public static (double h, double s, double l) RgbToHsl(int r, int g, int b)

Parameters

r int

The red value (0-255).

g int

The green value (0-255).

b int

The blue value (0-255).

Returns

(double h, double s, double l)

A tuple containing HSL values where h is in degrees (0-360), s and l are in range (0.0-1.0).

RgbToOklch(int, int, int)

Converts sRGB to OKLCH, the perceptual space Tailwind's own palette is defined in.

public static (double L, double C, double H) RgbToOklch(int r, int g, int b)

Parameters

r int

The red value (0-255).

g int

The green value (0-255).

b int

The blue value (0-255).

Returns

(double h, double s, double l)

Lightness (0-1), chroma (about 0-0.4) and hue in degrees (0-360; 0 for greys).

TryHexToRgb(string?, out (int R, int G, int B))

Parses a hex color (#rgb, #rrggbb or #rrggbbaa, with the leading #) into RGB.

public static bool TryHexToRgb(string? hex, out (int R, int G, int B) rgb)

Parameters

hex string

The hex color.

rgb (int r, int g, int b)

The red, green and blue values (0-255) when this returns true; any alpha is ignored.

Returns

bool

true when hex is a valid hex color.