---
title: "Parse a CSS color string in C#"
url: https://recursiveai.org/onehtml/cookbook/parse-css-color/
description: "Parse a CSS color string in C# and read red, green, blue, and alpha. Handle hex, named colors, rgb(), and hsl() without opening or rendering an HTML page."
version: "1.6.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/parse-css-color/parse-css-color.zip)

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

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onehtml/cookbook/parse-css-color/parse-css-color.zip), 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.

```csharp
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:

```csharp
using RecursiveAI.OneHtml.Css;

if (CssColor.TryParse("#336699", out var color))
    Console.WriteLine(color.ToRgba8().R);
```

```text
51
```

## Related recipes

- [Parse and evaluate CSS color expressions](https://recursiveai.org/onehtml/cookbook/parse-and-evaluate-css-colors/)
- [Read an element’s computed CSS style](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)

## 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](https://www.w3.org/TR/css-color-4/#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](https://recursiveai.org/onehtml/docs/css-values/#colors) for the supported forms.

## What does `currentColor` mean in CSS?

[`currentColor`](https://www.w3.org/TR/css-color-4/#currentcolor-color) 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`:

```csharp
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.
