colored

echo $ac->colored("Yellow on magenta.", array('bold','yellow','onmagenta')) . "\n";

@param string $txt   - text to color
@param array  $codes - the attributes for the text

@access public
@return string surrounded by escape codes
344507
public function colored($txt = '', $codes = array())
    {
        $attr = $this->color($codes);
        if (!empty($this->EACHLINE)) {
            $eachline = $this->EACHLINE;
            $parts = preg_split("/(\Q$eachline\E)/", $txt, 0, PREG_SPLIT_DELIM_CAPTURE);
            if (count($parts) > 0) {
                return implode('', array_map(function ($n) use ($eachline, $attr) {
                    return $n != "$eachline" ? $attr . $n . chr(27) . "[0m" : $n;
                }, $parts));
            }
        } else {
            return $attr . $txt . chr(27) . "[0m";
        }
    }
932423
private function parseTxt($txt)
    {
        $result = [];
        $lines = array_map('trim', mb_split('\r\n|\n|\r', $txt));
        // Parse each line individually
        foreach ($lines as $key => $line) {
            // Limit rule length
            $line = mb_substr($line, 0, self::MAX_LENGTH_RULE);
            // Remove comments
            $line = explode('#', $line, 2)[0];
            // Parse line
            $result[] = $this->parseLine($line);
            unset($lines[$key]);
        }
        return in_array(true, $result, true);
    }
1259130
protected function mbRenderImageByImagick($code, $encoding)
    {
        $backColor = $this->transparent ? new \ImagickPixel('transparent') : new \ImagickPixel(sprintf('#%06x', $this->backColor));
        $foreColor = new \ImagickPixel(sprintf('#%06x', $this->foreColor));

        $image = new \Imagick();
        $image->newImage($this->width, $this->height, $backColor);

        $draw = new \ImagickDraw();
        $draw->setFont($this->mbFontFile);
        $draw->setFontSize(30);
        $fontMetrics = $image->queryFontMetrics($draw, $code);

        $length = mb_strlen($code, $encoding);
        $w = (int)($fontMetrics['textWidth']) + $this->mbOffset * ($length - 1);
        $h = (int)($fontMetrics['textHeight']);
        $scale = min(($this->width - $this->padding * 2) / $w, ($this->height - $this->padding * 2) / $h);
        $x = 8;
        // font size and angle
        $fontSize = (int)(30 * $scale * 0.90);
        $angle = 0;
        // base line
        $yBottom = $this->height - $this->padding * 4;
        $yTop = (int)($h * $scale * 0.95) + $this->padding * 4;
        if ($yTop > $yBottom) {
            $yTop = $yBottom;
        }
        for ($i = 0; $i < $length; ++$i) {
            $letter = mb_substr($code, $i, 1, $encoding);
            $y = mt_rand($yTop, $yBottom);
            if (!$this->fixedAngle) {
                $angle = mt_rand(-15, 15);
            }
            $draw = new \ImagickDraw();
            $draw->setFont($this->mbFontFile);
            $draw->setFontSize($fontSize);
            $draw->setFillColor($foreColor);
            $image->annotateImage($draw, $x, $y, $angle, $letter);
            $fontMetrics = $image->queryFontMetrics($draw, $letter);
            $x += (int)($fontMetrics['textWidth']) + $this->mbOffset;
        }

        $image->setImageFormat('png');
        return $image->getImageBlob();
    }
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);
    }
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
        );
    }
