{"record":{"id":"a5e1b1bfab4cc9e4","repo":"dotnet/wpf","slug":"sr-format-sr-color-nullcolorcontext-null","errorCode":null,"errorMessage":"SR.Format(SR.Color_NullColorContext, null)","messagePattern":"SR\\.Format\\(SR\\.Color_NullColorContext, null\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs","lineNumber":380,"sourceCode":"            sRgbColor.r = ScRgbTosRgb(scRgbColor.r);\n            sRgbColor.g = ScRgbTosRgb(scRgbColor.g);\n            sRgbColor.b = ScRgbTosRgb(scRgbColor.b);\n\n            //add code to check if context is null and if not null then clamp native values\n        }\n\n        ///<summary>\n        /// GetNativeColorValues - return color values from color context\n        ///</summary>\n        public float[] GetNativeColorValues()\n        {\n            if (context != null)\n            {\n                return (float[])nativeColorValue.Clone();\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.Color_NullColorContext, null));\n            }\n        }\n        #endregion Public Methods\n\n        //------------------------------------------------------\n        //\n        //  Public Operators\n        //\n        //------------------------------------------------------\n\n        #region Public Operators\n        ///<summary>\n        /// Addition operator - Adds each channel of the second color to each channel of the\n        /// first and returns the result\n        ///</summary>\n        public static Color operator +(Color color1, Color color2)\n        {\n            if (color1.context == null && color2.context == null)","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs#L362-L398","documentation":"Color.GetNativeColorValues returns the profile-space channel values only when the color has an associated ColorContext. This InvalidOperationException is thrown when the color's context is null — i.e. the color is an sRGB color with no ICC profile, so no native color values exist.","triggerScenarios":"Calling GetNativeColorValues() on a Color created via FromRgb/FromScRgb (sRGB) or default(Color), which have context == null.","commonSituations":"Iterating a palette that mixes sRGB colors with profile-based colors and calling GetNativeColorValues on all of them; colors round-tripped through XAML losing their context.","solutions":["Only call GetNativeColorValues when the Color's ColorContext property is non-null","For sRGB colors, derive channel values from R/G/B (or ScR/ScG/ScB) instead","Recreate the color with Color.FromAValues/FromProfile to attach a context"],"exampleFix":"// before\n float[] vals = color.GetNativeColorValues();\n// after\n float[] vals = color.ColorContext != null ? color.GetNativeColorValues() : new float[]{ color.ScR, color.ScG, color.ScB };","handlingStrategy":"type-guard","validationCode":"float[] GetValues(Color c) => c.ColorContext != null ? c.GetNativeColorValues() : new[] { c.ScR, c.ScG, c.ScB };","typeGuard":"bool HasColorContext(Color c) => c.ColorContext != null;","tryCatchPattern":"try { var vals = color.GetNativeColorValues(); } catch (InvalidOperationException) { /* sRGB color: use ScR/ScG/ScB instead */ }","preventionTips":["Check ColorContext != null before calling GetNativeColorValues","Track whether a Color was created from a profile or from RGB","Don't assume contexts survive XAML/serialization round-trips"],"tags":["wpf","color","invalid-operation","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"}