{"record":{"id":"d88aa3795e4b27ae","repo":"Intervention/image","slug":"invalid-hsl-color-syntax-input","errorCode":null,"errorMessage":"Invalid hsl() color syntax \"{input}\"","messagePattern":"Invalid hsl\\(\\) color syntax \"(.+?)\"","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Hsl/Decoders/StringColorDecoder.php","lineNumber":53,"sourceCode":"            return false;\n        }\n\n        if (!str_starts_with(strtolower($input), 'hsl')) {\n            return false;\n        }\n\n        return true;\n    }\n\n    /**\n     * Decode hsl color strings.\n     *\n     * @throws InvalidArgumentException\n     */\n    public function decode(mixed $input): ColorInterface\n    {\n        if (preg_match(self::PATTERN, $input, $matches) !== 1) {\n            throw new InvalidArgumentException('Invalid hsl() color syntax \"' . $input . '\"');\n        }\n\n        $values = array_map(fn(string $value): int => match (strpos($value, '%')) {\n            false => intval(trim($value)),\n            default => intval(trim(str_replace('%', '', $value))),\n        }, [$matches['h'], $matches['s'], $matches['l']]);\n\n        // alpha value\n        if (array_key_exists('a', $matches)) {\n            $values[] = match (strpos($matches['a'], '%')) {\n                false => floatval(trim($matches['a'])),\n                default => floatval(trim(str_replace('%', '', $matches['a']))) / 100,\n            };\n        }\n\n        return new Color(...$values);\n    }\n}","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Hsl/Decoders/StringColorDecoder.php#L35-L71","documentation":"The HSL string decoder accepts 'hsl(...)' or 'hsla(...)' with a numeric hue (optionally suffixed 'deg'), saturation and luminance as plain numbers or percentages, and an optional alpha ('/ 0.5', comma or space separated, in 0-1 decimal or N% form). Strings that start with 'hsl' but violate this grammar throw the 'Invalid hsl() color syntax' InvalidArgumentException. Component values are matched as non-negative decimals, so negatives or letters fail here before any range validation happens.","triggerScenarios":"Hsl\\Color::parse('hsl(-10, 50%, 50%)') (negative hue); 'hsl(0 100% 50' (missing ')'); 'hsl(0, 100%, 50% / 1.5)' (alpha grammar only allows 0-1 decimals or up-to-3-digit percents); 'hsl(360deg,100%,50%,)' (trailing comma); smart quotes/Unicode minus from copied text.","commonSituations":"Free-text color fields in CMS/forms; CSS strings pasted from design tools with alternate spacing or Unicode punctuation; dynamically assembled strings with missing delimiters.","solutions":["Use canonical syntax: 'hsl(0, 100%, 50%)' or 'hsla(0, 100%, 50% / 0.5)'","Validate with a regex or try/catch around parse before trusting user input","Normalize input: trim whitespace, replace Unicode minus/quotes with ASCII equivalents","Build programmatically when values are dynamic: Hsl\\Color::create(0, 100, 50, 0.5)"],"exampleFix":"// before\n$color = Hsl\\Color::parse('hsla(-20, 100%, 50% / 50%)');\n\n// after\n$color = Hsl\\Color::parse('hsla(340, 100%, 50% / 0.5)');","handlingStrategy":"validation","validationCode":"if (!preg_match('/^hsla? ?\\( ?[0-9.]+(deg)?(, ?| )[0-9.]+%?(, ?| )[0-9.]+%?( ?\\/ ?|[, ] ?)?((0?\\.[0-9]+)|1\\.0|\\.[0-9]+|[0-9]{1,3}%|1|0)? ?\\)$/i', trim($input))) {\n    // reject before Hsl\\Color::parse()\n}","typeGuard":"function isHslString(mixed $input): bool\n{\n    return is_string($input)\n        && str_starts_with(strtolower(trim($input)), 'hsl')\n        && str_contains($input, '(')\n        && str_ends_with(trim($input), ')');\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException as ImageArg;\n\ntry {\n    $color = Hsl\\Color::parse($input);\n} catch (ImageArg $e) {\n    $color = Hsl\\Color::create(0, 0, 0); // or re-prompt the user\n}","preventionTips":["Validate hsl() strings at the input boundary with the same grammar","Use canonical comma-separated syntax with % on s and l","Express alpha as a 0-1 decimal or percent, never >1","Prefer Hsl\\Color::create() when values are programmatic"],"tags":["php","intervention-image","color","hsl","string-parsing","validation"],"backgroundTag":"color-string-parse-error","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}