---
title: "Parse and evaluate CSS colors in C#"
url: https://recursiveai.org/onehtml/cookbook/parse-and-evaluate-css-colors/
description: "Evaluate CSS color-mix() and relative rgb() in C# without an HTML document. Read numeric sRGB channels and reject unrecognized color values."
version: "1.6.0"
---

# Parse and evaluate CSS colors 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-and-evaluate-css-colors/parse-and-evaluate-css-colors.zip)

Use `color-mix()` to blend two CSS colors and relative `rgb()` to change channels or opacity on one color.

```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;

foreach (var text in new[] {
    "color-mix(in srgb, red, blue)",
    "rgb(from #804020 r g b / 50%)",
    "not-a-color"
})
{
    Console.WriteLine(
        CssColor.TryParse(text, out var color)
            ? color.ToRgba8().ToString()
            : $"Rejected: {text}");
}
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/parse-and-evaluate-css-colors/parse-and-evaluate-css-colors.zip), then `dotnet run`.

The snippet above prints 8-bit red, green and blue channels, then alpha on a 0–1 scale. The downloaded program instead prints all four channels on a 0–1 scale. Both reject the third input.

```text
(128, 0, 128, 1)
(128, 64, 32, 0.5)
Rejected: not-a-color
```

## The full program

The download prints normalized sRGB channels instead: all four numbers are on a 0–1 scale.

```csharp
using RecursiveAI.OneHtml.Css;

string[] inputs = [
    "color-mix(in srgb, red, blue)",
    "rgb(from #804020 r g b / 50%)",
    "not-a-color"
];
foreach (string input in inputs)
{
    if (!CssColor.TryParse(input, out var color))
    {
        Console.WriteLine($"Rejected: {input}");
        continue;
    }
    var (r, g, b, a) = color.ToSrgb();
    Console.WriteLine(FormattableString.Invariant(
        $"{r:F3}, {g:F3}, {b:F3}, {a:F3}"));
}
```

`dotnet run` prints:

```text
0.500, 0.000, 0.500, 1.000
0.502, 0.251, 0.125, 0.500
Rejected: not-a-color
```

When `TryParse` returns `false`, don’t use the output color. Pass one color value, not a declaration such as `color: red;`.

## Variations

**Read the dark branch of `light-dark()`.** Use this after the `using` line. `DarkScheme` is an explicit input, not a query of the reader’s system settings.

```csharp
var options = new ColorParseOptions { DarkScheme = true };
if (CssColor.TryParse("light-dark(white, black)", out var color, options))
    Console.WriteLine(color.ToCss());
```

```text
rgb(0, 0, 0)
```

## Related recipes

- [Resolve CSS custom properties and fallbacks in C#](https://recursiveai.org/onehtml/cookbook/resolve-css-custom-properties-with-fallback/)
- [Get computed CSS styles in C#](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)

## The short answer

Call `CssColor.TryParse` with the CSS value, then call `ToSrgb()` for four numbers from 0 to 1 or `ToRgba8()` for byte-sized RGB and alpha from 0 to 1. The parser evaluates supported color expressions from text; it doesn’t need a DOM or a stylesheet. A `false` result means the input did not parse as a supported color, not that it parsed as black.

## How do I mix colors in CSS?

In [`color-mix(in srgb, red, blue)`](https://www.w3.org/TR/css-color-5/#color-mix), `in srgb` picks the space in which to blend the two colors. With no percentages, each contributes 50%. The normalized sRGB result is `(0.5, 0, 0.5, 1)`; converting to 8-bit channels rounds the red and blue channels to `128`. To change the balance, give an operand a percentage. To change how the blend is calculated, choose a different supported color space; converting the result back to sRGB does not turn it into an sRGB blend.

`ToSrgb()` returns gamma-encoded sRGB and maps out-of-gamut colors into sRGB. If you want to retain the parsed color’s space instead, use `Space` and `C1`, `C2`, `C3`. The channel names and units depend on `Space`; those numbers are not always red, green and blue. onehtml does not support single-operand `color-mix()` or `color-mix()` with 0% weights in this release.

## What is the syntax for relative colors in CSS?

The [`from` syntax](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Colors/Using_relative_colors#general_syntax) starts with a base color, then specifies the output channels. In `rgb(from #804020 r g b / 50%)`, `r g b` reuse the original channels and `/ 50%` sets alpha to `0.5`. In onehtml’s relative `rgb()` expressions, channels used in math are numbers on the 0–255 scale, even though `ToSrgb()` returns numbers on the 0–1 scale.

There’s a difference between evaluating a color and preserving its source. The [CSS value docs](https://recursiveai.org/onehtml/docs/css-values/#colors) describe `ToCss()` for mixtures, relative colors and math inside colors: it returns the resolved color, not the original expression. Keep the input string if you need to write the formula back. For CSS that depends on an element, resolve `var()` through the cascade and resolve a standalone `currentcolor` against its actual color before reading numeric channels.
