Core\Cli\Color::index
Core\Cli\Color::index(uint $index): Core\Cli\ColorCore\Cli\Color::index( uint $index, ): Core\Cli\Color
Gives a colour from the terminal's palette by its number.
A terminal that shows colour has a palette of 256 entries, numbered 0 to 255. Core\Cli\Color::index returns the colour at one of those numbers. Entries 0 to 15 are the sixteen basic colours, which also have names on this class, such as Core\Cli\Color::RED. Entries 16 to 231 are mixed colours, and entries 232 to 255 are greys from nearly black to nearly white. A number above 255 throws a RuntimeError.
You pass the colour to Core\Cli\Style::of, and the style to Core\Cli\Text::styled. A terminal with only sixteen colours shows the nearest one it has, and output that goes to a file or to another program is written without any colour at all.
The examples below colour two lines by number, show that entry 1 and Core\Cli\Color::RED are the same colour, and give every state of a build report its own colour.
Parameters
$indexuintThe palette entry,0to255.
Return value
Returns Core\Cli\Color. The colour of that entry. A terminal with a smaller palette shows the nearest of its sixteen colours.
Errors
RuntimeError$indexis above255, which no palette has an entry for.
Examples
A colour by its number
<?nvs// Shows how to pick a colour from the terminal's 256-entry palette by its// number. This output goes to a file, and a file shows no colour, so only the// words are printed here.
// Entry 208 is an orange. `Core\Cli\Style::of` makes a style out of it.Core\Cli\Color $orange = Core\Cli\Color::index(208);Core\Cli\Style $warning = Core\Cli\Style::of({ color: $orange });echo Core\Cli\Text::styled('disk is 91% full', $warning), "\n";
// A style can also colour the background. Entry 17 is a dark blue.Core\Cli\Style $banner = Core\Cli\Style::of({ background: Core\Cli\Color::index(17) });echo Core\Cli\Text::styled(' release 1.0 ', $banner), "\n";disk is 91% full release 1.0The first sixteen have names
<?nvs// Shows what the numbers of the palette mean. Entries 0 to 15 are the sixteen// colours that also have names on `Core\Cli\Color`. Entries 232 to 255 are// greys.
// Entry 1 and `Core\Cli\Color::RED` are the same colour, written two ways.Core\Cli\Style $byNumber = Core\Cli\Style::of({ color: Core\Cli\Color::index(1) });Core\Cli\Style $byName = Core\Cli\Style::of({ color: Core\Cli\Color::RED });echo Core\Cli\Text::styled('red by number', $byNumber), "\n";echo Core\Cli\Text::styled('red by name', $byName), "\n";
// The greys run from 232, which is nearly black, to 255, which is nearly// white. Each line prints the word and the number of its entry.array<uint> $greys = [232, 240, 248, 255];foreach ($greys as uint $entry) { Core\Cli\Style $step = Core\Cli\Style::of({ color: Core\Cli\Color::index($entry) }); echo Core\Cli\Text::styled('grey', $step), ' ', $entry, "\n";}red by numberred by namegrey 232grey 240grey 248grey 255A colour for every state
<?nvs// Shows the everyday use: a build report that gives every state its own// colour. The palette has room for more states than the sixteen colours with// names.
class Report{ // Each state picks one palette entry: 40 is a green, 214 an amber, 196 a // red, and 244 a grey for every other state. public static function line(string $state, string $text): void { uint $entry = 244; if ($state == 'ok') { $entry = 40; } if ($state == 'slow') { $entry = 214; } if ($state == 'failed') { $entry = 196; }
Core\Cli\Style $style = Core\Cli\Style::of({ color: Core\Cli\Color::index($entry) }); echo Core\Cli\Text::styled($text, $style), "\n"; }}
Report::line('ok', 'build 2.1s');Report::line('slow', 'tests 41.7s');Report::line('failed', 'deploy failed');Report::line('skipped', 'publish skipped');build 2.1stests 41.7sdeploy failedpublish skipped