dotnet/wpf · error · InvalidOperationException

SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null)

Error message

SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null)

What it means

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.

Solutions

  1. 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.
  2. Check Color.Context before mutation and convert to sRGB first (e.g. use the sRGB values read from the color).
  3. Catch InvalidOperationException and fall back to rebuilding the color from its sRGB representation.

Example fix

// before
contextualColor.R = 128; // InvalidOperationException for CMYK context
// after
var rgb = Color.FromArgb(contextualColor.A, contextualColor.R, contextualColor.G, contextualColor.B);
rgb.R = 128;
Defensive patterns

Strategy: validation

Validate before calling

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));

Type guard

bool IsStandardRgb(Color c) => c.Context is null || Equals(c.Context, new ColorContext(PixelFormats.Bgr32));

Try / catch

try { color.R = value; }
catch (InvalidOperationException) { var rgb = Color.FromArgb(color.A, color.R, color.G, color.B); rgb.R = value; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/163e9d0da31cc1b4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs:824

        /// <summary>
        /// R
        /// </summary>
        public byte R
        {
            get
            {
                return sRgbColor.r;
            }
            set
            {
                if (context == null || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.Srgb) || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.ScRgb))
                {
                    scRgbColor.r = sRgbToScRgb(value);
                    sRgbColor.r = value;
                }
                else
                {
                    throw new InvalidOperationException(SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null));
                }
            }
        }

        ///<value>The Green channel as a byte whose range is [0..255].
        /// the value is not allowed to be out of range</value><summary>
        /// G
        ///</summary>
        public byte G
        {
            get
            {
                return sRgbColor.g;
            }
            set
            {
                if (context == null || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.Srgb) || (context.ColorSpaceFamily == ColorContext.StandardColorSpace.ScRgb))
                {

View on GitHub (pinned to 81131a70a4)