Read color channels

Convert first, then read channels in a known color space.

Read channels only after the application has chosen the coordinate schema it expects. A channel array without its color-space name is incomplete data.

Convert before reading space-specific channels

getChannels() returns the channels of the object's current color space. Convert first when your code expects a particular channel model:

use PhpColor\Color\Color;

$source = Color::parse('#3b82f6');
$oklch = $source->to('oklch');

$channels = $oklch->getChannels();

echo $oklch::getSpaceName();
echo $channels['l'];
echo $channels['c'];
echo $channels['h'];
use PhpColor\Color\Color; $source = Color::parse('#3b82f6'); $oklch = $source->to('oklch'); $channels = $oklch->getChannels(); echo $oklch::getSpaceName(); echo $channels['l']; echo $channels['c']; echo $channels['h'];

The result is an OklchColor. Its canonical space name is oklch, and its channel array contains l, c, and h. The original $source remains an sRGB object because PHPColor color objects are immutable.

Do not assume that the same channel name has the same scale in every space. For example, Oklch lightness is constrained to 0..1, while Lab and LCH use a 0..100 lightness scale.

Read channels without formatting

Use getChannels() when a downstream API needs numbers rather than CSS:

use PhpColor\Color\Color;

$color = Color::parse('#3b82f6')->to('oklch');

$channels = $color->getChannels();
$alpha = $color->getAlpha();

echo $channels['l'];
echo $channels['c'];
echo $channels['h'];
echo $alpha;
use PhpColor\Color\Color; $color = Color::parse('#3b82f6')->to('oklch'); $channels = $color->getChannels(); $alpha = $color->getAlpha(); echo $channels['l']; echo $channels['c']; echo $channels['h']; echo $alpha;

The channel array does not include alpha. Read alpha separately with getAlpha() or its alias getOpacity().

Channel keys and ranges belong to the current space. Convert before reading them if consuming code expects a fixed schema:

use PhpColor\Color\Color;

$rgb = Color::parse('oklch(0.65 0.18 264)')->toSrgb();
$channels = $rgb->getChannels();

echo $channels['r'];
echo $channels['g'];
echo $channels['b'];
use PhpColor\Color\Color; $rgb = Color::parse('oklch(0.65 0.18 264)')->toSrgb(); $channels = $rgb->getChannels(); echo $channels['r']; echo $channels['g']; echo $channels['b'];

The sRGB channel values are floats. CSS rgb() output scales and rounds them to integer 0..255 values, while color(srgb ...) formatting retains normalized decimal channels.

Treat channel arrays as an exchange schema

Do not infer a space from keys such as l, a, or b: Lab and Oklab use similar names with different scales and meanings. Keep the canonical space name beside the values when passing them to another service or storing them as structured data.

Use CSS serialization when the consumer expects CSS rather than numeric channels. Use a named palette when several application roles need to remain attached to their colors.

Continue with Choose text or channel input, Choose a color space, or ColorInterface.