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.

What it means

Final fallback of the TitleBar parser: the string was not a recognized '#' hex length, not a valid 'sc#' form, and reflection on typeof(Colors) found no matching named color property. Anything that survives all earlier branches lands here.

Source

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

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

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

				throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", colorString));
			}

			var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(colorString);

			if (prop != null)
			{
				return (Color)prop.GetValue(null);
			}

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

	}
}

View on GitHub (pinned to 0418340488)

Solutions

  1. Use an exact name of a Colors member (e.g. 'Red', 'DodgerBlue'); check against typeof(Colors).GetProperties() for the valid set.
  2. Normalize casing/trim before the reflection lookup if you want case-insensitive named colors.
  3. Fall back to the hex form (#RRGGBB) which is unambiguous.
  4. Whitelist allowed names in the UI.

Example fix

// before
var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(colorString);
// after - case-insensitive lookup with explicit error listing valid names
var prop = typeof(Colors).GetTypeInfo().DeclaredProperties
    .FirstOrDefault(p => string.Equals(p.Name, colorString, StringComparison.OrdinalIgnoreCase));
if (prop is null)
{
    throw new FormatException($"'{colorString}' is not a known Colors name nor a hex/sc# color.");
}
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> KnownColorNames =
    typeof(Colors).GetTypeInfo().DeclaredProperties.Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
static bool IsKnownColorName(string s) => s != null && KnownColorNames.Contains(s);

Try / catch

try { var c = ParseColor(input); }
catch (FormatException ex) { _log.Warning($"Unknown color '{input}'."); c = Colors.Transparent; }

Prevention

When it happens

Trigger: Calling the parser with a bare name that is not a member of Windows.UI.Colors / Microsoft.UI.Colors (e.g. 'HotPink', 'transparent', 'grey' with wrong casing if case-sensitive), or a fully unrecognized token like 'banana' or an empty string.

Common situations: Typo in a named color, CSS color names that don't exist in the WinUI Colors type, casing differences, or a non-color string fed in from configuration.

Related errors


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