Class ColorConverter
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
hexstringA 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
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
hexstringThe hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").
includeAlphaboolWhether 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
hexstringThe hexadecimal color string (e.g., "#FF5733" or "#FF5733AA").
includeAlphaboolWhether 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
hslstringThe HSL or HSLA string (e.g., "hsl(9, 100%, 65%)" or "hsla(9, 100%, 65%, 0.67)").
includeAlphaboolWhether to include alpha channel in the output if present in the HSL string.
fallbackValuestringThe 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
hdoubleThe hue value in degrees (0-360).
sdoubleThe saturation value (0.0 to 1.0).
ldoubleThe lightness value (0.0 to 1.0).
Returns
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
colorstringThe value to check.
Returns
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
Returns
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
rgbstringThe RGB or RGBA string (e.g., "rgb(255, 87, 51)" or "rgba(255, 87, 51, 0.67)").
includeAlphaboolWhether to include alpha channel in the output if present in the RGB string.
fallbackValuestringThe 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
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
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
hexstringThe 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.