|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: wujunze |
| 5 | + * Date: 2017/3/17 |
| 6 | + * Time: 下午2:12 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Wujunze; |
| 10 | + |
| 11 | + |
| 12 | +class Colors { |
| 13 | + private $foreground_colors = array(); |
| 14 | + private $background_colors = array(); |
| 15 | + |
| 16 | + public function __construct() { |
| 17 | + // Set up shell colors |
| 18 | + $this->foreground_colors['black'] = '0;30'; |
| 19 | + $this->foreground_colors['dark_gray'] = '1;30'; |
| 20 | + $this->foreground_colors['blue'] = '0;34'; |
| 21 | + $this->foreground_colors['light_blue'] = '1;34'; |
| 22 | + $this->foreground_colors['green'] = '0;32'; |
| 23 | + $this->foreground_colors['light_green'] = '1;32'; |
| 24 | + $this->foreground_colors['cyan'] = '0;36'; |
| 25 | + $this->foreground_colors['light_cyan'] = '1;36'; |
| 26 | + $this->foreground_colors['red'] = '0;31'; |
| 27 | + $this->foreground_colors['light_red'] = '1;31'; |
| 28 | + $this->foreground_colors['purple'] = '0;35'; |
| 29 | + $this->foreground_colors['light_purple'] = '1;35'; |
| 30 | + $this->foreground_colors['brown'] = '0;33'; |
| 31 | + $this->foreground_colors['yellow'] = '1;33'; |
| 32 | + $this->foreground_colors['light_gray'] = '0;37'; |
| 33 | + $this->foreground_colors['white'] = '1;37'; |
| 34 | + |
| 35 | + $this->background_colors['black'] = '40'; |
| 36 | + $this->background_colors['red'] = '41'; |
| 37 | + $this->background_colors['green'] = '42'; |
| 38 | + $this->background_colors['yellow'] = '43'; |
| 39 | + $this->background_colors['blue'] = '44'; |
| 40 | + $this->background_colors['magenta'] = '45'; |
| 41 | + $this->background_colors['cyan'] = '46'; |
| 42 | + $this->background_colors['light_gray'] = '47'; |
| 43 | + } |
| 44 | + |
| 45 | + // Returns colored string |
| 46 | + public function getColoredString($string, $foreground_color = null, $background_color = null) { |
| 47 | + $colored_string = ""; |
| 48 | + |
| 49 | + // Check if given foreground color found |
| 50 | + if (isset($this->foreground_colors[$foreground_color])) { |
| 51 | + $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; |
| 52 | + } |
| 53 | + // Check if given background color found |
| 54 | + if (isset($this->background_colors[$background_color])) { |
| 55 | + $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; |
| 56 | + } |
| 57 | + |
| 58 | + // Add string and end coloring |
| 59 | + $colored_string .= $string . "\033[0m"; |
| 60 | + |
| 61 | + return $colored_string; |
| 62 | + } |
| 63 | + |
| 64 | + // Returns all foreground color names |
| 65 | + public function getForegroundColors() { |
| 66 | + return array_keys($this->foreground_colors); |
| 67 | + } |
| 68 | + |
| 69 | + // Returns all background color names |
| 70 | + public function getBackgroundColors() { |
| 71 | + return array_keys($this->background_colors); |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments