Skip to content

Core\Cli\Color::rgb

Gives a colour from a red, a green and a blue value, which is how a brand colour is written.

Each of the three values is a level from 0 to 255, so Core\Cli\Color::rgb reaches about sixteen million colours. Core\Cli\Color::rgb(30, 144, 255) is a strong blue, and Core\Cli\Color::rgb(0, 0, 0) is black. A value above 255 throws a RuntimeError, and the error says which of the three it read.

You pass the colour to Core\Cli\Style::of, and the style to Core\Cli\Text::styled. A terminal that shows every colour writes the colour exactly. A terminal with 256 colours, or with only sixteen, writes the nearest colour it has, and output that goes to a file or to another program is written without any colour at all. That is why a program can use its brand colours everywhere and still be readable.

The examples below write one line in a brand colour, move a colour from red to green as a number grows, and keep a whole brand's colours in one place.

Parameters

  • $reduintThe red channel, 0 to 255.
  • $greenuintThe green channel, 0 to 255.
  • $blueuintThe blue channel, 0 to 255.

Return value

Returns Core\Cli\Color. The colour. A true-colour terminal shows it exactly. Other terminals show the nearest palette entry.

Errors

  • RuntimeErrorA channel is above 255.

Examples

A colour from three numbers

<?nvs
// Shows how to make a colour out of a red, a green and a blue value. This
// output goes to a file, and a file shows no colour, so only the words are
// printed here.
// 30 red, 144 green and 255 blue is a strong blue.
Core\Cli\Color $blue = Core\Cli\Color::rgb(30, 144, 255);
echo Core\Cli\Text::styled('a link to the report', Core\Cli\Style::of({ color: $blue })), "\n";
// The same colour behind white text makes a label.
Core\Cli\Style $label = Core\Cli\Style::of({
color: Core\Cli\Color::rgb(255, 255, 255),
background: $blue,
});
echo Core\Cli\Text::styled(' new ', $label), "\n";
Output
a link to the report
new

A colour that follows a number

<?nvs
// Shows a colour worked out from a number. A test run where everything fails
// is red, one where everything passes is green, and the runs in between mix
// the two colours.
class Bar
{
// Every step takes 51 off the red and adds 51 to the green, so step 0 is
// red and step 5 is green. The blue stays at 60 for all of them.
public static function color(uint $step): Core\Cli\Color
{
return Core\Cli\Color::rgb(255 - $step * 51, $step * 51, 60);
}
}
array<uint> $steps = [0, 2, 4, 5];
foreach ($steps as uint $step) {
Core\Cli\Style $style = Core\Cli\Style::of({ color: Bar::color($step) });
echo Core\Cli\Text::styled('tests passing', $style), ' ', $step * 20, "%\n";
}
Output
tests passing 0%
tests passing 40%
tests passing 80%
tests passing 100%

The colours of one brand

<?nvs
// Shows the everyday use: the colours of a brand are written once, and the
// rest of the program asks for them by name. A designer changing a colour
// changes one line.
class Brand
{
public static function ink(): Core\Cli\Color
{
return Core\Cli\Color::rgb(24, 24, 27);
}
public static function accent(): Core\Cli\Color
{
return Core\Cli\Color::rgb(217, 70, 39);
}
public static function quiet(): Core\Cli\Color
{
return Core\Cli\Color::rgb(113, 113, 122);
}
}
Core\Cli\Style $heading = Core\Cli\Style::of({ color: Brand::accent(), bold: true });
Core\Cli\Style $body = Core\Cli\Style::of({ color: Brand::ink() });
Core\Cli\Style $note = Core\Cli\Style::of({ color: Brand::quiet() });
echo Core\Cli\Text::styled('Deploy', $heading), "\n";
echo Core\Cli\Text::styled('3 services updated', $body), "\n";
echo Core\Cli\Text::styled('took 12.4s', $note), "\n";
Output
Deploy
3 services updated
took 12.4s