{"record":{"id":"429188d0b6b880c9","repo":"Intervention/image","slug":"unable-to-sort-by-color-channel-channel","errorCode":null,"errorMessage":"Unable to sort by color channel {channel}","messagePattern":"Unable to sort by color channel (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Palette.php","lineNumber":339,"sourceCode":"            Cmyk\\Channels\\Key::class,\n            Cmyk\\Channels\\Alpha::class => Cmyk\\Colorspace::class,\n            Hsl\\Channels\\Hue::class,\n            Hsl\\Channels\\Saturation::class,\n            Hsl\\Channels\\Luminance::class,\n            Hsl\\Channels\\Alpha::class => Hsl\\Colorspace::class,\n            Hsv\\Channels\\Hue::class,\n            Hsv\\Channels\\Saturation::class,\n            Hsv\\Channels\\Value::class,\n            Hsv\\Channels\\Alpha::class => Hsv\\Colorspace::class,\n            Oklab\\Channels\\Lightness::class,\n            Oklab\\Channels\\A::class,\n            Oklab\\Channels\\B::class,\n            Oklab\\Channels\\Alpha::class => Oklab\\Colorspace::class,\n            Oklch\\Channels\\Lightness::class,\n            Oklch\\Channels\\Chroma::class,\n            Oklch\\Channels\\Hue::class,\n            Oklch\\Channels\\Alpha::class => Oklch\\Colorspace::class,\n            default => throw new InvalidArgumentException('Unable to sort by color channel ' . $channel),\n        };\n\n        // create indexed array to track original colors\n        $originalBins = $this->bins;\n        $indices = array_keys($originalBins);\n\n        // sort indices based on channel values in the sort colorspace; each\n        // color is converted individually because palettes may mix colorspaces\n        usort(\n            $indices,\n            function (\n                string $indexA,\n                string $indexB,\n            ) use (\n                $channel,\n                $sortColorspace,\n                $originalBins,\n            ): int {","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Palette.php#L321-L357","documentation":"The value passed to sortByChannel() is an existing class, but not one of the channel classes Intervention Image knows how to sort by. A match expression maps only the built-in RGB, CMYK, HSL, HSV, OkLab and OkLCH channel classes to their sort colorspace; anything else (a custom channel class, a color class, a colorspace class) falls through to the default arm and throws.","triggerScenarios":"sortByChannel(\\Intervention\\Image\\Colors\\Rgb\\Color::class) (a color class, not a channel class), a user-defined class implementing ColorChannelInterface, or any FQCN outside the supported channel list matched in Palette::sortByChannel().","commonSituations":"Extending the library with a custom colorspace/channel and assuming sortByChannel() picks it up; passing Rgb\\Colorspace::class instead of a channel class; expecting a channel from one colorspace to sort a palette stored in another without conversion.","solutions":["Pass one of the built-in channel classes, e.g. Rgb\\Channels\\Red::class, Hsv\\Channels\\Value::class, Oklch\\Channels\\Hue::class","Sort by a channel object from an existing color: $palette->sortByChannel($color->channel(Red::class))","For custom channels, convert the palette first with $palette->toColorspace(...) and sort by a supported native channel of that colorspace","For arbitrary sort criteria, use $palette->toArray() with usort() instead"],"exampleFix":"// before\n$palette->sortByChannel(\\App\\Colors\\CustomChannel::class);\n\n// after\n$palette->sortByChannel(\\Intervention\\Image\\Colors\\Rgb\\Channels\\Red::class);","handlingStrategy":"validation","validationCode":"use Intervention\\Image\\Colors\\Rgb\\Channels\\Red;\nuse Intervention\\Image\\Colors\\Rgb\\Channels\\Green;\nuse Intervention\\Image\\Colors\\Rgb\\Channels\\Blue;\n\n$supported = [Red::class, Green::class, Blue::class /* + other colorspace channels */];\nif (!in_array($channel, $supported, true)) {\n    throw new \\InvalidArgumentException('Channel not sortable: ' . $channel);\n}\n$palette->sortByChannel($channel);","typeGuard":"function isSortableChannel(string $channel): bool\n{\n    return in_array($channel, [\n        \\Intervention\\Image\\Colors\\Rgb\\Channels\\Red::class,\n        \\Intervention\\Image\\Colors\\Rgb\\Channels\\Green::class,\n        \\Intervention\\Image\\Colors\\Rgb\\Channels\\Blue::class,\n        \\Intervention\\Image\\Colors\\Rgb\\Channels\\Alpha::class,\n        // CMYK, HSL, HSV, OkLab, OkLCH channel classes ...\n    ], true);\n}","tryCatchPattern":"try {\n    $palette->sortByChannel($channel);\n} catch (\\Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    // channel unsupported for sorting; fall back to presence order\n    $palette->sortByPresence();\n}","preventionTips":["Only pass built-in channel classes from the six supported colorspaces","Take channel objects from real colors: $color->channel(Red::class)","For custom criteria, sort $palette->toArray() with usort() yourself"],"tags":["palette","sorting","color-channels","php"],"backgroundTag":"unsupported-argument-value","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}