Recursive AI

Parse a CSS color string in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Read red, green, blue, and opacity from a CSS value without a browser.

Terminal
dotnet add package RecursiveAI.OneHtml

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneHtml.Css;
 
const string value = "rgb(51 102 153 / 50%)";
if (CssColor.TryParse(value, out var color))
    Console.WriteLine(color.ToRgba8());
else
    Console.WriteLine("Invalid CSS color.");

Unzip the example, then dotnet run.

The output gives red, green, and blue on a 0–255 scale and alpha (opacity) on a 0–1 scale.

Text
(51, 102, 153, 0.5)

TryParse returns false for an unrecognized value such as not-a-color; the example prints Invalid CSS color.

Variations

Parse hex with alpha. Replace the value line. #3698 expands to #33669988, with the opacity at the end.

C#
const string value = "#3698";
Text
(51, 102, 153, 0.5333333333333333)

Read just the red channel from a six-digit hex color. Use this in place of the snippet at the top:

C#
using RecursiveAI.OneHtml.Css;
 
if (CssColor.TryParse("#336699", out var color))
    Console.WriteLine(color.ToRgba8().R);
Text
51

The short answer

Use CssColor.TryParse from RecursiveAI.OneHtml.Css on one CSS color value. On success, call ToRgba8() if you need red, green, and blue as bytes and opacity as a fraction. The method expects the value itself, not a declaration like color: red;. If the string came from a stylesheet or a style attribute, extract the declaration’s value first.

How do I convert a CSS hex color to RGBA?

Pass the hex string, including the #, to the same parser. In CSS hex notation, an eight-digit hex color is #RRGGBBAA, with alpha last. The four-digit form doubles each digit, so #3698 means #33669988: the first three bytes are 51, 102, and 153, and the last byte is 136, or 136 / 255 opacity. A three- or six-digit color has no explicit alpha and is opaque.

The parser also accepts named colors, rgb(), and hsl(); you don’t need to detect which syntax the string uses before you pass it in. For floating-point sRGB channels on a 0–1 scale, use ToSrgb() instead. Converting a wide-gamut value into sRGB can change its channels: ToSrgb() gamut-maps values that can’t be represented in sRGB. Keep the parsed CssColor if you still need its original color space. See the CSS values guide for the supported forms.

What does currentColor mean in CSS?

currentColor refers to the element’s color, not a fixed set of RGB channels. The parser can recognize currentColor without a page, but it can’t tell you whether that element’s text is black, blue, or something else. Check IsCurrentColor before reading the channels. If you already know the element’s color, pass it to Resolve:

C#
using RecursiveAI.OneHtml.Css;
 
if (CssColor.TryParse("currentColor", out var border) &&
    CssColor.TryParse("#336699", out var text))
{
    Console.WriteLine(border.Resolve(text).ToCss());
}
Text
rgb(51, 102, 153)

A declaration such as color: var(--accent) needs a different step: resolve the custom property in its CSS context before passing the resulting value to the color parser.

All onehtml recipes