Datei: /php/graphics.php, Stand: 03.03.2001, 19:00

<?php 
/**
* Widget stuff: color translation, ID generation, ...

* Several widget functions to deal with images.
*
* @author   Ulf Wendel <ulf.wendel@phpdoc.de>
* @version  $Id: $
*/
class graphics {

    
/**
    * Mapping from named colors to RGB values.
    * 
    * @var  array
    * @see  color2RGB()
    */ 
    
var $colornames = array(    
                             
"white"    => array(255255255),
                             
"black"    => array(000),
                             
"red"      => array(25500),
                             
"green"    => array(02550),
                             
"blue"     => array(00255)
                        );
                        
    
/**
    * Translates a userdefined color specification into an array of RGB integer values.
    * 
    * Several formats can be handled. HTML like hexadecimal colors like #f0ff00, 
    * names colors, arrays of RGB integer values and strings with percentage values
    * like %100,%50,%20. If the format is unknown black gets returned [0, 0, 0].
    * 
    * @var      mixed   Color in various formats: #f0f0f0, %20,%100,%0, 
    *                   named - black, white..., [int 0 - 255, int 0 - 255, int 0 - 255]
    * @return   array   RGB color [int red, int green, int blue]
    * @access   public
    * @see      $colornames, HTMLColor2RGB(), PercentageColor2RGB(), NamedColor2RGB()
    */                        
    
function color2RGB($color) {
    
        if (
is_array($color)) {
            
            
// looks good...
            
if (== count($color)) {
            
                
// check the range
                
foreach ($color as $k => $v) {
                    if (
$v 0)
                        
$color[$k] = 0;
                    else if (
$v 255
                        
$color[$k] = 255;
                    else 
                        
$color[$k] = (int)$v;
               }
                
               return 
$color;        
            }
                

            
// unknown format - return black
            
return array(00);
        }

        
// #f0f0f0            
        
if ("#" == $color{0})
            return 
$this->HTMLColor2RGB($color);

        
// %50,%100,%50
        
if ("%" == $color{0})
            return 
$this->PercentageColor2RGB($color);

        
// might be a color name            
        
return $this->NamedColor2RGB($color);
    } 
// end func color2RGB     
    
    /**
    * Allocates a color in the given image.
    * 
    * Userdefined color specifications get translated into 
    * an array of rgb values.
    *
    * @param    resource    Image handle
    * @param    mixed       (Userdefined) color specification
    * @return   resource    Image color handle
    * @see      color2RGB()
    * @access   public
    */  
    
function allocateColor(&$img$color) {
        
        
$color $this->color2RGB($color);
        
        return 
ImageColorAllocate($img$color[0], $color[1], $color[2]);
    } 
// end func allocateColor                  
    
    /**
    * Returns the RGB integer values of an HTML like hexadecimal color like #00ff00.
    *
    * @param    string  HTML like hexadecimal color like #00ff00
    * @return   array   [int red, int green, int blue],
    *                   returns black [0, 0, 0] for invalid strings.
    * @access   public
    */
    
function HTMLColor2RGB($color) {
        if (
strlen($color) != 7)
            return array(
000);

        return array(
                        
hexdec(substr($color12)),
                        
hexdec(substr($color32)),
                        
hexdec(substr($color52))
                    );
    } 
// end func HTMLColor2RGB    
    
    /**
    * Returns the RGB interger values of a named color, [0,0,0] if unknown.
    *
    * The class variable $colornames is used to resolve
    * the color names. Modify it if neccessary. 
    *
    * @param    string  Case insensitive color name.
    * @return   array   [int red, int green, int blue], 
    *                   returns black [0, 0, 0] if the color is unknown.
    * @access   public
    * @see      $colornames
    */
    
function NamedColor2RGB($color) {
        
$color strtolower($color);
        
        if (!isset(
$this->colornames[$color]))
            return array(
000);
          
        return 
$this->colornames[$color];
    } 
// end func NamedColor2RGB 
    
    /**
    * Returns the RGB integer values of a color specified by a "percentage string" like "%50,%20,%100". 
    *
    * @param    string
    * @return   array   [int red, int green, int blue]
    * @access   public
    */
    
function PercentageColor2RGB($color) {
        
        
// split the string %50,%20,%100 by ,
        
$color explode(","$color);        
                
        foreach (
$color as $k => $v) {
        
            
// remove the trailing percentage sign %
            
$v = (int)substr($v1);
            
            
// range checks
            
if ($v >= 100) {
                
$color[$k] = 255;
            } else if (
$v <= 0) {
                
$color[$k] = 0;
            } else {
                
$color[$k] = (int)(2.55 $v);
            }
            
        } 

        return 
$color;
    } 
// end func PercentageColor2RGB
    
// end class graphics
?>