Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Last active December 14, 2015 17:08
Show Gist options
  • Select an option

  • Save jehoshua02/5119903 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/5119903 to your computer and use it in GitHub Desktop.
Try it: `git clone https://gist.github.com/5119903.git Color; php Color/usage.php | less -R`
<?php
class Color
{
protected static $fgColors = array(
'black' => "\033[0;30m",
'darkGray' => "\033[1;30m",
'red' => "\033[0;31m",
'lightRed' => "\033[1;31m",
'green' => "\033[0;32m",
'lightGreen' => "\033[1;32m",
'brown' => "\033[0;33m",
'yellow' => "\033[1;33m",
'blue' => "\033[0;34m",
'lightBlue' => "\033[1;34m",
'purple' => "\033[0;35m",
'lightPurple' => "\033[1;35m",
'cyan' => "\033[0;36m",
'lightCyan' => "\033[1;36m",
'lightGray' => "\033[0;37m",
'white' => "\033[1;37m",
);
protected static $bgColors = array(
'black' => "\033[40m",
'red' => "\033[41m",
'green' => "\033[42m",
'yellow' => "\033[43m",
'blue' => "\033[44m",
'magenta' => "\033[45m",
'cyan' => "\033[46m",
'lightGray' => "\033[47m",
);
protected static $end = "\033[0m";
public static function demo($phrase = 'The quick brown fox jumps over the lazy dog')
{
$fgColors = self::getFgColors();
$bgColors = self::getBgColors();
$maxlen = function ($array) {
return max(array_map('strlen', $array));
};
$labelSeparator = '/';
$pad = $maxlen($fgColors) + $maxlen($bgColors) + strlen($labelSeparator);
$format = "%-{$pad}s %s\n\n";
foreach ($fgColors as $fgColor) {
echo sprintf($format, $fgColor, self::$fgColor($phrase));
foreach ($bgColors as $bgColor) {
echo sprintf($format,
$fgColor . $labelSeparator . $bgColor,
self::colorize($phrase, $fgColor, $bgColor)
);
}
}
}
public static function __callStatic($fg, array $arguments)
{
return self::colorize(array_shift($arguments), $fg, array_shift($arguments));
}
public static function colorize($text, $fgName = null, $bgName = null)
{
$fg = ($fgName === null || !array_key_exists($fgName, self::$fgColors)) ? null : self::$fgColors[$fgName];
$bg = ($bgName === null || !array_key_exists($bgName, self::$bgColors)) ? null : self::$bgColors[$bgName];
return $fg . $bg . $text . self::$end;
}
public static function getFgColors()
{
return array_keys(self::$fgColors);
}
public static function getBgColors()
{
return array_keys(self::$bgColors);
}
}
<?php
require_once __DIR__ . '/Color.php';
// demo to display all fg/bg colors
Color::demo();
// static function for fg color by name
echo Color::green('Good to go.'), "\n";
echo Color::red('Oops! Something went wrong.'), "\n";
echo Color::blue('Just some info.'), "\n";
// get list of all fg colors
var_dump(Color::getFgColors());
// but what about bg colors?
echo Color::colorize('Success! Hoo-hah!', 'white', 'green'), "\n";
echo Color::colorize('Something went really wrong.', 'white', 'red'), "\n";
echo Color::colorize('Some important info', 'white', 'blue'), "\n";
// get list of all bg colors
var_dump(Color::getBgColors());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment