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 (sc#[scA,]scR,scG,scB).

What it means

Thrown by the same TitleBar parser when the string starts with 'sc#' (WinUI scRGB double format) but the comma-split does not produce exactly 3 (sc#r,g,b) or 4 (sc#a,r,g,b) values. The parser then has no branch to handle the wrong arity.

Source

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

				{
					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)
				{
					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. Supply exactly 3 components (sc#R,G,B, each 0..1) or 4 (sc#A,R,G,B).
  2. Ensure the decimal separator is '.' (invariant culture) — double.Parse here uses current culture, so a ',' separator can collide with decimal commas; use double.Parse(values[i], CultureInfo.InvariantCulture) to be safe.
  3. Strip trailing commas and whitespace before splitting.
  4. If the sc# form is not needed, use the plain hex form instead.

Example fix

// before
var values = colorString.Split(',');
// after - split with options and invariant parse
var values = colorString.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (values.Length is not 3 and not 4)
{
    throw new FormatException($"sc# needs 3 or 4 components, got {values.Length} in '{colorString}'.");
}
var scR = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture);
Defensive patterns

Strategy: validation

Validate before calling

static bool TryParseScRgb(string s, out double[] comps)
{
    comps = null;
    if (s == null || s.Length <= 3 || s[0]!='s' || s[1]!='c' || s[2]!='#') return false;
    var parts = s.Substring(3).Split(',', StringSplitOptions.TrimEntries);
    if (parts.Length is not 3 and not 4) return false;
    comps = parts.Select(p => double.Parse(p, CultureInfo.InvariantCulture)).ToArray();
    return comps.All(d => d is >= 0 and <= 1);
}

Try / catch

try { var c = ParseColor(input); }
catch (FormatException ex) when (ex.Message.Contains("sc#")) { /* prompt user for valid sc# form */ }

Prevention

When it happens

Trigger: Passing 'sc#0.5' (1 value), 'sc#0.5,0.2' (2 values), 'sc#0.1,0.2,0.3,0.4,0.5' (5+ values), or a value with a trailing comma that produces an empty segment count mismatch.

Common situations: Confusing the sc# double form with the hex form; using localized decimal separators (',' as decimal) that double.Parse misreads, or supplying RGB without alpha but the components are separated incorrectly.

Related errors


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