Convert HEX to HSL

Format a hexadecimal sRGB color as CSS hsl() or read its HSL channels.

Hex and HSL are both representations of sRGB. Parse the hex value and request HSL output:

use PhpColor\Color\Color;

$rgb = Color::parse('#3b82f6')->to('srgb');

echo $rgb->toCss('hsl');
// hsl(217.219251 91.219512% 59.803922%)
use PhpColor\Color\Color; $rgb = Color::parse('#3b82f6')->to('srgb'); echo $rgb->toCss('hsl'); // hsl(217.219251 91.219512% 59.803922%)

Read the coordinates when you need separate values:

$hsl = $rgb->toHsl();

$hue = $hsl['h'];
$saturation = $hsl['s'];
$lightness = $hsl['l'];
$hsl = $rgb->toHsl(); $hue = $hsl['h']; $saturation = $hsl['s']; $lightness = $hsl['l'];

Saturation and lightness are normalized from 0 to 1. HSL is useful for interoperating with existing CSS and data. Prefer OKLCH for perceptual lightness and chroma adjustments.