返回格式化后的字符串

@param string $text 要着色的文本
@param string|array|int $foregroundColors 前景色  eg: red、red+highlight、Colors::BLACK、[Colors::BLACK, Colors::HIGHLIGHT]
@param string|int $backgroundColors 背景色

@return string
972664
public static function colour($text, $foregroundColors = null, $backgroundColors = null)
    {
        if (self::$noAnsi) {
            return $text;
        }

        $colour = function ($text) use ($foregroundColors, $backgroundColors) {
            $colors = [];
            if ($backgroundColors) {
                $bColor = self::charToCode($backgroundColors);
                $colors[] = "4{$bColor[0]}";
            }

            if ($foregroundColors) {
                list($fColor, $option) = self::charToCode($foregroundColors);
                $option && $colors[] = $option;
                $colors[] = "3{$fColor}";
            }

            $colors && $text = sprintf("\033[%sm", implode(';', $colors)) . $text . "\033[0m";//关闭所有属性;

            return $text;
        };

        $lines = explode("\n", $text);
        foreach ($lines as &$line) {
            $line = $colour($line);
        }
        return implode("\n", $lines);
    }
1299941
public function color($string = null, $color = null, $background = null) {
		$result = '';
		
		if ($color !== null) {
			$result = sprintf(self::COLOR_PATTERN, $this->_fontColors[$color]);
		}
		
		if ($background !== null) {
			$result .= sprintf(self::COLOR_PATTERN, $this->_backgroundColors[$background]);
		}
		
		return $result . $string . sprintf(self::COLOR_PATTERN, 0);
	}
1228064
public static function _text($text, $values = array()) {
		_t($text, static::getDomain(), is_array($values) ? $values : array_slice(func_get_args(), 1));
	}
1149106
public function colors(string $text): string
    {
        $text = \str_replace(['<eol>', '<eol/>', '</eol>', "\r\n", "\n"], '__PHP_EOL__', $text);

        if (!\preg_match_all('/<(\w+)>(.*?)<\/end>/', $text, $matches)) {
            return \str_replace('__PHP_EOL__', \PHP_EOL, $text);
        }

        $end  = "\033[0m";
        $text = \str_replace(['<end>', '</end>'], $end, $text);

        foreach ($matches[1] as $i => $method) {
            $part = \str_replace($end, '', $this->{$method}(''));
            $text = \str_replace("<$method>", $part, $text);
        }

        return \str_replace('__PHP_EOL__', \PHP_EOL, $text);
    }
520482
public function do_text(
        $text,
        $offsetX = 0,
        $offsetY = 0,
        $opacity = 1.0,
        $color = 0x000000,
        $size = 12,
        $font_file = null
    ) {
        $textColor = imagecolorallocatealpha($this->_image, ($color >> 16) & 0xFF, ($color >> 8) & 0xFF,
            $color & 0xFF, abs(1 - $opacity) * 127);
        if ($font_file) {
            imagettftext($this->_image, $size, 0, $offsetX, $offsetY, $textColor, $this->alias->resolve($font_file), $text);
        } else {
            imagestring($this->_image, $size, $offsetX, $offsetY, $text, $textColor);
        }

        return $this;
    }
180394
public function paintString($string, $foregroundColor = null, $backgroundCcolor = null, $option=null)
    {
        $coloredString = "";

        if (isset($this->foregroundColors[$foregroundColor])) {
            $coloredString .= "\033[".$this->foregroundColors[$foregroundColor]."m";
        }

        if (isset($this->backgroundColors[$backgroundCcolor])) {
            $coloredString .= "\033[".$this->backgroundColors[$backgroundCcolor]."m";
        }

        if (isset($this->options[$option])) {
            $coloredString .= "\033[".$this->options[$option]."m";
        }

        return $coloredString.$string."\033[0m";
    }
873793
public function getColoredString($string, $foregroundColor = null, $backgroundColor = null) {

        $colored_string = "";

        // Check if given foreground color found
        if (isset($this->foregroundColors[$foregroundColor])) {
            $colored_string .= "\033[" . $this->foregroundColors[$foregroundColor] . "m";
        }
        // Check if given background color found
        if (isset($this->backgroundColors[$backgroundColor])) {
            $colored_string .= "\033[" . $this->backgroundColors[$backgroundColor] . "m";
        }

        // Add string and end coloring
        $colored_string .=  $string . "\033[0m";

        return $colored_string;
    }
930001
public function colorize($text, $options = 0, $width = 0)
    {
        $map = [
            self::AT_BOLD => "\033[1m",
            self::AT_ITALICS => "\033[3m",
            self::AT_UNDERLINE => "\033[4m",
            self::AT_BLINK => "\033[5m",
            self::AT_INVERSE => "\033[7m",

            self::BC_BLACK => "\033[40m",
            self::BC_RED => "\033[41m",
            self::BC_GREEN => "\033[42m",
            self::BC_YELLOW => "\033[43m",
            self::BC_BLUE => "\033[44m",
            self::BC_MAGENTA => "\033[45m",
            self::BC_CYAN => "\033[46m",
            self::BC_WHITE => "\033[47m",

            self::FC_BLACK => "\033[30m",
            self::FC_RED => "\033[31m",
            self::FC_GREEN => "\033[32m",
            self::FC_YELLOW => "\033[33m",
            self::FC_BLUE => "\033[34m",
            self::FC_MAGENTA => "\033[35m",
            self::FC_CYAN => "\033[36m",
            self::FC_WHITE => "\033[37m",
        ];

        if (!$this->isSupportColor()) {
            return $width ? str_pad($text, $width) : $text;
        }

        $c = '';
        for ($i = 0; $i < 32; $i++) {
            if ((1 << $i) & $options) {
                $c .= $map[(1 << $i) & $options];
            }
        }

        return $c . $text . "\033[0m" . str_repeat(' ', max($width - strlen($text), 0));
    }
