uw Wed Mar 28 01:51:11 2001 EDT
Added files:
/php4/pear/Experimental/Image color_helper.php
Log:
Widget functions to deal with colors when creating images.
I'm not sure where I should place this, lets wait for the next classes
(gtext, gbutton) before we take care on the correct directory structure so
we can see if it's worth it's own directory.
Index: php4/pear/Experimental/Image/color_helper.php
+++ php4/pear/Experimental/Image/color_helper.php
<?php
/**
* Widget stuff: color translation, ...
*
* Several widget functions to deal with images especially simple
* functions to convert userdefined colors into RGB colors.
*
* @author Ulf Wendel <[EMAIL PROTECTED]>
* @version $Id: color_helper.php,v 1.1 2001/03/28 09:51:11 uw Exp $
*/
class ColorHelper {
/**
* Mapping from named colors to RGB values.
*
* @var array
* @see color2RGB()
*/
var $colornames = array(
"white" => array(255, 255, 255),
"black" => array(0, 0, 0),
"red" => array(255, 0, 0),
"green" => array(0, 255, 0),
"blue" => array(0, 0, 255)
);
/**
* 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 (3 == 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(0, 0 , 0);
}
// #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(0, 0, 0);
return array(
hexdec(substr($color, 1, 2)),
hexdec(substr($color, 3, 2)),
hexdec(substr($color, 5, 2))
);
} // 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(0, 0, 0);
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($v, 1);
// 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 ColorHelper
?>
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]