Luminova Framework

PHP Luminova: Command Text Outputs Styling Using the Color Utils

Last updated: 2024-10-29 13:09:34

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 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 Value:

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').

<?php
echo Color::get('red', 'fgc')  

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

<?php
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 foreground(string $name): string

Parameters:

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

Return Value:

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 background(string $name): string

Parameters:

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

Return Value:

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 style(string $text, ?string $foreground, ?string $background = null): string

Parameters:

ParameterTypeDescription
$textstringThe text to be styled.
$foregroundstring|nullOptional foreground color name (e.g., 'red', 'green').
$backgroundstring|nullOptional background color name (e.g., 'blue', 'yellow').

Return Value:

string - Return the styled text with ANSI color codes applied, 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 apply(
    string $text, 
    ?int $font, 
    ?string $foreground = null, 
    ?string $background = null
): string

Parameters:

ParameterTypeDescription
$textstringThe text to color.
$fontint|nullOptional text font style formatting (e.g., Text::FONT_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.

Example Usage:

Apply bold formatting with a red foreground and black background.

<?php
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 has(?string $color, string $type = 'fg'): bool

Parameters:

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

Return Value:

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


Dynamic Static Magic Methods

With methods like fg{ColorName}(), bg{ColorName}() bgc{ColorName}() and bgc{ColorName}(), you can replace {ColorName} with a specific color name, such as red, lightYellow etc... This approach is helpful for dynamically handling colors in code.

fgColorName

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

public static fg{ColorName}(): ?string

Return Value:

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();  // Output: "Text color: red"

fgcColorName

Returns the ANSI code for the specified foreground color if it is defined.

public static fgc{ColorName}(): ?string

Return Value:

string|null - Returns the ANSI code for the foreground color if it exists, otherwise null.

Example:

To apply ANSI codes for foreground colors, you can use methods like fgcGreen() for green text or fgcWhite() for bright white text.

echo "\e[" . Color::fgcGreen() . "mThis text is green\e[0m";  // Applies green color to text

bgColorName

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

public static bg{ColorName}(): ?string

Return Value:

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();  // Output: "Background color: cyan"

bgcColorName

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

public static bgc{ColorName}(): ?string

Return Value:

string|null - Returns the ANSI code for the background color if it exists, otherwise null.

Example:

To apply ANSI codes for background colors, you can use methods like bgcRed() for red background text.

echo "\e[" . Color::bgcRed() . "mThis text has a red background\e[0m";