{"record":{"id":"cab70b9b4315b35d","repo":"Intervention/image","slug":"hex-color-has-an-invalid-format","errorCode":null,"errorMessage":"Hex color has an invalid format","messagePattern":"Hex color has an invalid format","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Rgb/Decoders/HexColorDecoder.php","lineNumber":53,"sourceCode":"\n        // matching max. length & only hexadecimal\n        if (strlen($input) <= 8 && preg_match('/^[a-f\\d]+$/i', $input) === 1) {\n            return true;\n        }\n\n        return preg_match(static::PATTERN, $input) === 1;\n    }\n\n    /**\n     * Decode hexadecimal rgb colors with and without transparency.\n     *\n     * @throws InvalidArgumentException\n     * @throws ColorDecoderException\n     */\n    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","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Decoders/HexColorDecoder.php#L35-L71","documentation":"HexColorDecoder::decode() validates the input against a strict structural pattern: an optional '#' followed by exactly 3, 4, 6 or 8 hex digits. The gatekeeper supports() is much looser (any string starting with '#', or up to 8 chars that are all hex), so strings like '#1' or '#zz' reach decode() and fail the full pattern, throwing this InvalidArgumentException.","triggerScenarios":"Rgb\\Color::parse('#zzz'), parse('#1'), parse('f0') (too short), or input containing non-hex characters that still passed the loose supports() check. Also reachable by calling HexColorDecoder::decode() directly.","commonSituations":"User-typed color values ('#red', '#00ff0g'); truncation bugs cutting hex strings short ('#ff0' is fine, '#f0' is not); whitespace or newlines around the value surviving a trim-less pipeline.","solutions":["Provide a well-formed hex value: #f00, #f00f, #ff0000 or #ff0000ff (3, 4, 6 or 8 hex digits)","Trim whitespace and validate with your own regex before parsing: preg_match('/^#?([a-f\\d]{3}|[a-f\\d]{4}|[a-f\\d]{6}|[a-f\\d]{8})$/i', $input)","Catch InvalidArgumentException when parsing free-form user input and surface a form error"],"exampleFix":"// before\n$color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse('#00ff0g');\n\n// after\n$input = trim($request->input('color'));\nif (!preg_match('/^#?([a-f\\d]{3}|[a-f\\d]{4}|[a-f\\d]{6}|[a-f\\d]{8})$/i', $input)) {\n    throw new \\RuntimeException('Please provide a valid hex color');\n}\n$color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse($input);","handlingStrategy":"validation","validationCode":"$input = trim($input);\nif (!preg_match('/^#?([a-f\\d]{3}|[a-f\\d]{4}|[a-f\\d]{6}|[a-f\\d]{8})$/i', $input)) {\n    throw new \\RuntimeException('Invalid hex color: ' . $input);\n}\n$color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse($input);","typeGuard":"function isHexColorString(mixed $input): bool\n{\n    return is_string($input)\n        && preg_match('/^#?([a-f\\d]{3}|[a-f\\d]{4}|[a-f\\d]{6}|[a-f\\d]{8})$/i', $input) === 1;\n}","tryCatchPattern":"try {\n    $color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse($hex);\n} catch (\\Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    // surface a form validation error instead of a 500\n    $errors['color'] = 'Please use a hex value like #ff0000';\n}","preventionTips":["Validate hex input with your own regex before parsing","Trim whitespace; only 3, 4, 6 or 8 hex digits (optional '#') are accepted","Note supports() is loose: any '#...' string reaches decode(), so the exception can fire for near-misses"],"tags":["hex-color","rgb","color-parsing","validation","php"],"backgroundTag":"invalid-hex-color","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}