Reorder a color palette
Reverse a scale or select one ordered range while preserving the color values.
Source
0
1
2
Reverse
0
1
2
Slice
0
1
reverse: light to dark
slice: reindexed from zero
Change sequence, not channel values
reverse() flips an unnamed palette and reindexes it. slice(1, 2) also reindexes the selected range from zero.
Use collection operations for collection order
Both methods return new palettes. The source sequence remains available for another ordering.
<?php
use PhpColor\Color\Palette\ColorPalette;
$palette = ColorPalette::parse([
'#111827', '#475569', '#f8fafc',
]);
echo implode(', ', $palette->reverse()->toHex())."\n";
foreach ($palette->slice(1, 2)->toHex() as $index => $hex) {
printf("%d %s\n", $index, $hex);
}
// #f8fafc, #475569, #111827
// 0 #475569
// 1 #f8fafc
use PhpColor\Color\Palette\ColorPalette;
$palette = ColorPalette::parse([
'#111827', '#475569', '#f8fafc',
]);
echo implode(', ', $palette->reverse()->toHex())."\n";
foreach ($palette->slice(1, 2)->toHex() as $index => $hex) {
printf("%d %s\n", $index, $hex);
}
// #f8fafc, #475569, #111827
// 0 #475569
// 1 #f8fafc
Reordering is limited to unnamed palettes
Named palettes encode semantic keys, so PHPColor refuses to reverse or slice them without an application-defined key policy.
ReverseOrder flips and numeric indexes restart from zero.
SliceThe selected range restarts its numeric indexes from zero.
ValuesColor objects are not transformed.
Named palettesBoth operations reject named input.