{"record":{"id":"d039c7ecda1acd1c","repo":"Intervention/image","slug":"failed-to-decode-hex-color","errorCode":null,"errorMessage":"Failed to decode hex color","messagePattern":"Failed to decode hex color","errorType":"exception","errorClass":"ColorDecoderException","httpStatus":null,"severity":"info","filePath":"src/Colors/Rgb/Decoders/HexColorDecoder.php","lineNumber":68,"sourceCode":"    public function decode(mixed $input): ColorInterface\n    {\n        if (preg_match(static::PATTERN, $input, $matches) !== 1) {\n            throw new InvalidArgumentException('Hex color has an invalid format');\n        }\n\n        // split into hex chunks\n        $values = match (strlen($matches['hex'])) {\n            3, 4 => str_split($matches['hex']),\n            6, 8 => str_split($matches['hex'], 2),\n            default => throw new InvalidArgumentException('Hex color has an incorrect length'),\n        };\n\n        // convert to decimal\n        $values = array_map(function (string $value): int {\n            return match (strlen($value)) {\n                1 => (int) hexdec($value . $value),\n                2 => (int) hexdec($value),\n                default => throw new ColorDecoderException('Failed to decode hex color'),\n            };\n        }, $values);\n\n        // normalize\n        $values = count($values) === 3 ? array_pad($values, 4, 255) : $values;\n        $values = array_map(fn(int $value): float => $value / 255, $values);\n\n        return Rgb::colorFromNormalized($values);\n    }\n}\n","sourceCodeStart":50,"sourceCodeEnd":79,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Decoders/HexColorDecoder.php#L50-L79","documentation":"While converting hex chunks to decimals, HexColorDecoder::decode() throws ColorDecoderException if any chunk is neither 1 nor 2 characters long. Because chunks come from str_split() of a 3/4-digit (1-char chunks) or 6/8-digit (2-char chunks) string, this branch is a defensive guard that cannot be reached through the built-in pattern; it would require a subclass overriding PATTERN plus the length match to produce odd chunk sizes.","triggerScenarios":"Practically unreachable via public APIs; only a heavily customized HexColorDecoder subclass that changes both PATTERN and the length match could produce malformed chunks.","commonSituations":"Deep forks of the decoder pipeline; none in normal usage.","solutions":["No user action required for stock usage","If you maintain a decoder subclass, keep chunk sizes at 1 or 2 characters","Treat occurrences as a signal that local decoder overrides diverged from upstream"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $color = $decoder->decode($hex);\n} catch (\\Intervention\\Image\\Exceptions\\ColorDecoderException $e) {\n    // defensive branch; if hit, your decoder overrides produce malformed chunks\n    $color = \\Intervention\\Image\\Colors\\Rgb\\Color::create(0, 0, 0);\n}","preventionTips":["No guard needed for stock usage; the branch is unreachable with the built-in pattern","If you subclass HexColorDecoder, keep chunk sizes at 1 or 2 characters","Treat this exception as a marker that local decoder overrides diverged from upstream"],"tags":["hex-color","rgb","decoder","defensive-check","php"],"backgroundTag":"invalid-hex-color","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}