{"record":{"id":"4c60b50b6fccb18a","repo":"dotnet/wpf","slug":"sr-invalidhighlightcolor","errorCode":null,"errorMessage":"SR.InvalidHighlightColor","messagePattern":"SR\\.InvalidHighlightColor","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs","lineNumber":928,"sourceCode":"        {\n            CheckInputs(service);\n\n            // Get the selection for the viewer and wrap it in a TextRange\n            ITextSelection selection = GetTextSelection((FrameworkElement)service.Root);\n            Invariant.Assert(selection != null, \"TextSelection is null\");\n\n            // Cannot create an annotation with zero length text anchor\n            if (selection.IsEmpty)\n            {\n                throw new InvalidOperationException(SR.EmptySelectionNotSupported);\n            }\n\n            Nullable<Color> color = null;\n            if (highlightBrush != null)\n            {\n                SolidColorBrush brush = highlightBrush as SolidColorBrush;\n                if (brush == null)\n                    throw new ArgumentException(SR.InvalidHighlightColor, nameof(highlightBrush));\n\n                // Opacity less than 0 is treated as 0; greater than 1 is treated a 1.\n                byte alpha;\n                if (brush.Opacity <= 0)\n                    alpha = 0;\n                else if (brush.Opacity >= 1)\n                    alpha = brush.Color.A;\n                else\n                    alpha = (byte) (brush.Opacity * brush.Color.A);\n\n                color = Color.FromArgb(alpha, brush.Color.R, brush.Color.G, brush.Color.B);\n            }\n\n            // Create a range so we can move its ends without changing the selection\n            ITextRange anchor = new TextRange(selection.Start, selection.End);\n\n            Annotation highlight = ProcessHighlights(service, anchor, author, color, create);\n","sourceCodeStart":910,"sourceCodeEnd":946,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs#L910-L946","documentation":"CreateHighlightForSelection requires the highlightBrush to be a SolidColorBrush because the highlight color is stored as a byte alpha + color. Passing any other Brush type (LinearGradientBrush, ImageBrush, etc.) throws ArgumentException.","triggerScenarios":"Calling AnnotationHelper.CreateHighlightForSelection with a brush that is not a SolidColorBrush, e.g. a gradient or resource-based brush from theme dictionaries.","commonSituations":"Passing Brushes-derived resources that resolve to gradients, binding the brush parameter to a user-configured brush, or passing SystemColors brushes that are solid (fine) versus custom gradient definitions (not).","solutions":["Pass a SolidColorBrush, e.g. Brushes.Yellow or new SolidColorBrush(Color.FromArgb(255,255,255,0)).","Freeze the SolidColorBrush for performance: brush.Freeze().","If you have a generic Brush, extract its color or fall back to a default SolidColorBrush.","Catch ArgumentException and report that only solid brushes are supported."],"exampleFix":"// before\nvar brush = (Brush)FindResource(\"BrandGradient\");\nAnnotationHelper.CreateHighlightForSelection(service, brush);\n// after\nvar brush = new SolidColorBrush(Colors.Yellow);\nbrush.Freeze();\nAnnotationHelper.CreateHighlightForSelection(service, brush);","handlingStrategy":"type-guard","validationCode":"bool IsSolidBrush(Brush b) => b is SolidColorBrush;","typeGuard":"bool IsSolidBrush(Brush? b) => b is SolidColorBrush;","tryCatchPattern":"try { AnnotationHelper.CreateHighlightForSelection(service, brush); } catch (ArgumentException ex) { log.Error(\"Highlight brush must be a SolidColorBrush: \" + ex.Message); }","preventionTips":["Only pass SolidColorBrush (ideally frozen) to annotation highlight APIs.","Resolve theme resources with a fallback to a solid color.","Convert gradients to an averaged solid color before highlighting."],"tags":["wpf","annotations","brush","arguments"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}