1047292
public function styleText($text)
    {
        if ($this->_outputAs == static::RAW) {
            return $text;
        }
        if ($this->_outputAs == static::PLAIN) {
            $tags = implode('|', array_keys(static::$_styles));

            return preg_replace('#</?(?:' . $tags . ')>#', '', $text);
        }

        return preg_replace_callback(
            '/<(?P<tag>[a-z0-9-_]+)>(?P<text>.*?)<\/(\1)>/ims',
            [$this, '_replaceTags'],
            $text
        );
    }
216625
function highlight($text) {
		$out='';
		$pre=FALSE;
		$text=trim($text);
		if (!preg_match('/^<\?php/',$text)) {
			$text='<?php '.$text;
			$pre=TRUE;
		}
		foreach (token_get_all($text) as $token)
			if ($pre)
				$pre=FALSE;
			else
				$out.='<span'.
					(is_array($token)?
						(' class="'.
							substr(strtolower(token_name($token[0])),2).'">'.
							$this->encode($token[1]).''):
						('>'.$this->encode($token))).
					'</span>';
		return $out?('<code>'.$out.'</code>'):$text;
	}
712179
public function paint($string)
    {
        $coloredString = "";

        // set foreground color
        if (array_key_exists($this->foreground, $this->foregroundColors)) {
            $coloredString .= "\033[" . $this->foregroundColors[ $this->foreground ] . "m";
        }

        // set background color
        if (array_key_exists($this->background, $this->backgroundColors)) {
            $coloredString .= "\033[" . $this->backgroundColors[ $this->background ] . "m";
        }

        // combine with string and end the painting
        $coloredString .= $string . "\033[0m";

        return $coloredString;
    }
69289
public static function color($text, $color)
    {
        if (isset(ShellTool::$colors[$color])) {
            return ShellTool::$colors[$color]['start'] . $text . ShellTool::$colors[$color]['close'];
        } else {
            return $text;
        }
    }
622221
public function jsonFrontMatter($text)
    {
        list($text, $frontmatter) = $this->extractFrontMatter($text, "{{{\n", "}}}\n");

        if (!empty($frontmatter)) {
            $frontmatter = json_decode($frontmatter, true);

            if (is_null($frontmatter)) {
                throw new Exception("Failed parsing JSON frontmatter.");
            }
        }

        return [
            "text" => $text,
            "frontmatter" => $frontmatter
        ];
    }
45014
public function write($text, $params=array())
    {
        $color = (isset($params['color'])) ?  $params['color'] : array(0, 0, 0);
        $color = imagecolorallocate($this->edited, $color[0], $color[1], $color[2]);
        $size = (isset($params['size']))? $params['size'] : 5;
        $x = (isset($params['x']))? $params['x'] : 2;
        $y = (isset($params['y']))? $params['y'] : 2;


        imagestring($this->edited, $size, $x, $y, $text, $color);
    }
335907
public function getText(String $text=null, String $colour='green', String $backgroundColor='black')
	{
		$colours = [
			'green' => Attributes::COLOR_GREEN,
			'red' => Attributes::COLOR_RED,
			'yellow' => Attributes::COLOR_YELLOW,
			'blue' => Attributes::COLOR_BLUE,
			'cyan' => Attributes::COLOR_CYAN,
			'magenta' => Attributes::COLOR_MAGENTA,
			'white' => Attributes::COLOR_WHITE,
		];

		$backgroundColors = [
			'green' => Attributes::BG_GREEN,
			'red' => Attributes::BG_RED,
			'yellow' => Attributes::BG_YELLOW,
			'blue' => Attributes::BG_BLUE,
			'cyan' => Attributes::BG_CYAN,
			'magenta' => Attributes::BG_MAGENTA,
			'black' => Attributes::BG_BLACK,
		];

		if (isset($colours[$colour])) {
			$text = $colours[$colour] . $text;
		}

		$text = $backgroundColors[$backgroundColor] . $text; 

		return $text . "\n";
	}
206212
public function setBackgroundColor($backgroundColor)
    {
        if ( is_null($this->backColor) ) {
            $this->backColor = new Color;
        }

        $this->backColor->setFromArray($backgroundColor);

        return $this;
    }
1304558
private String getInputFontColor(String backgroundColor) {

        if ((backgroundColor != null) && (backgroundColor.indexOf("#") == 0)) {
            // remove the "#" from the color string
            backgroundColor = backgroundColor.substring(1);
            int colorValue = 50001;
            try {
                // calculate int value of color
                colorValue = Integer.parseInt(backgroundColor, 16);
            } catch (NumberFormatException nf) {
                // this should never happen
            }
            if (colorValue < 50000) {
                // for dark colors set font color to white
                return "#FFFFFF";
            } else {
                // for other colors use black
                return "#000000";
            }
        }
        return "#000000";
    }