{"record":{"id":"20cde2a286269639","repo":"AvaloniaUI/Avalonia","slug":"invalid-hsl-color-string-s","errorCode":null,"errorMessage":"Invalid HSL color string: '{s}'.","messagePattern":"Invalid HSL color string: '(.+?)'\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/HslColor.cs","lineNumber":226,"sourceCode":"\n        /// <summary>\n        /// Parses an HSL color string.\n        /// </summary>\n        /// <param name=\"s\">The HSL color string to parse.</param>\n        /// <returns>The parsed <see cref=\"HslColor\"/>.</returns>\n        public static HslColor Parse(string s)\n        {\n            if (s is null)\n            {\n                throw new ArgumentNullException(nameof(s));\n            }\n\n            if (TryParse(s, out HslColor hslColor))\n            {\n                return hslColor;\n            }\n\n            throw new FormatException($\"Invalid HSL color string: '{s}'.\");\n        }\n\n        /// <summary>\n        /// Parses an HSL color string.\n        /// </summary>\n        /// <param name=\"s\">The HSL color string to parse.</param>\n        /// <param name=\"hslColor\">The parsed <see cref=\"HslColor\"/>.</param>\n        /// <returns>True if parsing was successful; otherwise, false.</returns>\n        public static bool TryParse(string? s, out HslColor hslColor)\n        {\n            bool prefixMatched = false;\n\n            hslColor = default;\n\n            if (s is null)\n            {\n                return false;\n            }","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/HslColor.cs#L208-L244","documentation":"HslColor.Parse parses a 'hsl(...)' / 'hsla(...)' CSS-style string into an HslColor. After stripping the prefix and splitting channels it must satisfy TryParse's grammar; on any malformed token, prefix mismatch, or out-of-range channel it returns false and Parse throws FormatException including the offending string for diagnostics.","triggerScenarios":"Passing 'hsl(240,100%,50%)' missing a channel; 'hsl(240 100 50)' without the % required on saturation/lightness; an 'rgb(...)' string fed to HslColor.Parse by mistake; trailing characters after the closing paren; commas vs spaces grammar mismatch.","commonSituations":"Loading theme colors from a user-edited config or CSS file with inconsistent notation; copy-pasting a hex color (#336699) into a field typed as HSL; binding a textbox directly to HslColor.Parse without validation.","solutions":["Call HslColor.TryParse(s, out var c) first and only fall back to Parse for the exception path.","Normalize the input: ensure hsl()/hsla() prefix, comma separators, and % on S and L.","If accepting hex/RGB too, dispatch by prefix to Color.Parse, HslColor.Parse, or HsvColor.Parse.","Show the user the exact failing string in your UI error message."],"exampleFix":"// before\nvar c = HslColor.Parse(userInput); // throws on any malformed string\n\n// after\nif (!HslColor.TryParse(userInput?.Trim(), out var c))\n    throw new ArgumentException($\"'{userInput}' is not a valid hsl()/hsla() color.\");\n// or accept multiple formats:\nstatic object ParseColor(string s) => s.StartsWith(\"hsl\") ? (object)HslColor.Parse(s)\n    : s.StartsWith(\"hsv\") ? HsvColor.Parse(s)\n    : Color.Parse(s);","handlingStrategy":"type-guard","validationCode":"if (!HslColor.TryParse(s?.Trim(), out var c))\n    throw new ArgumentException($\"'{s}' is not a valid hsl()/hsla() color.\");","typeGuard":"static bool IsHslColorString(string? s)\n{\n    var t = s?.Trim();\n    return t != null\n        && (t.StartsWith(\"hsl(\", StringComparison.OrdinalIgnoreCase)\n            || t.StartsWith(\"hsla(\", StringComparison.OrdinalIgnoreCase))\n        && t.EndsWith(\")\")\n        && HslColor.TryParse(t, out _);\n}","tryCatchPattern":"try { return HslColor.Parse(s); }\ncatch (FormatException ex) { throw new ArgumentException($\"Bad HSL: '{s}'\", ex); }","preventionTips":["Use TryParse for user-supplied color strings.","Dispatch by prefix when multiple notations are accepted.","Normalize separators and % suffixes before parsing."],"tags":["color","hsl","parsing","format-exception","argument-validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}