{"record":{"id":"b498ebb292eb5f30","repo":"AvaloniaUI/Avalonia","slug":"invalid-color-string-s-tostring","errorCode":null,"errorMessage":"Invalid color string: '{s.ToString()}'.","messagePattern":"Invalid color string: '(.+?)'\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Color.cs","lineNumber":135,"sourceCode":"                return color;\n            }\n\n            throw new FormatException($\"Invalid color string: '{s}'.\");\n        }\n\n        /// <summary>\n        /// Parses a color string.\n        /// </summary>\n        /// <param name=\"s\">The color string.</param>\n        /// <returns>The <see cref=\"Color\"/>.</returns>\n        public static Color Parse(ReadOnlySpan<char> s)\n        {\n            if (TryParse(s, out Color color))\n            {\n                return color;\n            }\n\n            throw new FormatException($\"Invalid color string: '{s.ToString()}'.\");\n        }\n\n        /// <summary>\n        /// Parses a color string.\n        /// </summary>\n        /// <param name=\"s\">The color string.</param>\n        /// <param name=\"color\">The parsed color</param>\n        /// <returns>The status of the operation.</returns>\n        public static bool TryParse(string? s, out Color color)\n        {\n            color = default;\n\n            if (string.IsNullOrEmpty(s))\n            {\n                return false;\n            }\n\n            if (s[0] == '#' &&","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Color.cs#L117-L153","documentation":"The ReadOnlySpan<char> overload of Color.Parse throws the same FormatException as the string overload, but for span inputs. It exists for performance-sensitive parsing paths (e.g. XAML/JSON parsers) that avoid allocating a string. The error message interpolates s.ToString() so the offending input is visible.","triggerScenarios":"Calling Color.Parse(ReadOnlySpan<char> s) where the span content is not a valid color. Often reached from internal XAML/markup parsers or when slicing a larger buffer.","commonSituations":"Internal parser feeding a malformed token. A XAML color attribute with a typo. Slicing a buffer incorrectly leaving non-color characters.","solutions":["Use Color.TryParse(ReadOnlySpan<char>, out Color) to avoid the throw on bad input.","Validate the span content matches a known color/hex format before calling Parse.","Ensure upstream tokenization produces a clean color token."],"exampleFix":"// before\nvar color = Color.Parse(tokenSpan);\n\n// after\nif (!Color.TryParse(tokenSpan, out var color))\n    color = Colors.Black;","handlingStrategy":"validation","validationCode":"if (!Color.TryParse(spanInput, out var color))\n    color = Colors.Black;","typeGuard":null,"tryCatchPattern":"Color color;\ntry { color = Color.Parse(spanInput); }\ncatch (FormatException) { color = Colors.Black; }","preventionTips":["Use the span-based TryParse overload to avoid both allocations and exceptions.","Ensure upstream tokenizers produce clean color tokens.","Handle parse failures gracefully in high-throughput parsing paths."],"tags":["color","parsing","media","span","performance"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}