Convert HEX to RGB

Parse a hexadecimal color and emit CSS RGB or read its integer channels.

Parse the hexadecimal value, convert it to sRGB, then choose the representation your application needs:

use PhpColor\Color\Color;

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

echo $rgb->toCss(); // rgb(59 130 246)

$channels = $rgb->getChannels();
$red = (int) round($channels['r'] * 255);
$green = (int) round($channels['g'] * 255);
$blue = (int) round($channels['b'] * 255);
use PhpColor\Color\Color; $rgb = Color::parse('#3b82f6')->to('srgb'); echo $rgb->toCss(); // rgb(59 130 246) $channels = $rgb->getChannels(); $red = (int) round($channels['r'] * 255); $green = (int) round($channels['g'] * 255); $blue = (int) round($channels['b'] * 255);

The channel values returned by PHPColor are normalized floats from 0 to 1. Multiply by 255 and round only when an external API requires byte values.

For a known six-digit value with no validation, alpha, or shorthand support, native PHP is enough:

[$red, $green, $blue] = sscanf('#3b82f6', '#%02x%02x%02x');
[$red, $green, $blue] = sscanf('#3b82f6', '#%02x%02x%02x');

Use PHPColor when the input may also be #rgb, #rgba, #rrggbbaa, a named color, or another CSS syntax. See Supported concrete color input for the complete input contract.