dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Color_NullColorContext, null)
Error message
SR.Format(SR.Color_NullColorContext, null)
What it means
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.
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
Example fix
// before
float[] vals = color.GetNativeColorValues();
// after
float[] vals = color.ColorContext != null ? color.GetNativeColorValues() : new float[]{ color.ScR, color.ScG, color.ScB }; Defensive patterns
Strategy: type-guard
Validate before calling
float[] GetValues(Color c) => c.ColorContext != null ? c.GetNativeColorValues() : new[] { c.ScR, c.ScG, c.ScB }; Type guard
bool HasColorContext(Color c) => c.ColorContext != null;
Try / catch
try { var vals = color.GetNativeColorValues(); } catch (InvalidOperationException) { /* sRGB color: use ScR/ScG/ScB instead */ } Prevention
- 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
When it happens
Trigger: Calling GetNativeColorValues() on a Color created via FromRgb/FromScRgb (sRGB) or default(Color), which have context == null.
Common situations: 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.
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
- SR.Format(SR.Color_ColorContextNotsRGB_or_scRGB, null)
- Animation_Invalid_DefaultValue
- Cannot remove signature from read-only file.
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a5e1b1bfab4cc9e4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs:380
sRgbColor.r = ScRgbTosRgb(scRgbColor.r);
sRgbColor.g = ScRgbTosRgb(scRgbColor.g);
sRgbColor.b = ScRgbTosRgb(scRgbColor.b);
//add code to check if context is null and if not null then clamp native values
}
///<summary>
/// GetNativeColorValues - return color values from color context
///</summary>
public float[] GetNativeColorValues()
{
if (context != null)
{
return (float[])nativeColorValue.Clone();
}
else
{
throw new InvalidOperationException(SR.Format(SR.Color_NullColorContext, null));
}
}
#endregion Public Methods
//------------------------------------------------------
//
// Public Operators
//
//------------------------------------------------------
#region Public Operators
///<summary>
/// Addition operator - Adds each channel of the second color to each channel of the
/// first and returns the result
///</summary>
public static Color operator +(Color color1, Color color2)
{
if (color1.context == null && color2.context == null)View on GitHub (pinned to 81131a70a4)