{"record":{"id":"6ac35bc33d3ef4d6","repo":"phalcon/cphalcon","slug":"the-color-color-is-not-a-valid-hex-color","errorCode":null,"errorMessage":"The color '{color}' is not a valid hex color","messagePattern":"The color '(.+?)' is not a valid hex color","errorType":"exception","errorClass":"Phalcon\\Image\\Exceptions\\InvalidColor","httpStatus":null,"severity":"error","filePath":"phalcon/Image/Adapter/AbstractAdapter.zep","lineNumber":634,"sourceCode":"     * @throws InvalidColor\n     */\n    private function parseColor(string color) -> array\n    {\n        var channels;\n\n        if (\n            strlen(color) > 1 &&\n            substr(color, 0, 1) === \"#\"\n        ) {\n            let color = substr(color, 1);\n        }\n\n        if (strlen(color) === 3) {\n            let color = (string) preg_replace(\"/./\", \"$0$0\", color);\n        }\n\n        if (1 !== preg_match(\"/^[0-9a-fA-F]{6}$/\", color)) {\n            throw new InvalidColor(color);\n        }\n\n        /** @var image_color_channels $channels */\n        let channels = array_map(\n            \"hexdec\",\n            str_split(color, 2)\n        );\n\n        return channels;\n    }\n}\n","sourceCodeStart":616,"sourceCodeEnd":646,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Image/Adapter/AbstractAdapter.zep#L616-L646","documentation":"The adapter's hex-color parser (used for background(), rotate() and text colors) accepts only 'rgb', '#rgb', 'rrggbb' and '#rrggbb': it strips a leading '#', doubles 3-character forms, then requires the result to match ^[0-9a-fA-F]{6}$ - otherwise InvalidColor is thrown. CSS color names, rgba(), 8-digit hex and other formats are not supported.","triggerScenarios":"->background('white'); ->rotate(45, 'zz12ab'); a color string from user input containing whitespace, quotes or a wrong length such as '#12ab'.","commonSituations":"Passing CSS color names instead of hex; free-text color fields saved from forms or databases without validation; alpha-suffixed colors like '#aabbccdd' which fail the 6-hex rule.","solutions":["Validate before calling: preg_match('/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $color)","Map CSS names to hex yourself ('white' => 'ffffff') or store canonical '#rrggbb' values","Trim and normalize user input before passing it to the adapter"],"exampleFix":"// before\n$image->background('white'); // names not supported -> throws\n\n// after\n$map = ['white' => 'ffffff', 'black' => '000000'];\n$color = $map[$requested] ?? $requested;\nif (!preg_match('/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $color)) {\n    throw new InvalidArgumentException('Invalid color: ' . $requested);\n}\n$image->background($color);","handlingStrategy":"validation","validationCode":"$color = trim((string) $color);\nif (!preg_match('/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $color)) {\n    throw new \\InvalidArgumentException(sprintf('\"%s\" is not a valid hex color', $color));\n}\n$image->background(ltrim($color, '#'));","typeGuard":"function isHexColor(mixed $color): bool\n{\n    return is_string($color) && 1 === preg_match('/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', trim($color));\n}","tryCatchPattern":"try { $image->background($color); } catch (\\Phalcon\\Image\\Exceptions\\InvalidColor $e) { $image->background('ffffff'); // safe default\n}","preventionTips":["Accept only hex input in forms/APIs; validate with the regex above","Map CSS color names to hex in your own lookup table","Store colors as canonical '#rrggbb' in configuration and databases"],"tags":["php","phalcon","image","color","validation"],"backgroundTag":"invalid-hex-color","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}