{"record":{"id":"fa3bfca065189b71","repo":"unoplatform/uno","slug":"the-0-string-passed-in-the-colorstring-argument","errorCode":null,"errorMessage":"The {0} string passed in the colorString argument is not a recognized Color format.","messagePattern":"The (.+?) string passed in the colorString argument is not a recognized Color format\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/SamplesApp/SamplesApp.Samples/Windows_UI_ViewManagement/TitleBarColorTests.xaml.cs","lineNumber":116,"sourceCode":"\n\t\t\t\t\t\t\treturn Color.FromArgb(a, r, g, b);\n\t\t\t\t\t\t}\n\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvar cuint = Convert.ToUInt16(colorString.Substring(1), 16);\n\t\t\t\t\t\t\tvar r = (byte)((cuint >> 8) & 0xf);\n\t\t\t\t\t\t\tvar g = (byte)((cuint >> 4) & 0xf);\n\t\t\t\t\t\t\tvar b = (byte)(cuint & 0xf);\n\t\t\t\t\t\t\tr = (byte)(r << 4 | r);\n\t\t\t\t\t\t\tg = (byte)(g << 4 | g);\n\t\t\t\t\t\t\tb = (byte)(b << 4 | b);\n\n\t\t\t\t\t\t\treturn Color.FromArgb(255, r, g, b);\n\t\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new FormatException(string.Format(\"The {0} string passed in the colorString argument is not a recognized Color format.\", colorString));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (colorString.Length > 3 && colorString[0] == 's' && colorString[1] == 'c' && colorString[2] == '#')\n\t\t\t{\n\t\t\t\tvar values = colorString.Split(',');\n\n\t\t\t\tif (values.Length == 4)\n\t\t\t\t{\n\t\t\t\t\tvar scA = double.Parse(values[0].Substring(3));\n\t\t\t\t\tvar scR = double.Parse(values[1]);\n\t\t\t\t\tvar scG = double.Parse(values[2]);\n\t\t\t\t\tvar scB = double.Parse(values[3]);\n\n\t\t\t\t\treturn Color.FromArgb((byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));\n\t\t\t\t}\n\n\t\t\t\tif (values.Length == 3)","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SamplesApp/SamplesApp.Samples/Windows_UI_ViewManagement/TitleBarColorTests.xaml.cs#L98-L134","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Trim whitespace and validate the '#' prefix and length before the switch: only #RGB(4), #ARGB(5), #RRGGBB(7), #AARRGGBB(9) are accepted.","If you need standard #RRGGBBAA (RGBA order) or CSS-style, normalize to one of the four supported shapes first.","Provide a clear UI hint listing accepted formats near the input.","Consider using the framework's Microsoft.UI.ColorHelper.TryParse or XAML converters instead of this sample parser."],"exampleFix":"// before\nvar cuint = Convert.ToUInt16(colorString.Substring(1), 16);\n// after - guard length/prefix so the default branch is never reached with a typo\ncolorString = colorString.Trim();\nif (colorString.Length < 2 || colorString[0] != '#')\n{\n    throw new FormatException($\"Expected a '#'-prefixed hex color, got '{colorString}'.\");\n}","handlingStrategy":"validation","validationCode":"static readonly HashSet<int> ValidHashLengths = new(){4,5,7,9};\nstatic bool IsHashColor(string s) =>\n    s != null && s.Length >= 4 && s[0] == '#' && ValidHashLengths.Contains(s.Length)\n    && s.Skip(1).All(c => \"0123456789abcdefABCDEF\".Contains(c));\n// call: if (!IsHashColor(input)) report a UI error instead of throwing.","typeGuard":null,"tryCatchPattern":"try { var c = ParseColor(input); }\ncatch (FormatException ex) { _log.Warning(ex.Message); c = Colors.Transparent; }","preventionTips":["Trim and validate the '#' hex length before the switch.","Restrict UI input to the four accepted formats.","Consider using the framework Color parser for production code."],"tags":["xaml","color","parsing","samples","validation"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}