unoplatform/uno · error · FormatException

The {0} string passed in the colorString argument is not a r

Error message

The {0} string passed in the colorString argument is not a recognized Color format.

What it means

Thrown by the TitleBar sample's local color parser when a '#'-prefixed string does not match any of the recognized hash lengths (9 = #AARRGGBB, 7 = #RRGGBB, 5 = #ARGB, 4 = #RGB). The switch on colorString.Length falls through to default. This is sample-internal parsing; it is not the framework's Color helper.

Source

Thrown at src/SamplesApp/SamplesApp.Samples/Windows_UI_ViewManagement/TitleBarColorTests.xaml.cs:116

							return Color.FromArgb(a, r, g, b);
						}

					case 4:
						{
							var cuint = Convert.ToUInt16(colorString.Substring(1), 16);
							var r = (byte)((cuint >> 8) & 0xf);
							var g = (byte)((cuint >> 4) & 0xf);
							var b = (byte)(cuint & 0xf);
							r = (byte)(r << 4 | r);
							g = (byte)(g << 4 | g);
							b = (byte)(b << 4 | b);

							return Color.FromArgb(255, r, g, b);
						}

					default:
						throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format.", colorString));
				}
			}

			if (colorString.Length > 3 && colorString[0] == 's' && colorString[1] == 'c' && colorString[2] == '#')
			{
				var values = colorString.Split(',');

				if (values.Length == 4)
				{
					var scA = double.Parse(values[0].Substring(3));
					var scR = double.Parse(values[1]);
					var scG = double.Parse(values[2]);
					var scB = double.Parse(values[3]);

					return Color.FromArgb((byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));
				}

				if (values.Length == 3)

View on GitHub (pinned to 0418340488)

Solutions

  1. Trim whitespace and validate the '#' prefix and length before the switch: only #RGB(4), #ARGB(5), #RRGGBB(7), #AARRGGBB(9) are accepted.
  2. If you need standard #RRGGBBAA (RGBA order) or CSS-style, normalize to one of the four supported shapes first.
  3. Provide a clear UI hint listing accepted formats near the input.
  4. Consider using the framework's Microsoft.UI.ColorHelper.TryParse or XAML converters instead of this sample parser.

Example fix

// before
var cuint = Convert.ToUInt16(colorString.Substring(1), 16);
// after - guard length/prefix so the default branch is never reached with a typo
colorString = colorString.Trim();
if (colorString.Length < 2 || colorString[0] != '#')
{
    throw new FormatException($"Expected a '#'-prefixed hex color, got '{colorString}'.");
}
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<int> ValidHashLengths = new(){4,5,7,9};
static bool IsHashColor(string s) =>
    s != null && s.Length >= 4 && s[0] == '#' && ValidHashLengths.Contains(s.Length)
    && s.Skip(1).All(c => "0123456789abcdefABCDEF".Contains(c));
// call: if (!IsHashColor(input)) report a UI error instead of throwing.

Try / catch

try { var c = ParseColor(input); }
catch (FormatException ex) { _log.Warning(ex.Message); c = Colors.Transparent; }

Prevention

When it happens

Trigger: Calling the parser with a malformed hex color: '#RGB' is 4 chars (valid) but '#RRGGBBAA' wrong-order, '#FF' (len 3, no default branch hit differently), '#12345' (len 6, falls to default), '#123456789' (len 10, default), or a string with trailing whitespace that changes the length.

Common situations: User types a color in the sample's input box and adds spaces, omits '#', uses an 8-digit ARGB expecting RGBA order, or pastes an alpha-first vs alpha-last value; culture-specific parsing differences are not the cause here (length check happens before numeric parse).

Related errors


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