home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Color.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  27.1 KB  |  729 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2002 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Jason Lotito <jason@lehighweb.com>                          |
  16. // |          Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  17. // |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Color.php,v 1.9 2002/12/28 23:08:27 sebastian Exp $
  21.  
  22. /**
  23. *    Color
  24. *    Handles and manages color mixing.
  25. *    
  26. *    TODO: Eventually, I would like to expand upon this class to include other
  27. *    color types, and make it handle colors in a cleaner manner, however, as of
  28. *    right now, I would rather get it out rather than remain vaporware.  At least
  29. *    this way, other people can take a look at it, and make suggestions.
  30. *    Besides, someone else might get more use out of it than I.
  31. *
  32. *    The class is really simple to use, and pretty much does its job fairly well.
  33. *    A sample of what can be done with this class is found here: 
  34. *        http://www.newbienetwork.net/class.colour.php
  35. *    As you can well see, it is very good at what it does, and is rather quick.
  36. *    If someone has ideas or thoughts on this, please let me know.  I would like
  37. *    expand it to handling image colors, as well as converting to CMYK, and even
  38. *    the dreaded Pantone(TM) colors!  If someone knows of converting algo's or
  39. *    know of anything that might be of interest to me, let me know =).
  40. *    about it to.
  41. *
  42. *    Also, one more thing - Yes, I know, I will be try to get off the setColors()
  43. *    2 color limitation, but since this script started out as a simple function
  44. *    that could _mix_ to colors together, it just kept going like that.
  45. *
  46. *   If you notice, the version is only 0.1.  This is because I don't know of
  47. *   anyone really using it, and so it hasn't been tested completely.  The more
  48. *   input I get back on it, the closer it goes to a 1.0 release.
  49. *
  50. *   @version    0.2
  51. *   @author     Jason Lotito <jason@lehighweb.com>
  52. */
  53. class Image_Color
  54. {
  55.     /**
  56.     *   first color that the class handles for ranges and mixes.
  57.     *   @access private
  58.     *   @see    setColors
  59.     */
  60.     var $color1 = array();
  61.     
  62.     /**
  63.     *   second color that the class handles for ranges and mixes.
  64.     *    @access private
  65.     */
  66.     var $color2 = array();
  67.     
  68.     /**
  69.     *   Boolean value for determining whether colors outputted should be websafe or not.  Defaults to false.
  70.     *    @access private
  71.     *   @see    setWebSafe
  72.     */
  73.     var $_websafeb = false;
  74.     
  75.     /**
  76.     *   the websafe ranges we use to determine where to set each value.  The key
  77.     *   is the websafe part, and the value is the non-websafe value.
  78.     *    @access private
  79.     */
  80.     var $websafe = array(   '00' => '00', 
  81.                             '33' => '51', 
  82.                             '66' => '102', 
  83.                             '99' => '153', 
  84.                             'cc' => '204', 
  85.                             'ff' => '255');
  86.     
  87.     /**
  88.     *    mixColors
  89.     *    Given two colors, this will return a mix of the two together.
  90.     *
  91.     *    @access    public
  92.     *    @param    string(optional)    $col1    The first color you want to mix
  93.     *    @param    string(optional)    $col2    The second color you want to mix
  94.     *    @result    string                        The mixed color.
  95.     *    @author    Jason Lotito <jason@lehighweb.com>
  96.     */
  97.     function mixColors ( $col1=false, $col2=false )
  98.     {
  99.         if ( $col1 )
  100.         {
  101.             $this->_setColors($col1, $col2);
  102.         }
  103.         
  104.         // RED
  105.         $color3[0] = ( $this->color1[0] + $this->color2[0] ) / 2;
  106.         // GREEN
  107.         $color3[1] = ( $this->color1[1] + $this->color2[1] ) / 2;
  108.         // BLUE
  109.         $color3[2] = ( $this->color1[2] + $this->color2[2] ) / 2;
  110.         
  111.         if ( $this->_websafeb )
  112.         {
  113.             array_walk( $color3, '_makeWebSafe' );
  114.         }
  115.         
  116.         return $this->_returnColor( $color3 );
  117.     }
  118.     
  119.     /**
  120.     *    setWebSafe
  121.     *    Sets whether we should output websafe colors or not.
  122.     *
  123.     *    @access    public
  124.     *    @param    bool=true    If set to true (def.), websafe is on, otherwise not.
  125.     *    @author    Jason Lotito <jason@lehighweb.com>
  126.     */
  127.     function setWebSafe( $bool=true )
  128.     {
  129.         $this->_websafeb = $bool;
  130.     }
  131.     
  132.     /**
  133.     *    setColors
  134.     *    This class primarily works with two colors, and using this function, you
  135.     *    can easily set both colors.
  136.     *
  137.     *    @access    public
  138.     *    @param    string    The first color in hex format
  139.     *    @param    string    The second color in hex format
  140.     *    @author    Jason Lotito <jason@lehighweb.com>
  141.     */
  142.     function setColors( $col1, $col2 )
  143.     {
  144.         $this->_setColors($col1, $col2);
  145.     }
  146.     
  147.     /**
  148.     *   getRange
  149.     *   Given a degree, you can get the range of colors between one color and
  150.     *   another color.
  151.     *
  152.     *   @access     public
  153.     *   @param      string  $degrees How much each 'step' between the colors we should take.
  154.     *   @return     array   Returns an array of all the colors, one element for each color.
  155.     *   @author     Jason Lotito <jason@lehighweb.com>
  156.     */
  157.     function getRange ( $degrees=2 )
  158.     {
  159.         if ( $degrees == 0 )
  160.             $degrees = 1;
  161.         
  162.         /**
  163.         The degrees give us how much we should advance each color at each phase
  164.         of the loop.  This way, the advance is equal throughout all the colors.
  165.         
  166.         TODO: Allow for degrees for individual parts of the colors.
  167.         */
  168.         
  169.         // RED
  170.         $red_steps   = ( $this->color2[0] - $this->color1[0] ) / $degrees;
  171.         // GREEN
  172.         $green_steps = ( $this->color2[1] - $this->color1[1] ) / $degrees;
  173.         // BLUE
  174.         $blue_steps  = ( $this->color2[2] - $this->color1[2] ) / $degrees;
  175.         
  176.         $allcolors = array();
  177.         $x = 0;
  178.         
  179.         /**
  180.         The loop stops once any color has gone beyond the end color.
  181.         */
  182.         
  183.         // Loop through all the degrees between the colors
  184.         for ( $x = 0; $x < $degrees; $x++ )
  185.         {
  186.             $col[0] = $red_steps * $x;
  187.             $col[1] = $green_steps * $x;
  188.             $col[2] = $blue_steps * $x;
  189.             
  190.             // Loop through each R, G, and B
  191.             for ( $i = 0; $i < 3; $i++ )
  192.             {
  193.                 $partcolor = $this->color1[$i] + $col[$i];
  194.                 // If the color is less than 256
  195.                 if (  $partcolor < 256 )
  196.                 {
  197.                     // Makes sure the colors is not less than 0
  198.                     if ( $partcolor > -1 )
  199.                     {
  200.                         $newcolor[$i] = $partcolor;
  201.                     } else {
  202.                         $newcolor[$i] = 0;
  203.                     }
  204.                 // Color was greater than 255
  205.                 } else {
  206.                     $newcolor[$i] = 255;
  207.                 }
  208.             }
  209.             
  210.             if ( $this->_websafeb )
  211.             {
  212.                 array_walk( $newcolor, '_makeWebSafe' );
  213.             }
  214.             
  215.             $allcolors[] = $this->_returnColor($newcolor);
  216.         }
  217.         
  218.           
  219.         return $allcolors;
  220.     }
  221.     
  222.     /**
  223.     *    changeLightness
  224.     *    Changes the lightness of the color.
  225.     *
  226.     *    The argument it takes determines the direction to go.  If you give it a
  227.     *    negative number, it will make the color darker, however, if you give it
  228.     *    a positive number, it gets lighter.
  229.     *    @access public
  230.     *    @param    int    degree    The degree of the change you wish to take place.
  231.     *    @author    Jason Lotito <jason@lehighweb.com>
  232.     */
  233.     function changeLightness ( $degree=10 )
  234.     {
  235.         $color2 =& $this->color2;
  236.         $color1 =& $this->color1;
  237.         
  238.         for ( $x = 0; $x < 3; $x++ )
  239.         {
  240.             if ( ( $color1[$x] + $degree ) < 256 )
  241.             {
  242.                 if ( ( $color1[$x] + $degree ) > -1 )
  243.                 {
  244.                     $color1[$x] += $degree;
  245.                 } else {
  246.                     $color1[$x] = 0;
  247.                 }
  248.             } else {
  249.                 $color1[$x] = 255;
  250.             }
  251.         }
  252.         
  253.         for ( $x = 0; $x < 3; $x++ )
  254.         {
  255.             if ( ( $color2[$x] + $degree ) < 256 )
  256.             {
  257.                 if ( ( $color2[$x] + $degree ) > -1 )
  258.                 {
  259.                     $color2[$x] += $degree;
  260.                 } else {
  261.                     $color2[$x] = 0;
  262.                 }
  263.             } else {
  264.                 $color2[$x] = 255;
  265.             }
  266.         }
  267.     }
  268.     
  269.     /**
  270.     *    getTextColor
  271.     *    Given a color, will return whether you should use a dark font or a light font.
  272.     *
  273.     *    You can change the dark and the light color, however by default, they are
  274.     *    set to be 'white' (ffffff) and 'black' (000000), which are standard text
  275.     *    colors.  This is determined by the G(reen) value of RGB.
  276.     *
  277.     *   @access     public
  278.     *   @param      string  $color  The color to analyze
  279.     *   @param      string  $light(optional) The light color value to return if we should have light text
  280.     *   @param      string  $dark(optional) The dark color value to return if we should have dark text
  281.     *   @author     Jason Lotito <jason@lehighweb.com>
  282.     */
  283.     function getTextColor ( $color, $light='FFFFFF', $dark='000000' )
  284.     {
  285.         $color = Image_Color::_splitColor($color);
  286.         if ( $color[1] > hexdec('66') )
  287.         {
  288.             return $dark;
  289.         } else {
  290.             return $light;
  291.         }
  292.     }
  293.     
  294.     /**
  295.     *    _setColors
  296.     *    Internal method to correctly set the colors.
  297.     *
  298.     *    @access    private
  299.     *    @param    string    Color 1
  300.     *    @param    string     Color 2
  301.     *    @author    Jason Lotito <jason@lehighweb.com>
  302.     */
  303.     function _setColors ( $col1, $col2 )
  304.     {
  305.         $this->color1 = Image_Color::_splitColor($col1);
  306.         $this->color2 = Image_Color::_splitColor($col2);
  307.     }
  308.     
  309.     /**
  310.     *    _splitColor
  311.     *    Given a color, it will properly split it up into a 3 element dec. array.
  312.     *    
  313.     *    @access    private
  314.     *    @param    string    The color.
  315.     *    @return    array    3 element array containing the RGB information.
  316.     *    @author    Jason Lotito <jason@lehighweb.com>
  317.     */
  318.     function _splitColor ( $color )
  319.     {
  320.         $color = str_replace('#', '', $color);
  321.         $c[] = hexdec( substr( $color, 0, 2 ) );
  322.         $c[] = hexdec( substr( $color, 2, 2 ) );
  323.         $c[] = hexdec( substr( $color, 4, 2 ) );
  324.         return $c;
  325.     }
  326.     
  327.     /**
  328.     *    _returnColor
  329.     *    Given an array of 3 elements containing RGB decimal information, it will
  330.     *    return an HTML compatible HEX color.
  331.     *
  332.     *    @access    private
  333.     *    @author    Jason Lotito <jason@lehighweb.com>
  334.     */
  335.     function _returnColor ( $color )
  336.     {
  337.         return sprintf('%02X%02X%02X',$color[0],$color[1],$color[2]);
  338.     }
  339.     
  340.     /**
  341.     *    rgb2hex
  342.     *    Given an array of 3 elements containing RGB information, it will return
  343.     *    a string of the HEX color.
  344.     *
  345.     *    @access    public
  346.     *    @param    array    3 element array.
  347.     *    @return string
  348.     *    @author    Jason Lotito <jason@lehighweb.com>
  349.     */
  350.     function rgb2hex ( $color )
  351.     {
  352.         return Image_Color::_returnColor( $color );
  353.     }
  354.     
  355.     /**
  356.     *    hex2rgb
  357.     *    Given a hex color, returns a 4 element array, with keys 0-2 containing
  358.     *    the RGB values appropriately and with key 3 containing the original
  359.     *    color.
  360.     *
  361.     *    @access    public
  362.     *    @param    string    The HEX string of the color.
  363.     *    @return    array    4 element array.
  364.     *    @author    Jason Lotito <jason@lehighweb.com>
  365.     */
  366.     function hex2rgb ( $hex )
  367.     {
  368.         $return = Image_Color::_splitColor( $hex );
  369.         $return['hex'] = $hex;
  370.         return $return;
  371.     }
  372.     
  373.     /**
  374.     *   hsv2rgb
  375.     *   Converts a HSV (Hue, Saturation, Brightness) value to RGB.
  376.     *
  377.     *   @access public
  378.     *   @param  integer $h  Hue
  379.     *   @param  integer $s  Saturation
  380.     *   @param  integer $v  Brightness
  381.     *   @return string      The RGB value.
  382.     *   @author    Jason Lotito <jason@lehighweb.com>
  383.     */
  384.     function hsv2rgb ( $h, $s, $v )
  385.     {
  386.         return Image_Color::hex2rgb(Image_Color::hsv2hex($h, $s, $v));
  387.     }
  388.     
  389.     /**
  390.     *   hsv2hex
  391.     *   Converts a HSV (Hue, Saturation, Brightness) value to Hexidecimal.
  392.     *
  393.     *   Originally written by @author.  Integrated into Class by Jason Lotito.
  394.     *
  395.     *   @access public
  396.     *   @param  integer $h  Hue
  397.     *   @param  integer $s  Saturation
  398.     *   @param  integer $v  Brightness
  399.     *   @return string      The hex value.
  400.     *    @author    @author    Jurgen Schwietering <jurgen@schwietering.com>
  401.     */
  402.     function hsv2hex ( $h, $s, $v )
  403.     {
  404.         $s /= 256.0;
  405.         $v /= 256.0;
  406.         if ( $s == 0.0 )
  407.         {
  408.             $r = $g = $b = $v;
  409.             return '';
  410.         } else {
  411.             $h = $h/256.0*6.0;
  412.             $i = floor($h);
  413.             $f = $h - $i;
  414.             
  415.             $v *= 256.0;
  416.             $p = (integer)($v * (1.0 - $s));
  417.             $q = (integer)($v * (1.0 - $s * $f));
  418.             $t = (integer)($v * (1.0 - $s * (1.0 - $f)));
  419.             switch( $i )
  420.             {
  421.                 case 0:  
  422.                     $r = $v; 
  423.                     $g = $t; 
  424.                     $b = $p; 
  425.                     break;
  426.                 case 1:  
  427.                     $r = $q; 
  428.                     $g = $v; 
  429.                     $b = $p; 
  430.                     break;
  431.                 case 2:  
  432.                     $r = $p; 
  433.                     $g = $v; 
  434.                     $b = $t; 
  435.                     break;
  436.                 case 3:  
  437.                     $r = $p; 
  438.                     $g = $q; 
  439.                     $b = $v; 
  440.                     break;
  441.                 case 4:  
  442.                     $r = $t; 
  443.                     $g = $p; 
  444.                     $b = $v; 
  445.                     break;
  446.                 default: 
  447.                     $r = $v; 
  448.                     $g = $p; 
  449.                     $b = $q; 
  450.                     break;
  451.             }
  452.         }
  453.  
  454.         $newcolor = array($r, $g, $b);
  455.  
  456.         return $this->_returnColor($newcolor);
  457.     }
  458.  
  459.     /**
  460.     * Allocates a color in the given image.
  461.     * 
  462.     * Userdefined color specifications get translated into 
  463.     * an array of rgb values.
  464.     *
  465.     * @param    resource    Image handle
  466.     * @param    mixed       (Userdefined) color specification
  467.     * @return   resource    Image color handle
  468.     * @see      color2RGB()
  469.     * @access   public
  470.     */  
  471.     function allocateColor(&$img, $color) {
  472.         $color = $this->color2RGB($color);
  473.  
  474.         return ImageColorAllocate($img, $color[0], $color[1], $color[2]);
  475.     }                 
  476.  
  477.     /**
  478.     * Returns the RGB interger values of a named color, [0,0,0] if unknown.
  479.     *
  480.     * The static variable $colornames is used to resolve
  481.     * the color names. Modify it if neccessary. 
  482.     *
  483.     * @param    string  Case insensitive color name.
  484.     * @return   array   [int red, int green, int blue], 
  485.     *                   returns black [0, 0, 0] if the color is unknown.
  486.     * @access   public
  487.     * @static
  488.     * @author   Sebastian Bergmann <sb@sebastian-bergmann.de>
  489.     */
  490.     function namedColor2RGB($color) {
  491.         static $colornames;
  492.  
  493.         if (!isset($colornames)) {
  494.             $colornames = array(
  495.               'aliceblue'             => array(240, 248, 255),
  496.               'antiquewhite'          => array(250, 235, 215),
  497.               'aqua'                  => array(  0, 255, 255),
  498.               'aquamarine'            => array(127, 255, 212),
  499.               'azure'                 => array(240, 255, 255),
  500.               'beige'                 => array(245, 245, 220),
  501.               'bisque'                => array(255, 228, 196),
  502.               'black'                 => array(  0,   0,   0),
  503.               'blanchedalmond'        => array(255, 235, 205),
  504.               'blue'                  => array(  0,   0, 255),
  505.               'blueviolet'            => array(138,  43, 226),
  506.               'brown'                 => array(165,  42,  42),
  507.               'burlywood'             => array(222, 184, 135),
  508.               'cadetblue'             => array( 95, 158, 160),
  509.               'chartreuse'            => array(127, 255,   0),
  510.               'chocolate'             => array(210, 105,  30),
  511.               'coral'                 => array(255, 127,  80),
  512.               'cornflowerblue'        => array(100, 149, 237),
  513.               'cornsilk'              => array(255, 248, 220),
  514.               'crimson'               => array(220,  20,  60),
  515.               'cyan'                  => array(  0, 255, 255),
  516.               'darkblue'              => array(  0,   0,  13), 
  517.               'darkcyan'              => array(  0, 139, 139),
  518.               'darkgoldenrod'         => array(184, 134,  11),
  519.               'darkgray'              => array(169, 169, 169),
  520.               'darkgreen'             => array(  0, 100,   0),
  521.               'darkkhaki'             => array(189, 183, 107),
  522.               'darkmagenta'           => array(139,   0, 139),
  523.               'darkolivegreen'        => array( 85, 107,  47),
  524.               'darkorange'            => array(255, 140,   0),
  525.               'darkorchid'            => array(153,  50, 204),
  526.               'darkred'               => array(139,   0,   0),
  527.               'darksalmon'            => array(233, 150, 122),
  528.               'darkseagreen'          => array(143, 188, 143),
  529.               'darkslateblue'         => array( 72,  61, 139),
  530.               'darkslategray'         => array( 47,  79,  79),
  531.               'darkturquoise'         => array(  0, 206, 209),
  532.               'darkviolet'            => array(148,   0, 211),
  533.               'deeppink'              => array(255,  20, 147),
  534.               'deepskyblue'           => array(  0, 191, 255),
  535.               'dimgray'               => array(105, 105, 105),
  536.               'dodgerblue'            => array( 30, 144, 255),
  537.               'firebrick'             => array(178,  34,  34),
  538.               'floralwhite'           => array(255, 250, 240),
  539.               'forestgreen'           => array( 34, 139,  34),
  540.               'fuchsia'               => array(255,   0, 255),
  541.               'gainsboro'             => array(220, 220, 220),
  542.               'ghostwhite'            => array(248, 248, 255),
  543.               'gold'                  => array(255, 215,   0),
  544.               'goldenrod'             => array(218, 165,  32),
  545.               'gray'                  => array(128, 128, 128),
  546.               'green'                 => array(  0, 128,   0),
  547.               'greenyellow'           => array(173, 255,  47),
  548.               'honeydew'              => array(240, 255, 240),
  549.               'hotpink'               => array(255, 105, 180),
  550.               'indianred'             => array(205,  92,  92),
  551.               'indigo'                => array(75,    0, 130),
  552.               'ivory'                 => array(255, 255, 240),
  553.               'khaki'                 => array(240, 230, 140),
  554.               'lavender'              => array(230, 230, 250),
  555.               'lavenderblush'         => array(255, 240, 245),
  556.               'lawngreen'             => array(124, 252,   0),
  557.               'lemonchiffon'          => array(255, 250, 205),
  558.               'lightblue'             => array(173, 216, 230),
  559.               'lightcoral'            => array(240, 128, 128),
  560.               'lightcyan'             => array(224, 255, 255),
  561.               'lightgoldenrodyellow'  => array(250, 250, 210),
  562.               'lightgreen'            => array(144, 238, 144),
  563.               'lightgrey'             => array(211, 211, 211),
  564.               'lightpink'             => array(255, 182, 193),
  565.               'lightsalmon'           => array(255, 160, 122),
  566.               'lightseagreen'         => array( 32, 178, 170),
  567.               'lightskyblue'          => array(135, 206, 250),
  568.               'lightslategray'        => array(119, 136, 153),
  569.               'lightsteelblue'        => array(176, 196, 222),
  570.               'lightyellow'           => array(255, 255, 224),
  571.               'lime'                  => array(  0, 255,   0),
  572.               'limegreen'             => array( 50, 205,  50),
  573.               'linen'                 => array(250, 240, 230),
  574.               'magenta'               => array(255,   0, 255),
  575.               'maroon'                => array(128,   0,   0),
  576.               'mediumaquamarine'      => array(102, 205, 170),
  577.               'mediumblue'            => array(  0,   0, 205),
  578.               'mediumorchid'          => array(186,  85, 211),
  579.               'mediumpurple'          => array(147, 112, 219),
  580.               'mediumseagreen'        => array( 60, 179, 113),
  581.               'mediumslateblue'       => array(123, 104, 238),
  582.               'mediumspringgreen'     => array(  0, 250, 154),
  583.               'mediumturquoise'       => array(72, 209, 204),
  584.               'mediumvioletred'       => array(199,  21, 133),
  585.               'midnightblue'          => array( 25,  25, 112),
  586.               'mintcream'             => array(245, 255, 250),
  587.               'mistyrose'             => array(255, 228, 225),
  588.               'moccasin'              => array(255, 228, 181),
  589.               'navajowhite'           => array(255, 222, 173),
  590.               'navy'                  => array(  0,   0, 128),
  591.               'oldlace'               => array(253, 245, 230),
  592.               'olive'                 => array(128, 128,   0),
  593.               'olivedrab'             => array(107, 142,  35),
  594.               'orange'                => array(255, 165,   0),
  595.               'orangered'             => array(255,  69,   0),
  596.               'orchid'                => array(218, 112, 214),
  597.               'palegoldenrod'         => array(238, 232, 170),
  598.               'palegreen'             => array(152, 251, 152),
  599.               'paleturquoise'         => array(175, 238, 238),
  600.               'palevioletred'         => array(219, 112, 147),
  601.               'papayawhip'            => array(255, 239, 213),
  602.               'peachpuff'             => array(255, 218, 185),
  603.               'peru'                  => array(205, 133,  63),
  604.               'pink'                  => array(255, 192, 203),
  605.               'plum'                  => array(221, 160, 221),
  606.               'powderblue'            => array(176, 224, 230),
  607.               'purple'                => array(128,   0, 128),
  608.               'red'                   => array(255,   0,   0),
  609.               'rosybrown'             => array(188, 143, 143),
  610.               'royalblue'             => array( 65, 105, 225),
  611.               'saddlebrown'           => array(139,  69,  19),
  612.               'salmon'                => array(250, 128, 114),
  613.               'sandybrown'            => array(244, 164,  96),
  614.               'seagreen'              => array( 46, 139,  87),
  615.               'seashell'              => array(255, 245, 238),
  616.               'sienna'                => array(160,  82,  45),
  617.               'silver'                => array(192, 192, 192),
  618.               'skyblue'               => array(135, 206, 235),
  619.               'slateblue'             => array(106,  90, 205),
  620.               'slategray'             => array(112, 128, 144),
  621.               'snow'                  => array(255, 250, 250),
  622.               'springgreen'           => array(  0, 255, 127),
  623.               'steelblue'             => array( 70, 130, 180),
  624.               'tan'                   => array(210, 180, 140),
  625.               'teal'                  => array(  0, 128, 128),
  626.               'thistle'               => array(216, 191, 216),
  627.               'tomato'                => array(255,  99,  71),
  628.               'turquoise'             => array( 64, 224, 208),
  629.               'violet'                => array(238, 130, 238),
  630.               'wheat'                 => array(245, 222, 179),
  631.               'white'                 => array(255, 255, 255),
  632.               'whitesmoke'            => array(245, 245, 245),
  633.               'yellow'                => array(255, 255,   0),
  634.               'yellowgreen'           => array(154, 205,  50)
  635.             );
  636.         }
  637.         
  638.         $color = strtolower($color);
  639.  
  640.         if (!isset($colornames[$color])) {
  641.             return array(0, 0, 0);
  642.         }
  643.  
  644.         return $colornames[$color];
  645.     }
  646.  
  647.     /**
  648.     * Returns the RGB integer values of a color specified by a "percentage string" like "%50,%20,%100". 
  649.     *
  650.     * @param    string
  651.     * @return   array   [int red, int green, int blue]
  652.     * @access   public
  653.     */
  654.     function percentageColor2RGB($color) {
  655.         // split the string %50,%20,%100 by ,
  656.         $color = explode(",", $color);        
  657.                 
  658.         foreach ($color as $k => $v) {
  659.             // remove the trailing percentage sign %
  660.             $v = (int)substr($v, 1);
  661.  
  662.             // range checks
  663.             if ($v >= 100) {
  664.                 $color[$k] = 255;
  665.             } else if ($v <= 0) {
  666.                 $color[$k] = 0;
  667.             } else {
  668.                 $color[$k] = (int)(2.55 * $v);
  669.             }
  670.         } 
  671.  
  672.         return $color;
  673.     }
  674. }
  675.  
  676. // For Array Walk
  677. // {{{
  678.     /**
  679.     *    _makeWebSafe
  680.     *    Function for array_walk() to easily change colors from whatever to 
  681.     *    the closests websafe representation.
  682.     *
  683.     *    @access   private
  684.     *    @param    int        One element of the decimal RGB value of a color.
  685.     *    @return   int        The websafe equivalent of the color setting.
  686.     *    @author   Jason Lotito <jason@lehighweb.com>
  687.     */
  688.     function _makeWebSafe ( &$color )
  689.     {
  690.         if ( $color == 0 )
  691.         {
  692.             return $color;
  693.         } else {
  694.             if ( ($color % 51) == 0 )
  695.             {
  696.                 return $color;
  697.             } else {
  698.                 if ( $color < 26 ) {
  699.                     $color = 00;
  700.                     return $color;
  701.                 } else if ( $color < 77 && $color > 25 ) {
  702.                     $color = 51;
  703.                     return $color;
  704.                 } else if ( $color > 76 && $color < 127 ) {
  705.                     $color = 102;
  706.                     return $color;
  707.                 } else if ( $color > 126 && $color < 178 ) {
  708.                     $color = 153;
  709.                     return $color;
  710.                 } else if ( $color > 177 && $color < 229 ) {
  711.                     $color = 204;
  712.                     return $color;
  713.                 } else {
  714.                     $color = 255;
  715.                     return $color;
  716.                 }
  717.             }
  718.         }
  719.     }
  720. // }}}
  721.  
  722. /*
  723. * Local variables:
  724. * tab-width: 4
  725. * c-basic-offset: 4
  726. * End:
  727. */
  728. ?>
  729.