spectreconsole/spectre.console · error · InvalidOperationException

Could not find color or style '{part}'.

Error message

Could not find color or style '{part}'.

What it means

In the foreground position (before any `on`), a part matched no decoration, no color name, and is not hex/rgb/integer, so it is neither a known color nor a style. The parser reports 'Could not find color or style {part}.' and the public Parse re-throws it.

Source

Thrown at src/Spectre.Console.Ansi/AnsiMarkupTagParser.cs:10

namespace Spectre.Console;

internal static class AnsiMarkupTagParser
{
    public static (Style Style, Link? Link) Parse(string text)
    {
        var result = Parse(text, out var error);
        if (error != null)
        {
            throw new InvalidOperationException(error);
        }

        return result ?? throw new InvalidOperationException("Could not parse style.");
    }

    public static bool TryParse(string text, [NotNullWhen(true)] out (Style Style, Link?)? result)
    {
        result = Parse(text, out _);
        return result != null;
    }

    private static (Style Style, Link? Link)? Parse(string text, out string? error)
    {
        var effectiveDecoration = (Decoration?)null;
        var effectiveForeground = (Color?)null;
        var effectiveBackground = (Color?)null;
        var effectiveLink = (string?)null;

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Use a decoration name registered in DecorationTable (e.g. bold, italic, underline, dim) or a known color
  2. Check spelling against the library's supported style vocabulary
  3. Use hex/rgb/index forms for colors

Example fix

// before
AnsiMarkup.Parse("[bonld]text[/]");

// after
AnsiMarkup.Parse("[bold]text[/]");
Defensive patterns

Strategy: validation

Validate before calling

public static bool IsValidFgToken(string token) =>
    DecorationTable.GetDecoration(token) != null
    || ColorTable.GetColor(token) != null
    || Regex.IsMatch(token, @"^#[0-9A-Fa-f]{6}$")
    || Regex.IsMatch(token, @"^rgb\(\d{1,3},\d{1,3},\d{1,3}\)$")
    || (int.TryParse(token, out var n) && n is >= 0 and <= 255);

Try / catch

try { AnsiMarkup.Parse(markup); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Could not find color or style"))
{ /* drop the unknown token or substitute a known style */ }

Prevention

When it happens

Trigger: Foreground slot with an unknown token, e.g. `[bonld]` (typo of bold), `[mycolor]`, or a stray decoration keyword placed where it isn't recognized.

Common situations: Misspelled style name; using a non-existent decoration; copy-pasting a style name from an incompatible library version.

Related errors


AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13). Data as JSON: /api/errors/02a824e8659302a7. Report an issue: GitHub.