unoplatform/uno · error · FormatException
Failed to parse color code: #{colorCode}
Error message
Failed to parse color code: #{colorCode} What it means
After the '#', ParseColorCode accepts only specific lengths: 4 (#RGB, alpha forced to 0xFF), 7 (#RRGGBB, alpha 0xFF), and 9 (#AARRGGBB). Any other length — including #RRGGBBAA (8) or stray digits — falls through to a FormatException reporting the offending value.
Source
Thrown at src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/Utils/ColorCodeParser.cs:56
b = Convert.ToByte(new string(colorCode[4], 2), 16);
}
else if (colorCode.Length == 7)
{
a = 0xFF;
r = Convert.ToByte(colorCode.Substring(1, 2), 16);
g = Convert.ToByte(colorCode.Substring(3, 2), 16);
b = Convert.ToByte(colorCode.Substring(5, 2), 16);
}
else if (colorCode.Length == 9)
{
a = Convert.ToByte(colorCode.Substring(1, 2), 16);
r = Convert.ToByte(colorCode.Substring(3, 2), 16);
g = Convert.ToByte(colorCode.Substring(5, 2), 16);
b = Convert.ToByte(colorCode.Substring(7, 2), 16);
}
else
{
throw new FormatException($"Failed to parse color code: #{colorCode}");
}
return $"{a}, {r}, {g}, {b}";
}
}
}
View on GitHub (pinned to 0418340488)
Solutions
- Use a supported form: #RGB, #RRGGBB, or #AARRGGBB.
- Double-check the digit count and that alpha precedes RGB (AARRGGBB) for 9-char literals.
- Validate the literal length before generation.
Example fix
<!-- before --> <SolidColorBrush Color="#FF00000" /> <!-- after --> <SolidColorBrush Color="#FFFF0000" />
Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidHexColor(string s)
=> s != null && s.StartsWith("#") && (s.Length == 4 || s.Length == 7 || s.Length == 9)
&& s.Skip(1).All(c => "0123456789aAbBcCdDeEfF".IndexOf(c) >= 0); Prevention
- Use only #RGB (4), #RRGGBB (7), or #AARRGGBB (9) forms.
- For 8-channel data remember Uno expects AARRGGBB, not RGBA.
When it happens
Trigger: A hex color literal whose length (excluding the '#') is not 3, 6, or 8 — e.g. '#FF00' (5 total), '#12345' (6 total), '#1234567' (8 total), '#123456789' (10 total).
Common situations: Typo in a XAML color such as a dropped/doubled digit; pasting an 8-digit RGBA when ARGB was expected; truncation during copy/paste.
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
- Color code must start with #
- The {0} string passed in the colorString argument is not a r
- The {0} string passed in the colorString argument is not a r
- The {0} string passed in the colorString argument is not a r
- Expected end of x:Bind expression or start of argument list.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/ae8a1d8448e81e74.
Report an issue: GitHub.