tui-cs/Terminal.Gui · error · FormatException
Failed to parse Color from {s}
Error message
Failed to parse Color from {s} What it means
This error is thrown by FileSystemColorProvider.StringToColor() when Color.TryParse fails to parse a string into a Color value. The FileSystemColorProvider maps file names and extensions to colors for display in file dialogs. If a color string in the configuration (FilenameToColor or ExtensionToColor dictionaries) is not a valid color representation, a FormatException is thrown with the offending string.
Source
Thrown at Terminal.Gui/FileServices/FileSystemColorProvider.cs:451
return extColor;
}
return null;
}
private static Color StringToColor (string str)
{
if (!Color.TryParse (str, out Color? c))
{
ThrowFormatException (str);
}
return c.Value;
[DoesNotReturn]
static void ThrowFormatException (string s)
{
throw new FormatException ($"Failed to parse Color from {s}");
}
}
}
View on GitHub (pinned to 2e47b11478)
Solutions
- Verify the color string against Terminal.Gui's supported color names (e.g., 'red', 'blue', 'green') and valid hex formats ('#RRGGBB').
- Use Color.TryParse yourself before adding to the dictionary to get a clear failure point.
- Check the config file or code that populates FileSystemColorProvider for typos.
- Correct the invalid color string to a valid color name or hex value.
Example fix
// before -- invalid color string causes FormatException
provider.FilenameToColor["file.txt"] = FileSystemColorProvider.StringToColor("bluw");
// after -- validate before use
if (Color.TryParse("blue", out Color? c)) { provider.FilenameToColor["file.txt"] = c.Value; } Defensive patterns
Strategy: validation
Validate before calling
string colorStr = "blue";
if (Color.TryParse(colorStr, out Color? c)) { provider.FilenameToColor["file.txt"] = c.Value; }
else { Console.Error.WriteLine($"Invalid color: {colorStr}"); } Try / catch
try { Color color = StringToColor(colorStr); }
catch (FormatException ex) { Console.Error.WriteLine($"Bad color config: {ex.Message}"); /* use default color */ } Prevention
- Validate color strings with Color.TryParse before adding to FileSystemColorProvider dictionaries.
- Check color configuration files for typos and invalid color names.
- Use only Terminal.Gui-recognized color names and hex formats (#RRGGBB).
- Add a config validation step at application startup.
When it happens
Trigger: Thrown at FileSystemColorProvider.cs:451 (inside ThrowFormatException, called from StringToColor at line 443) when Color.TryParse(str, out Color? c) returns false. StringToColor is used internally when populating the FilenameToColor and ExtensionToColor dictionaries. The trigger is an invalid color string being added via configuration or code.
Common situations: A typo in a color configuration string (e.g., 'bluw' instead of 'blue'), using a color name not recognized by the Color parser (e.g., a CSS color name not in Terminal.Gui's color set), providing an invalid hex format (e.g., '#GGGGGG'), or loading a config file with corrupt color values.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Unexpected color name: {colorString}.
- Unexpected token when parsing Color: {reader.TokenType}
- Provided text is too short to be any known color format.
- The text provided was null or empty.
- The text provided consisted of only whitespace characters.
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/5818857015ac6fb1.
Report an issue: GitHub.