{"record":{"id":"edc231fa3bb81f97","repo":"Intervention/image","slug":"failed-to-import-color-color-class-to-colorspac-edc231","errorCode":null,"errorMessage":"Failed to import color {color_class} to {colorspace_class}","messagePattern":"Failed to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Oklab/Colorspace.php","lineNumber":124,"sourceCode":"        $b = $rgbToLinear($b);\n\n        $l = 0.4122214708 * $r + 0.5363325363 * $g + 0.0514459929 * $b;\n        $m = 0.2119034982 * $r + 0.6806995451 * $g + 0.1073969566 * $b;\n        $s = 0.0883024619 * $r + 0.2817188376 * $g + 0.6299787005 * $b;\n\n        $l = $cbrt($l);\n        $m = $cbrt($m);\n        $s = $cbrt($s);\n\n        try {\n            return new Color(\n                0.2104542553 * $l + 0.7936177850 * $m - 0.0040720468 * $s,\n                1.9779984951 * $l - 2.4285922050 * $m + 0.4505937099 * $s,\n                0.0259040371 * $l + 0.7827717662 * $m - 0.8086757660 * $s,\n                $color->alpha()->normalized(),\n            );\n        } catch (InvalidArgumentException $e) {\n            throw new ColorException(\n                'Failed to import color ' . $color::class . ' to ' . $this::class,\n                previous: $e,\n            );\n        }\n    }\n\n    /**\n     * Import given OKLCH color OKLAB colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importOklchColor(OklchColor $color): OklabColor\n    {\n        $hRad = deg2rad($color->hue()->value());\n\n        try {\n            return new Color(\n                $color->lightness()->value(),","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Oklab/Colorspace.php#L106-L142","documentation":"This ColorException wraps an InvalidArgumentException raised while constructing the OKLAB color during an RGB to OKLAB conversion (src/Colors/Oklab/Colorspace.php:116-128). The matrix math maps normalized RGB into lightness (0..1) and a/b axes (-0.4..0.4); a channel constructor only rejects values outside those bounds, so with a healthy RgbColor this is a defensive path that should not fire.","triggerScenarios":"Converting an RgbColor whose channels report normalized values outside 0..1 (possible with a corrupted or custom channel object) via $rgb->toColorspace(Oklab\\Colorspace::class), producing computed OKLAB coordinates beyond the channel limits.","commonSituations":"Almost exclusively an internal/edge failure: corrupted color objects, custom channel implementations violating bounds, or numerical drift from non-standard channel min/max overrides; not caused by normal usage.","solutions":["Inspect getPrevious() to see which channel and range was violated","Verify the source RGB color's channels: $color->red()->normalized() must be within 0..1","Rebuild the color from known-good values (e.g. re-parse from hex) and retry","If reproducible with plain valid colors, file a library bug including both channel values"],"exampleFix":"// before\n$oklab = $rgb->toColorspace(Oklab\\Colorspace::class); // ColorException: Failed to import color ...\n\n// after\n// ensure the source color is well-formed, then convert\n$rgb = Rgb\\Color::create($r, $g, $b); // re-validated 0..255 values\n$oklab = $rgb->toColorspace(Oklab\\Colorspace::class);","handlingStrategy":"try-catch","validationCode":"// Guard the source color's normalized channels before converting\n$channels = [$rgb->red(), $rgb->green(), $rgb->blue()];\nforeach ($channels as $ch) {\n    if ($ch->normalized() < 0.0 || $ch->normalized() > 1.0) {\n        // rebuild the color from known-good values before converting\n    }\n}","typeGuard":"function hasNormalizedRgbChannels(RgbColor $color): bool\n{\n    foreach ([$color->red(), $color->green(), $color->blue()] as $ch) {\n        $n = $ch->normalized();\n        if ($n < 0.0 || $n > 1.0) { return false; }\n    }\n    return true;\n}","tryCatchPattern":"try {\n    $oklab = $rgb->toColorspace(Oklab\\Colorspace::class);\n} catch (ColorException $e) {\n    // practically an internal error: log getPrevious() and rebuild the source color\n}","preventionTips":["Rebuild colors from hex or create() before converting when provenance is unclear","Avoid custom channel subclasses with inconsistent min()/max()","Log chained exceptions (getPrevious) for any ColorException"],"tags":["color","oklab","colorspace","rgb","conversion","php"],"backgroundTag":"colorspace-conversion-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}