Luminova Framework

PHP Luminova: CLI Output Color Formatting

Last updated: 2026-11-28 20:57:12

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 Definition

  • Class namespace: Luminova\Command\Utils\Color

ANSI Color Codes

Foreground Colors (Text Colors)

List of text foreground colors with their ANSI codes.

Color KeyColor NameANSI Code
blackBlack0;30
darkGrayDark Gray1;30
redRed0;31
lightRedLight Red1;31
darkRedDark Red2;31
greenGreen0;32
lightGreenLight Green1;32
darkGreenDark Green2;32
yellowYellow0;33
lightYellowLight Yellow1;33
darkYellowDark Yellow2;33
blueBlue0;34
lightBlueLight Blue1;34
magentaMagenta0;35
lightMagentaLight Magenta1;35
cyanCyan0;36
lightCyanLight Cyan1;36
lightGrayLight Gray0;37
whiteWhite1;37
brightBlackBright Black90
brightRedBright Red91
brightGreenBright Green92
brightYellowBright Yellow93
brightBlueBright Blue94
brightMagentaBright Magenta95
brightCyanBright Cyan96
brightWhiteBright White97

Background Colors

List of text background colors with their ANSI codes.

Color KeyColor NameANSI Code
blackBlack40
redRed41
greenGreen42
yellowYellow43
blueBlue44
magentaMagenta45
cyanCyan46
lightGrayLight Gray47
darkGrayDark Gray100
lightRedLight Red101
lightGreenLight Green102
lightYellowLight Yellow103
lightBlueLight Blue104
lightMagentaLight Magenta105
lightCyanLight Cyan106
whiteWhite107

Methods

get

Retrieves the ANSI code for a given color name based on the specified type.

public static function get(string $color, string $type = 'fgc'): string

Parameters:

ParameterTypeDescription
$colorstringThe name of the color to retrieve (e.g., 'red', 'green').
$typestringThe type of color, either 'fgc' for foreground or 'bgc' for background (default is 'fgc').

Return:

string - Return the ANSI code for the requested color, or an empty string if the color is not defined.

Example usage:

Returns the ANSI code for foreground red ('0;31').

echo Color::get('red', 'fgc');

Returns the ANSI code for background blue ('44').

echo Color::get('blue', 'bgc');

foreground

Retrieves the ANSI code for a foreground (text) color.

This method looks up the ANSI code for the specified foreground color from the predefined $foregrounds array.

public static function foreground(string $name): string

Parameters:

ParameterTypeDescription
$namestringThe name of the foreground color (e.g., 'red', 'green').

Return:

string - Return the ANSI code for the specified foreground color, or an empty string if not found.


background

Retrieves the ANSI code for a background color.

This method looks up the ANSI code for the specified background color from the predefined $backgrounds array.

public static function background(string $name): string

Parameters:

ParameterTypeDescription
$namestringThe name of the background color (e.g., 'blue', 'yellow').

Return:

string - Return the ANSI code for the specified background color, or an empty string if not found.


style

Styles the provided text with optional foreground or background colors using ANSI formatting.

This method applies ANSI color codes to the provided text using the specifiedforeground and background colors. If the text already contains ANSI codes, it applies the colors regardless.

public static function style(
    string $text,
    ?string $foreground,
    ?string $background = null
): string

Parameters:

ParameterTypeDescription
$textstringThe text to be styled.
$foreground?stringOptional text foreground color name (e.g., 'red', 'green').
$background?stringOptional background color name (e.g., 'blue', 'yellow').

Return:

string - Return the styled text with ANSI color codes or the original text if no valid colors are given.

Example usage:

Styles the text with red foreground and blue background.

echo Color::style('Hello', 'red', 'blue');

Styles the text with green foreground.

echo Color::style('Hello', 'green');

apply

Applies optional font styles, foreground or optional background colors to the given textThis method will return the text unchanged if it already contains ANSI codes or if no formatting options are provided.

public static function apply(
    string $text,
    ?int $fonts,
    ?string $foreground = null,
    ?string $background = null
): string

Parameters:

ParameterTypeDescription
$textstringThe text to color.
$fonts?intOptional font style(s) (e.g., Text::FONT_BOLD or Text::FONT_BOLD | Text::FONT_UNDERLINR).
$foreground?stringOptional text foreground color name (e.g, red, white).
$background?stringOptional background color name (e.g, cyan, green).

Return:

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

Example Usage:

Apply bold formatting with a red foreground and black background.

use Luminova\Command\Utils\Text;

echo Text::apply('Hello, World!', Text::FONT_BOLD | Text::FONT_ITALIC, 'red', 'black');

has

Validates if the specified color name exists in the given color mapping array.

public static function has(?string $color, string $type = 'fg'): bool

Parameters:

ParameterTypeDescription
$color?stringThe color name to validate.
$typestringThe mapping of color names to ANSI codes (e.g, fg, bg).

Return:

bool - Return true if the color exists, otherwise false.


Dynamic Static Magic Methods

Use fg{ColorName}() or bg{ColorName}() with a color name such as Red or LightYellow. Call the method without arguments to look up a color name, or pass text to apply that color. Use foreground() and background() to look up ANSI codes.

fgColorName

Returns the specified foreground (text) color name if it exists within the foreground color mapping.

Color::fgRed(?string $text = null): ?string

Return:

string|null - Returns the foreground color name if it exists, otherwise null.

Example:

To set the foreground color to red, call the fgRed() method. This returns the color name "red" if it's valid.

echo Color::fgRed();  // red

bgColorName

Returns the specified background color name if it exists within the background color mapping.

Color::bgCyan(?string $text = null): ?string

Return:

string|null - Returns the background color name if it exists, otherwise null.

Example:

To set the background color to cyan, call the bgCyan() method. This returns the color name "cyan" if it’s a valid background color.

echo Color::bgCyan();  // cyan