{"record":{"id":"163e9d0da31cc1b4","repo":"dotnet/wpf","slug":"sr-format-sr-color-colorcontextnotsrgb-or-scrgb-null","errorCode":null,"errorMessage":"SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null)","messagePattern":"SR\\.Format\\(SR\\.Color_ColorContextNotsRGB_or_scRGB, null\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs","lineNumber":824,"sourceCode":"        /// <summary>\n        /// R\n        /// </summary>\n        public byte R\n        {\n            get\n            {\n                return sRgbColor.r;\n            }\n            set\n            {\n                if (context == null || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.Srgb) || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.ScRgb))\n                {\n                    scRgbColor.r = sRgbToScRgb(value);\n                    sRgbColor.r = value;\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null));\n                }\n            }\n        }\n\n        ///<value>The Green channel as a byte whose range is [0..255].\n        /// the value is not allowed to be out of range</value><summary>\n        /// G\n        ///</summary>\n        public byte G\n        {\n            get\n            {\n                return sRgbColor.g;\n            }\n            set\n            {\n                if (context == null || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.Srgb) || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.ScRgb))\n                {","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs#L806-L842","documentation":"The Color.R byte setter throws InvalidOperationException when the color carries a ColorContext that is neither sRGB nor scRGB. Setting the 8-bit red channel requires an sRGB-based conversion that is only valid for those two standard color spaces.","triggerScenarios":"Assigning Color.R (byte) on a Color whose context is a non-standard profile (e.g. a CMYK or custom ICC profile loaded via new ColorContext(uri)), hitting the else branch at Color.cs:824.","commonSituations":"Editing channel values of colors extracted from ICC-profiled images or XPS documents; copying pixels from a CMYK bitmap into a mutable Color and trying to tweak R/G/B.","solutions":["Only set R/G/B on colors without a context or with an sRGB/scRGB context; create a new context-free color via FromArgb and modify that.","Check Color.Context before mutation and convert to sRGB first (e.g. use the sRGB values read from the color).","Catch InvalidOperationException and fall back to rebuilding the color from its sRGB representation."],"exampleFix":"// before\ncontextualColor.R = 128; // InvalidOperationException for CMYK context\n// after\nvar rgb = Color.FromArgb(contextualColor.A, contextualColor.R, contextualColor.G, contextualColor.B);\nrgb.R = 128;","handlingStrategy":"validation","validationCode":"bool canSetChannels(Color c) => c.Context == null || c.Context == new ColorContext(PixelFormats.Bgra32) || c.Context == new ColorContext(PixelFormats.Prgba64) == false && (c.Context.ProfileUri == null || IsStandardRgb(c.Context));","typeGuard":"bool IsStandardRgb(Color c) => c.Context is null || Equals(c.Context, new ColorContext(PixelFormats.Bgr32));","tryCatchPattern":"try { color.R = value; }\ncatch (InvalidOperationException) { var rgb = Color.FromArgb(color.A, color.R, color.G, color.B); rgb.R = value; }","preventionTips":["Check Color.Context before mutating channels","Convert profile-tagged colors to context-free copies for editing","Avoid channel edits on CMYK/custom-profile colors"],"tags":["wpf","color","color-context","srgb"],"backgroundTag":"invalid-state-transition","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"}