Sample a color scale

Read any normalized position without adding every possible sample to the stored palette.

Stops 0 0.5 1
Samples 0.2 0.5 0.8
0.0 #111827
0.2 #392a70
0.5 #7c3aed
0.8 #c1b4fc
1.0 #f8fafc

Interpolate only the position you need

Positions 0, 0.5, and 1 return the stored stops. Positions 0.2 and 0.8 are Oklab mixes of their neighboring entries.

Sample the ordered palette by position

at() maps the normalized position across the complete sequence, then mixes only the two surrounding colors.

<?php

use PhpColor\Color\Palette\ColorPalette;

$scale = ColorPalette::parse([
    '#111827', '#7c3aed', '#f8fafc',
]);

foreach ([0.0, 0.2, 0.5, 0.8, 1.0] as $position) {
    printf("%.1f %s\n", $position, $scale->at($position)->toHex());
}

// 0.0 #111827
// 0.2 #392a70
// 0.5 #7c3aed
// 0.8 #c1b4fc
// 1.0 #f8fafc
use PhpColor\Color\Palette\ColorPalette; $scale = ColorPalette::parse([ '#111827', '#7c3aed', '#f8fafc', ]); foreach ([0.0, 0.2, 0.5, 0.8, 1.0] as $position) { printf("%.1f %s\n", $position, $scale->at($position)->toHex()); } // 0.0 #111827 // 0.2 #392a70 // 0.5 #7c3aed // 0.8 #c1b4fc // 1.0 #f8fafc

Sampling requires an unnamed scale

A named palette represents keyed roles rather than a continuous sequence, so at() rejects named input.

RangePositions below 0 clamp to 0; values above 1 clamp to 1.
SpaceInterpolation between neighboring stops uses Oklab.
StopsExact stop positions return the stored color object.
Named palettesConvert role-based data into an ordered scale before sampling.

Choose a next step

Store the stops and derive the positions you need