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

  1. Use a supported form: #RGB, #RRGGBB, or #AARRGGBB.
  2. Double-check the digit count and that alpha precedes RGB (AARRGGBB) for 9-char literals.
  3. 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

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

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/ae8a1d8448e81e74. Report an issue: GitHub.