Use color-mix() to blend two CSS colors and relative rgb() to change channels or opacity on one color.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, 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.
(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.
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:
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.
var options = new ColorParseOptions { DarkScheme = true }; if (CssColor.TryParse("light-dark(white, black)", out var color, options)) Console.WriteLine(color.ToCss());
rgb(0, 0, 0)Related recipes
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), 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 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 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.