Luminova Framework

PHP Luminova: Styling Command Text Outputs Using the Color Utils Module

Last updated: 2024-08-30 19:53:36

The Color Util class is a helper class for command line application for formatting and applying colors to your output text for vibrancy, it's useful when you want to display text with a background col

Introducing the Color Utils class, a valuable tool for command-line applications that adds a splash of vibrancy and clarity to your text output. With Color Utils, you can apply formatting and colors to your text, transforming ordinary command-line displays into visually stunning and highly readable experiences.

Whether you're highlighting important information, organizing output for better comprehension, or simply adding a touch of flair to your CLI tool, Color Utils offers the functionalities to meet your needs. Easily customize text and background colors to suit your preferences and enhance the visual appeal of your application.


  • Class namespace: \Luminova\Command\Utils\Color

ANSI Color Codes

Foreground Colors

List of text foreground colors with their ANSI codes.

Color KeyColor NameANSI Code
blackBlack0;30
darkGrayDark Gray1;30
blueBlue0;34
darkBlueDark Blue0;34
lightBlueLight Blue1;34
greenGreen0;32
lightGreenLight Green1;32
cyanCyan0;36
lightCyanLight Cyan1;36
redRed0;31
lightRedLight Red1;31
purplePurple0;35
lightPurpleLight Purple1;35
yellowYellow0;33
lightYellowLight Yellow1;33
lightGrayLight Gray0;37
whiteWhite1;37

Background Colors

List of text background colors with their ANSI codes.

Color KeyColor NameANSI Code
blackBlack40
redRed41
greenGreen42
yellowYellow43
blueBlue44
magentaMagenta45
cyanCyan46
lightGrayLight Gray47

Methods

apply

Applies foreground and optional background colors to the given text.

public static apply(string $text, int|null $format = null, string|null $foreground = null, string|null $background = null): string

Parameters:

ParameterTypeDescription
$textstringThe text to color.
$formatint|nullOptionally apply text formatting (e.g, Text::ANSI_BOLD).
$foregroundstring|nullThe foreground color name.
$backgroundstring|nullOptional background color name.

Return Value:

string - Return the formatted text with ANSI color codes, or the original text if unsupported.


length

Calculates the length of the ANSI formatting codes applied to a given text. This method helps determine the exact length of ANSI sequences in a text, which can be useful when subtracting the ANSI codes from the overall text length.

public static length(int|null $format = null, string|null $foreground = null, string|null $background = null): int

Parameters:

ParameterTypeDescription
$formatint|nullAnd optional text formatting to include (e.g., Text::ANSI_*).
$foregroundstring|nullAn optional foreground color name to include (default: null).
$backgroundstring|nullAn optional background color name to include (default: null).

Return Value:

int - Return the total length of ANSI formatting codes that is included.

Example

Apply bold formatting with a red foreground and black background.

<?php
$text = Text::apply('Hello, World!', Text::ANSI_BOLD, 'red', 'black');
$ansiLength = Text::length(Text::ANSI_BOLD, 'red', 'black');

echo $text; // Outputs: Hello, World! (with bold red text on a black background)
echo "\nANSI Length: " . $ansiLength; // Outputs: ANSI Length: <calculated length>