spectreconsole/spectre.console · error · InvalidOperationException
Could not find color '{part}'.
Error message
Could not find color '{part}'. What it means
After the `on` keyword, foreground is false so the part is expected to be a background color. When the part matches no known color name, is not a valid hex/rgb/integer, the parser reports 'Could not find color {part}.' This string is re-thrown by the public Parse.
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
- Use a color name present in ColorTable (standard names like red, blue, green, etc.)
- Use `#rrggbb`, `rgb(r,g,b)`, or a 0-255 index for the background
- Use `default` to leave the background unset
- Double-check spelling and that the token comes after `on`
Example fix
// before
AnsiMarkup.Parse("[red on purpel]text[/]");
// after
AnsiMarkup.Parse("[red on purple]text[/]"); Defensive patterns
Strategy: validation
Validate before calling
// Validate a background token against known forms before embedding:
public static bool IsValidBgToken(string token) =>
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}\)$")
|| token == "default"; Try / catch
try { AnsiMarkup.Parse(markup); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Could not find color"))
{ /* replace unknown bg token with 'default' or a known name */ } Prevention
- Spell color names exactly as defined in ColorTable
- Use hex or rgb forms to avoid name mismatches
- Validate the part appears after `on` for backgrounds
When it happens
Trigger: A background position holding an unrecognized token, e.g. `[red on purpel]` (typo), `[on notacolor]`, or `[on default default]` where the second 'default' after `on` is not a recognized color.
Common situations: Misspelled color name; using a decoration keyword (e.g. `bold`) in the background slot; expecting a custom color name that isn't registered.
Related errors
- Color number must be greater than or equal to 0 (was {number
- Color number must be less than or equal to 255 (was {number}
- Could not find color or style '{part}'.
- A foreground color has already been set.
- A background color has already been set.
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/7562ffc06dbbc7eb.
Report an issue: GitHub.