{"record":{"id":"9c088dd8f8712801","repo":"dotnet/wpf","slug":"new-notsupportedexception-only-support-color-spaces-with-3-4","errorCode":null,"errorMessage":"new NotSupportedException() // Only support color spaces with 3,4,5,6,7,8 channels","messagePattern":"new NotSupportedException\\(\\) // Only support color spaces with 3,4,5,6,7,8 channels","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransform.cs","lineNumber":134,"sourceCode":"        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        private void InitializeICM()\n        {\n            _colorTransformHelper = new ColorTransformHelper();\n        }\n\n        private long ICM2Color(Span<float> srcValue)\n        {\n            long colorValue;\n\n            if (srcValue.Length < 3 || srcValue.Length > 8)\n            {\n                throw new NotSupportedException(); // Only support color spaces with 3,4,5,6,7,8 channels\n            }\n\n            if (srcValue.Length <= 4)\n            {\n                Span<UInt16> channel = stackalloc UInt16[4];\n\n                for (int i = 0; i < srcValue.Length; i++)\n                {\n                    if (srcValue[i] >= 1.0)// this fails for values above 1.0 and below 0.0\n                    {\n                        channel[i] = 0xffff;\n                    }\n                    else if (srcValue[i] <= 0.0)\n                    {\n                        channel[i] = 0x0;\n                    }\n                    else\n                    {","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorTransform.cs#L116-L152","documentation":"ColorTransform.ICM2Color throws NotSupportedException when the source color span has fewer than 3 or more than 8 channels. The ICM color translation path only supports color spaces with 3-8 channels, mapping them into packed UInt16 channels.","triggerScenarios":"Calling ColorTransform.ValueToColor (via inputColor) on a color whose pixel/color format yields srcValue outside 3..8 channels, e.g. grayscale (1-2 channel) or exotic >8-channel formats.","commonSituations":"Translating colors from grayscale or CMYK-extended/hi-fi profiles with unusual channel counts; custom pixel formats fed into the CMS pipeline; 16+ channel spectral profiles.","solutions":["Convert the source color to an RGB/CMYK (3-8 channel) format before applying the transform","Use a supported pixel format (Bgra32, Rgba64, Cmyk32, etc.) when constructing colors for ColorTransform","Catch NotSupportedException and handle grayscale/special formats with a dedicated path","Check PixelFormat.ChannelsCount of the source format before invoking the transform"],"exampleFix":"// before\nvar color = transform.ValueToColor(grayFormat, span);\n// after\nif (span.Length < 3 || span.Length > 8) { throw new InvalidOperationException(\"Convert to a 3-8 channel format first\"); } var color = transform.ValueToColor(format, span);","handlingStrategy":"validation","validationCode":"if (srcValue.Length < 3 || srcValue.Length > 8) throw new InvalidOperationException($\"Unsupported channel count {srcValue.Length}; convert to a 3-8 channel color space first\");","typeGuard":"bool IsSupportedChannelCount(PixelFormat fmt) => fmt.BitsPerPixel >= 24 && GetChannels(fmt) is >= 3 and <= 8;","tryCatchPattern":"try { color = transform.ValueToColor(format, span); } catch (NotSupportedException) { color = TranslateViaIntermediateRgb(span); }","preventionTips":["Check PixelFormat channel count before CMS translation","Convert grayscale/hi-fi formats to RGB/CMYK first","Restrict inputs to known supported formats (3-8 channels)"],"tags":["wpf","color-management","unsupported"],"backgroundTag":"unsupported-operation","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"}