Console_Color
[ class tree: Console_Color ] [ index: Console_Color ] [ all elements ]

Source for file Color.php

Documentation is available at Color.php

  1. <?php
  2.  
  3. // Copyright (c) 2007 Stefan Walk
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy 
  6. // of this software and associated documentation files (the "Software"), to 
  7. // deal in the Software without restriction, including without limitation the 
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
  9. // sell copies of the Software, and to permit persons to whom the Software is 
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in 
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
  21. // IN THE SOFTWARE.
  22.  
  23. $GLOBALS['_CONSOLE_COLOR_CODES'= array (
  24.     'color' => array(
  25.             'black'  => 30,
  26.             'red'    => 31,
  27.             'green'  => 32,
  28.             'brown'  => 33,
  29.             'blue'   => 34,
  30.             'purple' => 35,
  31.             'cyan'   => 36,
  32.             'grey'   => 37,
  33.             'yellow' => 33
  34.     ),
  35.     'style' => array(
  36.             'normal'     => 0,
  37.             'bold'       => 1,
  38.             'light'      => 1,
  39.             'underscore' => 4,
  40.             'underline'  => 4,
  41.             'blink'      => 5,
  42.             'inverse'    => 6,
  43.             'hidden'     => 8,
  44.             'concealed'  => 8
  45.     ),
  46.     'background' => array(
  47.             'black'  => 40,
  48.             'red'    => 41,
  49.             'green'  => 42,
  50.             'brown'  => 43,
  51.             'yellow' => 43,
  52.             'blue'   => 44,
  53.             'purple' => 45,
  54.             'cyan'   => 46,
  55.             'grey'   => 47
  56.     )
  57. );
  58.  
  59.     
  60. /**
  61.  * A simple class to use ANSI Colorcodes.
  62.  *
  63.  * Of all the functions, you probably only want to use convert() and escape().
  64.  * They are easier to use. However, if you want to access colorcodes more
  65.  * directly, look into the other functions.
  66.  *
  67.  * @package Console_Color
  68.  * @category Console
  69.  * @author Stefan Walk <et@php.net>
  70.  * @license http://www.opensource.org/licenses/mit-license.php MIT License
  71.  *
  72.  */
  73. class Console_Color {
  74.  
  75.     /**
  76.      * Returns an ANSI-Controlcode
  77.      * 
  78.      * Takes 1 to 3 Arguments: either 1 to 3 strings containing the name of the
  79.      * FG Color, style and BG color, or one array with the indices color, style
  80.      * or background.
  81.      *
  82.      * @access public
  83.      * @param mixed $color Optional
  84.      *    Either a string with the name of the foreground color, or
  85.      *    an array with the indices 'color', 'style', 'background' and
  86.      *    corresponding names as values.
  87.      * @param string $style Optional name of the style
  88.      * @param string $background Optional name of the background color
  89.      * @return string 
  90.      */
  91.     function color($color=null$style = null$background = null// {{{
  92.         {
  93.         $colors &$GLOBALS['_CONSOLE_COLOR_CODES'];
  94.         if (is_array($color)) {
  95.             $style @$color['style'];
  96.             $background @$color['background'];
  97.             $color @$color['color'];
  98.         }
  99.         if ($color == 'reset'{
  100.             return "\033[0m";
  101.         }
  102.         $code = array();
  103.         if (isset($color)) {
  104.             $code[$colors['color'][$color];
  105.         }
  106.         if (isset($style)) {
  107.             $code[$colors['style'][$style];
  108.         }
  109.         if (isset($background)) {
  110.             $code[$colors['background'][$background];
  111.         }
  112.         if (empty($code)) {
  113.             $code[= 0;
  114.         }
  115.         $code implode(';'$code);
  116.         return "\033[{$code}m";
  117.     // }}}
  118.  
  119.     
  120.     /**
  121.      * Returns a FG color controlcode
  122.      *
  123.      * @access public
  124.      * @param string $name 
  125.      * @return string 
  126.      */
  127.     function fgcolor($name)
  128.     {
  129.         $colors &$GLOBALS['_CONSOLE_COLOR_CODES'];
  130.         return "\033[".$colors['color'][$name].'m';
  131.     }
  132.     
  133.     /**
  134.      * Returns a style controlcode
  135.      *
  136.      * @access public
  137.      * @param string $name 
  138.      * @return string 
  139.      */
  140.     function style($name)
  141.     {
  142.         $colors &$GLOBALS['_CONSOLE_COLOR_CODES'];
  143.         return "\033[".$colors['style'][$name].'m';
  144.     }
  145.     
  146.     /**
  147.      * Returns a BG color controlcode
  148.      *
  149.      * @access public
  150.      * @param string $name 
  151.      * @return string 
  152.      */
  153.     function bgcolor($name)
  154.     {
  155.         $colors &$GLOBALS['_CONSOLE_COLOR_CODES'];
  156.         return "\033[".$colors['background'][$name].'m';
  157.     }
  158.  
  159.     /**
  160.      * Converts colorcodes in the format %y (for yellow) into ansi-control
  161.      * codes. The conversion table is: ('bold' meaning 'light' on some
  162.      * terminals). It's almost the same conversion table irssi uses.
  163.      * <pre>
  164.      *                  text      text            background
  165.      *      ------------------------------------------------
  166.      *      %k %K %0    black     dark grey       black
  167.      *      %r %R %1    red       bold red        red
  168.      *      %g %G %2    green     bold green      green
  169.      *      %y %Y %3    yellow    bold yellow     yellow
  170.      *      %b %B %4    blue      bold blue       blue
  171.      *      %m %M %5    magenta   bold magenta    magenta
  172.      *      %p %P       magenta (think: purple)
  173.      *      %c %C %6    cyan      bold cyan       cyan
  174.      *      %w %W %7    white     bold white      white
  175.      *
  176.      *      %F     Blinking, Flashing
  177.      *      %U     Underline
  178.      *      %8     Reverse
  179.      *      %_,%9  Bold
  180.      *
  181.      *      %n     Resets the color
  182.      *      %%     A single %
  183.      * </pre>
  184.      * First param is the string to convert, second is an optional flag if
  185.      * colors should be used. It defaults to true, if set to false, the
  186.      * colorcodes will just be removed (And %% will be transformed into %)
  187.      *
  188.      * @access public
  189.      * @param string $string 
  190.      * @param bool $colored 
  191.      * @return string 
  192.      */
  193.     function convert($string$colored=true)
  194.     {
  195.         static $conversions = array // static so the array doesn't get built
  196.                                       // everytime
  197.             // %y - yellow, and so on... {{{
  198.                         '%y' => array('color' => 'yellow'),
  199.             '%g' => array('color' => 'green' ),
  200.             '%b' => array('color' => 'blue'  ),
  201.             '%r' => array('color' => 'red'   ),
  202.             '%p' => array('color' => 'purple'),
  203.             '%m' => array('color' => 'purple'),
  204.             '%c' => array('color' => 'cyan'  ),
  205.             '%w' => array('color' => 'grey'  ),
  206.             '%k' => array('color' => 'black' ),
  207.             '%n' => array('color' => 'reset' ),
  208.             '%Y' => array('color' => 'yellow',  'style' => 'light'),
  209.             '%G' => array('color' => 'green',   'style' => 'light'),
  210.             '%B' => array('color' => 'blue',    'style' => 'light'),
  211.             '%R' => array('color' => 'red',     'style' => 'light'),
  212.             '%P' => array('color' => 'purple',  'style' => 'light'),
  213.             '%M' => array('color' => 'purple',  'style' => 'light'),
  214.             '%C' => array('color' => 'cyan',    'style' => 'light'),
  215.             '%W' => array('color' => 'grey',    'style' => 'light'),
  216.             '%K' => array('color' => 'black',   'style' => 'light'),
  217.             '%N' => array('color' => 'reset',   'style' => 'light'),
  218.             '%3' => array('background' => 'yellow'),
  219.             '%2' => array('background' => 'green' ),
  220.             '%4' => array('background' => 'blue'  ),
  221.             '%1' => array('background' => 'red'   ),
  222.             '%5' => array('background' => 'purple'),
  223.             '%6' => array('background' => 'cyan'  ),
  224.             '%7' => array('background' => 'grey'  ),
  225.             '%0' => array('background' => 'black' ),
  226.             // Don't use this, I can't stand flashing text
  227.                         '%F' => array('style' => 'blink'),
  228.             '%U' => array('style' => 'underline'),
  229.             '%8' => array('style' => 'inverse'),
  230.             '%9' => array('style' => 'bold'),
  231.             '%_' => array('style' => 'bold')
  232.             // }}}
  233.                 );
  234.         if ($colored{
  235.             $string str_replace('%%''% '$string);
  236.             foreach($conversions as $key => $value{
  237.                 $string str_replace($keyConsole_Color::color($value),
  238.                           $string);
  239.             }
  240.             $string str_replace('% ''%'$string);
  241.  
  242.         else {
  243.             $string preg_replace('/%((%)|.)/''$2'$string);
  244.         }
  245.         return $string;
  246.     }
  247.  
  248.     /**
  249.      * Escapes % so they don't get interpreted as color codes
  250.      * 
  251.      * @access public
  252.      * @param string string
  253.      * @return string 
  254.      */
  255.     function escape($string{
  256.         return str_replace('%''%%'$string);
  257.     }
  258.  
  259.     /**
  260.      * Strips ANSI color codes from a string
  261.      *
  262.      * @acess public
  263.      * @param string string
  264.      * @return string 
  265.      */
  266.     function strip($string{
  267.         return preg_replace('/\033\[[\d;]+m/','',$string);
  268.     }
  269.  
  270. }
  271. ?>

Documentation generated on Fri, 26 Jan 2007 17:30:07 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.