PHP Luminova: CLI Output Color Formatting
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 Key | Color Name | ANSI Code |
|---|---|---|
black | Black | 0;30 |
darkGray | Dark Gray | 1;30 |
red | Red | 0;31 |
lightRed | Light Red | 1;31 |
darkRed | Dark Red | 2;31 |
green | Green | 0;32 |
lightGreen | Light Green | 1;32 |
darkGreen | Dark Green | 2;32 |
yellow | Yellow | 0;33 |
lightYellow | Light Yellow | 1;33 |
darkYellow | Dark Yellow | 2;33 |
blue | Blue | 0;34 |
lightBlue | Light Blue | 1;34 |
magenta | Magenta | 0;35 |
lightMagenta | Light Magenta | 1;35 |
cyan | Cyan | 0;36 |
lightCyan | Light Cyan | 1;36 |
lightGray | Light Gray | 0;37 |
white | White | 1;37 |
brightBlack | Bright Black | 90 |
brightRed | Bright Red | 91 |
brightGreen | Bright Green | 92 |
brightYellow | Bright Yellow | 93 |
brightBlue | Bright Blue | 94 |
brightMagenta | Bright Magenta | 95 |
brightCyan | Bright Cyan | 96 |
brightWhite | Bright White | 97 |
Background Colors
List of text background colors with their ANSI codes.
| Color Key | Color Name | ANSI Code |
|---|---|---|
black | Black | 40 |
red | Red | 41 |
green | Green | 42 |
yellow | Yellow | 43 |
blue | Blue | 44 |
magenta | Magenta | 45 |
cyan | Cyan | 46 |
lightGray | Light Gray | 47 |
darkGray | Dark Gray | 100 |
lightRed | Light Red | 101 |
lightGreen | Light Green | 102 |
lightYellow | Light Yellow | 103 |
lightBlue | Light Blue | 104 |
lightMagenta | Light Magenta | 105 |
lightCyan | Light Cyan | 106 |
white | White | 107 |
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'): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$color | string | The name of the color to retrieve (e.g., 'red', 'green'). |
$type | string | The 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | The text to be styled. |
$foreground | ?string | Optional text foreground color name (e.g., 'red', 'green'). |
$background | ?string | Optional 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | The text to color. |
$fonts | ?int | Optional font style(s) (e.g., Text::FONT_BOLD or Text::FONT_BOLD | Text::FONT_UNDERLINR). |
$foreground | ?string | Optional text foreground color name (e.g, red, white). |
$background | ?string | Optional 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'): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$color | ?string | The color name to validate. |
$type | string | The 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): ?stringReturn:
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(); // redbgColorName
Returns the specified background color name if it exists within the background color mapping.
Color::bgCyan(?string $text = null): ?stringReturn:
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