Skip to content

Instantly share code, notes, and snippets.

@bdelespierre
Created November 20, 2013 17:19
Show Gist options
  • Select an option

  • Save bdelespierre/7567134 to your computer and use it in GitHub Desktop.

Select an option

Save bdelespierre/7567134 to your computer and use it in GitHub Desktop.
<?php
namespace CommandLine;
class Colors {
public static $_backgroundColors = array(
'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'light_gray' => '47',
);
public static $_foregroundColors = array(
'black' => '0;30',
'dark_gray' => '1;30',
'blue' => '0;34',
'light_blue' => '1;34',
'green' => '0;32',
'light_green' => '1;32',
'cyan' => '0;36',
'light_cyan' => '1;36',
'red' => '0;31',
'light_red' => '1;31',
'purple' => '0;35',
'light_purple' => '1;35',
'brown' => '0;33',
'yellow' => '1;33',
'light_gray' => '0;37',
'white' => '1;37',
);
public static function colorize ($string, $foreground = null, $background = null) {
$colored_string = "";
// Check if given foreground color exists
if ($foreground && isset(static::$_foregroundColors[$foreground])) {
$colored_string .= "\033[" . static::$_foregroundColors[$foreground] . "m";
}
// Check if given background color exists
if ($background && isset(static::$_backgroundColors[$background])) {
$colored_string .= "\033[" . static::$_backgroundColors[$background] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
public static function getForegroundColors () {
return array_keys(static::$_foregroundColors);
}
public static function getBackgroundColors () {
return array_keys(static::$_backgroundColors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment