{"record":{"id":"1f33e06dde14a946","repo":"AvaloniaUI/Avalonia","slug":"invalid-brush-string-s","errorCode":null,"errorMessage":"Invalid brush string: '{s}'.","messagePattern":"Invalid brush string: '(.+?)'\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Brush.cs","lineNumber":88,"sourceCode":"            _ = s ?? throw new ArgumentNullException(nameof(s));\n\n            if (s.Length > 0)\n            {\n                // Attempt to get a cached known brush first\n                // This is a performance optimization for known colors\n                var brush = KnownColors.GetKnownBrush(s);\n                if (brush != null)\n                {\n                    return brush;\n                }\n\n                if (Color.TryParse(s, out Color color))\n                {\n                    return new ImmutableSolidColorBrush(color);\n                }\n            }\n\n            throw new FormatException($\"Invalid brush string: '{s}'.\");\n        }\n\n        protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)\n        {\n            if (change.Property == TransformProperty) \n                _resource.ProcessPropertyChangeNotification(change);\n\n            RegisterForSerialization();\n\n            base.OnPropertyChanged(change);\n        }\n        \n        private protected void RegisterForSerialization() =>\n            _resource.RegisterForInvalidationOnAllCompositors(this);\n\n        private protected bool IsOnCompositor(Compositor c) => _resource.TryGetForCompositor(c) != null;\n\n        private CompositorResourceHolder<ServerCompositionSimpleBrush> _resource;","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Brush.cs#L70-L106","documentation":"Brush.Parse throws a FormatException when the input string is neither a known color name nor a parseable color value. Brush.Parse first checks known colors, then attempts Color.TryParse; if both fail, the string is not a valid brush specification. Unlike WPF, Avalonia's Brush.Parse only accepts color representations (names, hex), not full brush syntax.","triggerScenarios":"Calling Brush.Parse(s) with a malformed color string or an unrecognized token. Examples: \"redd\", \"#GGG\", \"rgb(300,0,0)\", or a XAML resource key that isn't a color.","commonSituations":"Typo in a color name in XAML/code. Passing a resource key or theme token instead of a color string. Misunderstanding that Brush.Parse accepts only color values, not gradient/gradient-brush syntax. Loading color strings from a config file with bad data.","solutions":["Validate the string with Color.TryParse before passing to Brush.Parse.","Use a known color name or a valid hex string (#RGB, #RRGGBB, #AARRGGBB, #RRGGBBAA).","If the string comes from config/user input, wrap in try-catch for FormatException or use TryParse patterns."],"exampleFix":"// before\nvar brush = Brush.Parse(userInput);\n\n// after\nif (Color.TryParse(userInput, out var color))\n    var brush = new ImmutableSolidColorBrush(color);\nelse\n    // handle invalid input / show error","handlingStrategy":"validation","validationCode":"if (Color.TryParse(s, out var color))\n    brush = new ImmutableSolidColorBrush(color);\nelse\n    brush = Brushes.Transparent; // or throw a domain-specific error","typeGuard":null,"tryCatchPattern":"IBrush brush;\ntry { brush = Brush.Parse(s); }\ncatch (FormatException) { brush = Brushes.Transparent; }","preventionTips":["Validate color strings with Color.TryParse before Brush.Parse.","Treat Brush.Parse as accepting only color values, not gradient syntax.","Sanitize user/config-supplied color strings (trim, validate format)."],"tags":["brush","parsing","media","color","format"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}