794476
public static function highlight($code, $language)
    {
        if ($language !== 'php') {
            return htmlspecialchars($code, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
        }

        if (strncmp($code, '<?php', 5) === 0) {
            $text = @highlight_string(trim($code), true);
        } else {
            $text = highlight_string("<?php ".trim($code), true);
            $text = str_replace('&lt;?php', '', $text);
            if (($pos = strpos($text, '&nbsp;')) !== false) {
                $text = substr($text, 0, $pos) . substr($text, $pos + 6);
            }
        }
        // remove <code><span style="color: #000000">\n and </span>tags added by php
        $text = substr(trim($text), 36, -16);

        return $text;
    }
603774
public static function colorizeInstantMessages($str)
    {

        preg_match_all( "/\[im\](.*?)\[\/im\]/s", $str, $matches );

        $count = count($matches[1]);
        $str = preg_replace("~\[(im|\/im)\]~", "", $str);

        for($x=0;$x<$count;$x++){
            preg_match_all( "~(.*?: )~", $matches[1][$x], $ims );

            $names = array_unique($ims[1]);
            $num_names = count($names);

            $im=0;
            foreach($names as $name){
                if ($im%2) {$color="red"; } else {$color="blue";}

                $str = str_replace($name, '<b><i><span style="color:'.$color.'">'.$name.'</span></i></b>', $str);
                $im++;
            }

        }

        return $str;
    }
818753
public static function colorize($data, $search = null, $color = '#ffff66') {
		if ($search != null) {
			return str_ireplace($search, '<span style="background-color:'.$color.';">'.$search.'</span>', $data);
		} else {
			return '<span style="background-color:'.$color.';">'.$data.'</span>';
		}
	}
331073
public static function toLower($txt, bool $cleanPhrase = false): string
    {
        $txt = $cleanPhrase ? self::cleanPhrase($txt) : $txt;
        return mb_strtolower($txt, self::ENCODING);
    }
581700
protected function formatText($text) {
        $syntax = [
            '/\<strong\>/m' => "\033[1m",
            '/\<u\>/m' => "\033[4m",
            '/\<dim\>/m' => "\033[2m",
            '/\<fatal\>/m' => "\033[" . $this->colors[self::FATAL] . "m",
            '/\<error\>/m' => "\033[" . $this->colors[self::ERROR] . "m",
            '/\<warning\>/m' => "\033[" . $this->colors[self::WARNING] . "m",
            '/\<notice\>/m' => "\033[" . $this->colors[self::NOTICE] . "m",
            '/\<debug\>/m' => "\033[" . $this->colors[self::DEBUG] . "m",
            '/\<red\>/m' => "\033[31m",
            '/\<yellow\>/m' => "\033[33m",
            '/\<green\>/m' => "\033[32m",
            '/\<grey\>/m' => "\033[37m",
            '/\<\/([a-z]+)\>/m' => "\033[0m",
        ];

        $replacements = '$1';

        if ($this->colorize) {
            $replacements = $syntax;
        }

        $result = preg_replace(
            array_keys($syntax),
            $replacements,
            $text
        );

        if (!is_string($result)) {
            throw new RuntimeException('Unable to format text', ExitApp::RUNTIME_ERROR);
        }

        return $result;
    }
1302201
def write_color(self, text, attr=None):
        '''
        '''
        log(u'write_color("%s", %s)' % (text, attr))
        chunks = self.terminal_escape.split(text)
        log(u'chunks=%s' % repr(chunks))
        bg = self.savebg
        n = 0 # count the characters we actually write, omitting the escapes
        if attr is None:#use attribute from initial console
            attr = self.attr
        try:
            fg = self.trtable[(0x000f&attr)]
            bg = self.trtable[(0x00f0&attr)>>4]
        except TypeError:
            fg = attr
            
        for chunk in chunks:
            m = self.escape_parts.match(chunk)
            if m:
                log(m.group(1))
                attr = ansicolor.get(m.group(1), self.attr)
            n += len(chunk)
            System.Console.ForegroundColor = fg
            System.Console.BackgroundColor = bg
            System.Console.Write(chunk)
        return n
1140486
protected function _prepareTxt($txt, $spaces = true)
    {
        if ($spaces) $txt = preg_replace('/\s+/is', ' ', $txt);
        $txt = str_replace('&euro;', '€', $txt);
        $txt = html_entity_decode($txt, ENT_QUOTES, $this->_encoding);
        return $txt;
    }
1207784
def code2color(color_string):
    """"""

    color_tuple = ast.literal_eval(color_string)
    color_tuple_int = map(lambda x: int(x * 255.0), color_tuple)

    return wx.Colour(*color_tuple_int)
1367664
public static function colorizeLines($text, $fgcolor = null, $bgcolor = null)
    {
        $lines = explode("\n", $text);
        foreach ($lines as &$line) {
            $line = self::colorize($line, $fgcolor, $bgcolor);
        }
        return implode("\n", $lines);
    }
688913
protected function replaceColor($text, $tag, $match, $style): string
    {
        $replace = self::$noColor ? $match : sprintf("\033[%sm%s\033[0m", $style, $match);

        return \str_replace("<$tag>$match</$tag>", $replace, $text);
        // return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
    }
649315
public static String filterColors (String txt)
    {
        if (txt == null) return null;
        return COLOR_PATTERN.matcher(txt).replaceAll("");
    }
189632
public function uncolor($codes = array())
    {
        $nums = array();
        $result = array();
        $patts = array('/^' . chr(27) . '\[/', '/m$/');
        foreach ($codes as $code) {
            $esc = preg_replace($patts, '', $code);
            if (preg_match('/^((?:\d+;)*\d*)$/', $esc, $matches)) {
                if ($matches[0] != '') {
                    $nums = array_merge($nums, explode(';', $matches[0]));
                }
            } else {
                throw new InvalidArgumentException("Bad escape sequence $esc");
            }
        }
        foreach ($nums as $num) {
            $num += 0; // Strip leading zeroes
            if (isset($this->ATTRIBUTES_R[$num])) {
                $result[] = $this->ATTRIBUTES_R[$num];
            } else {
                throw new InvalidArgumentException("No name for escape sequence $num");
            }
        }
        return $result;
    }