cvs: pear /Image_Color2 Color2.php package.xml package2.xml /Image_Color2/Color2 Model.php /Image_Color2/Color2/Model Cmyk.php Grayscale.php Hex.php
Hsl.php Hsv.php Named.php WebsafeHex.php /Image_Color2/Image Color2.php /Image_Color2/Image/Color2 Model.php /Image_Color2/Image/Color2/Model
Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php /Image_Color2/Tests AllTests.php Color2.php /Image_Color2/Tests/Model
AllTests.php Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php

From: Date: Fri, 14 Oct 2005 08:17:39 +0000
Subject: cvs: pear /Image_Color2 Color2.php package.xml package2.xml /Image_Color2/Color2 Model.php /Image_Color2/Color2/Model Cmyk.php Grayscale.php Hex.php
Hsl.php Hsv.php Named.php WebsafeHex.php /Image_Color2/Image Color2.php /Image_Color2/Image/Color2 Model.php /Image_Color2/Image/Color2/Model
Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php /Image_Color2/Tests AllTests.php Color2.php /Image_Color2/Tests/Model
AllTests.php Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-35506@lists.php.net to get a copy of this message
drewish Fri Oct 14 04:17:39 2005 EDT Added files: /pear/Image_Color2/Image Color2.php /pear/Image_Color2/Image/Color2 Model.php /pear/Image_Color2/Image/Color2/Model Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php /pear/Image_Color2/Tests AllTests.php Color2.php /pear/Image_Color2/Tests/Model AllTests.php Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php Removed files: /pear/Image_Color2 Color2.php /pear/Image_Color2/Color2 Model.php /pear/Image_Color2/Color2/Model Cmyk.php Grayscale.php Hex.php Hsl.php Hsv.php Named.php WebsafeHex.php Modified files: /pear/Image_Color2 package.xml package2.xml Log: * Realized I'd chosen a dumb directory structure. It works better if the directories are layed out in the same way they'll be installed.

http://cvs.php.net/diff.php/pear/Image_Color2/package.xml?r1=1.2&r2=1.3&ty=u Index: pear/Image_Color2/package.xml diff -u pear/Image_Color2/package.xml:1.2 pear/Image_Color2/package.xml:1.3 --- pear/Image_Color2/package.xml:1.2 Fri Oct 14 03:37:41 2005 +++ pear/Image_Color2/package.xml Fri Oct 14 04:17:36 2005 @@ -33,10 +33,10 @@ </notes> <filelist> <dir name="/"> - <dir baseinstalldir="Image" name="Color2" > + <dir baseinstalldir="Image" name="Image/Color2" > <file name="Model.php" role="php" /> </dir> <!-- /Color2 --> - <dir baseinstalldir="Image" name="Color2\Model" > + <dir baseinstalldir="Image" name="Image/Color2\Model" > <file name="Cmyk.php" role="php" /> <file name="Grayscale.php" role="php" /> <file name="Hex.php" role="php" /> @@ -45,7 +45,7 @@ <file name="Named.php" role="php" /> <file name="WebsafeHex.php" role="php" /> </dir> <!-- /Color2/Model --> - <file baseinstalldir="Image" name="Color2.php" role="php" /> + <file baseinstalldir="Image" name="Image/Color2.php" role="php" /> </dir> <!-- / --> </filelist> </release> http://cvs.php.net/diff.php/pear/Image_Color2/package2.xml?r1=1.2&r2=1.3&ty=u Index: pear/Image_Color2/package2.xml diff -u pear/Image_Color2/package2.xml:1.2 pear/Image_Color2/package2.xml:1.3 --- pear/Image_Color2/package2.xml:1.2 Fri Oct 14 03:37:41 2005 +++ pear/Image_Color2/package2.xml Fri Oct 14 04:17:36 2005 @@ -39,10 +39,10 @@ </notes> <contents> <dir name="/"> - <dir baseinstalldir="Image" name="Color2" > + <dir baseinstalldir="Image" name="Image/Color2" > <file name="Model.php" role="php" /> </dir> <!-- /Color2 --> - <dir baseinstalldir="Image" name="Color2/Model" > + <dir baseinstalldir="Image" name="Image/Color2/Model" > <file name="Cmyk.php" role="php" /> <file name="Grayscale.php" role="php" /> <file name="Hex.php" role="php" /> @@ -51,7 +51,7 @@ <file name="Named.php" role="php" /> <file name="WebsafeHex.php" role="php" /> </dir> <!-- /Color2/Model --> - <file baseinstalldir="Image" name="Color2.php" role="php" /> + <file baseinstalldir="Image" name="Image/Color2.php" role="php" /> </dir> <!-- / --> </contents> <dependencies> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2.php +++ pear/Image_Color2/Image/Color2.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Color2.php is the root of the Image_Colro2 package. * * PHP version 5 * * @category Image * @package Image_Color2 * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Color2.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * As a PEAR package, all exceptions are either PEAR_Exception or derived from * it. */ require_once 'PEAR/Exception.php'; /** * This class requires the Hex color model because it's the standard/simplest * way to represent an RGB values as a string. */ require_once 'Image/Color2/Model/Hex.php'; /** * The class requires the Named color model because it's cool to be able to * do things like $color = Image_Color2('teal') and have it work. */ require_once 'Image/Color2/Model/Named.php'; /** * Image_Color2 is a PHP5 package to convert between RGB and various other * color models. * * Here it is in action: * <code> * <?php * require_once 'Image/Color2.php'; * * \/\/ load a named string and view it as a hex string * $red = new Image_Color2('red'); * print $red->getHex() . "\n"; # '#ff0000' * * \/\/ load a hex string and view it as an RGB array * $blue = new Image_Color2('#0000ff'); * var_dump($blue->getRgb()); # array(0, 0, 255, 'type' => 'rgb') * * \/\/ find the average of red and blue (i.e. mix them)... * $avg = Image_Color2::average($red, $blue); * \/\/ ...then convert it to named color * print $avg->convertTo('named')->getString() . "\n"; # 'purple' * * \/\/ convert blue from RGB to HSV... * $hsv = $blue->convertTo('hsv'); * \/\/ ...then display it as an HSV string and array * print $hsv->getString() . "\n"; # '240 100% 100%' * print_r($hsv->getArray()); # array(240, 100, 100, 'type' => 'hsv') * ?> * </code> * * When converting from one color model to another, say HSL to HSV, an * intermediate RGB value is used. Each color model has a distinct gamut, or * range of expressible color values, and the use of an intermediate value * makes the conversions very imprecise. As a result, the output of these * conversions should be viewed as an approximation, unfit for any application * where color matching is important. * * A large portion of the code for this package was derived from the work of * Jason Lotito and the other contributors to Image_Color. * * @category Image * @package Image_Color2 * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @version Release: 0.1.2 * @link http://pear.php.net/package/Image_Color2 * @see Image_Color * @todo Figure out a clean way to support alpha channels. This class * will preserve them but as soon as you call a color model for * conversion they'll be discarded. I think this class will need * to maintain a separate variable for the alpha channel it really * seems independent of the color model. 50% red would be the same * no mater how you represent it. */ class Image_Color2 { /** * RGB value of the color. After it's assigned by the constructor it should * never be null. * @var array */ protected $_rgb; /** * Color model used to read a non-RGB color. This is assigned by the * constructor. If the source color is RGB no color model is needed so this * will be null. * @var Image_Color2_Model */ protected $_model = null; /** * Construct a color from a string, array, or an instance of * Image_Color2_Model. * <code> * \/\/ from a named string * $red = new Image_Color2('red'); * print $red->getHex(); \/\/ '#ff0000' * * \/\/ from a hex string * $blue = new Image_Color2('#0000ff'); * print $blue->getHex(); \/\/ '#0000ff' * * \/\/ from an array * $black = new Image_Color2(array(0,0,0)); * print $black->getHex(); \/\/ '#000000' * </code> * * @param array|string|Image_Color2_Model $src specifying a color. * Non-RGB arrays should include the type element to specify a * color model. Strings will be interpreted as hex if they * begin with a #, otherwise they'll be treated as named colors. * @throws PEAR_Exception if the color cannot be loaded. * @uses _createModelReflectionMethod() If the color is non-RGB the * function is used to construct an Image_Color2_Model for * conversion. */ public function __construct($src) { if (is_array($src)) { // check if a type parameter was offered up. $type = (isset($src['type'])) ? $src['type'] : ''; // type needs to be a proper case to match the class name. $type = ucwords($type); if (!$type || $type == 'Rgb') { $src['type'] = 'rgb'; $this->_model = null; $this->_rgb = $src; } else { $method = self::_createModelReflectionMethod($type, 'fromArray'); $this->_model = $method->invoke(null, $src); } } else if (is_string($src)) { if ('#' == substr($src, 0, 1)) { $this->_model = Image_Color2_Model_Hex::fromString($src); } else { $this->_model = Image_Color2_Model_Named::fromString($src); } } else if ($src instanceof Image_Color2_Model) { $this->_model = $src; } // at this point we either have a model, an rgb value, or a problem. if (!is_null($this->_model)) { $this->_rgb = $this->_model->getRgb(); } if (is_null($this->_rgb)) { throw new PEAR_Exception('Invalid color definition.'); } } /** * Return a ReflectionMethod of a Image_Color2_Model implementation found in * the Image/Color2/Model directory. * @param string $type Name of a ColorModel implementation (i.e. for * Image_Color2_Model_Hsv this would be 'hsv'). * @param string $methodName Name of a static factory method on the * ColorModel interface ('fromArray', 'fromString', or 'fromRgb'). * @return ReflectionMethod * @throws PEAR_Exception if the class cannot be loaded, or it does not * implement the Image_Color2_Model interface. * @uses Image_Color2_Model As the interface for color conversion. * @internal */ protected static function _createModelReflectionMethod($type, $methodName) { $classpath = 'Image/Color2/Model/' . $type . '.php'; if (!include_once($classpath)) { throw new PEAR_Exception( "File '{$classpath}' for $type was not found."); } $classname = 'Image_Color2_Model_' . $type; if (!class_exists($classname)) { throw new PEAR_Exception( "Class '{$classname}' for $type was not found."); } $reflect = new ReflectionClass($classname); if (!$reflect->implementsInterface('Image_Color2_Model')) { throw new PEAR_Exception( "Class '{$classname}' doesn't implement Image_Color2_Model."); } return $reflect->getMethod($methodName); } /** * Return the average of the RGB value of two Image_Color2 objects. If * both objects have an alpha channel it will be averaged too. * <code> * $red = new Image_Color2('red'); * $blue = new Image_Color2('blue'); * $color = Image_Color2::average($red, $blue); * print $color->convertTo('named')->getString(); \/\/ 'purple' * </code> * @param Image_Color2 $left * @param Image_Color2 $right * @return Image_Color2 */ public static function average(Image_Color2 $left, Image_Color2 $right) { $lrgb = $left->getRgb(); $rrgb = $right->getRgb(); // remove the type element so we can properly compare lengths unset($lrgb['type']); unset($rrgb['type']); // the color may be RGB or RGBA, either way, they need to be the same // length. $size = min(count($lrgb), count($rrgb)); // find the average of each pair of elements $avg = array(); for ($i = 0; $i < $size; $i++) { $avg[] = (integer) ((($lrgb[$i] + $rrgb[$i] ) / 2) + 0.5); } return new Image_Color2($avg); } /** * Return a copy of this color converted to another color model. * <code> * $blue = new Image_Color2('#0000ff'); * $hsv = $blue->convertTo('hsv'); * print $hsv->getString(); \/\/ '240 100% 100%' * </code> * @param string Name of a color model. If this variable is foo then a * class named Image_Color2_Model_Foo is required. * @return Image_Color2 * @throws PEAR_Exception if the desired color model cannot be found or it * cannot convert the color. * @uses _createModelReflectionMethod() The function is used to * construct an Image_Color2_Model that is passed back to the * constructor. */ public function convertTo($type) { $method = self::_createModelReflectionMethod($type, 'fromRgb'); $model = $method->invoke(null, $this->_rgb); if (is_null($model)) { throw new PEAR_Exception( "The '{$type}' color model couldn't convert the color."); } else { return new Image_Color2($model); } } /** * Return the color as a PEAR style RGB array. * * The optional 'type' => 'rgb' element should always be included. * * <code> * $color = new Image_Color2(array(0,128,255)); * print_r($color->getRgb()); \/\/ array(0, 128, 255, 'type' => 'rgb') * * \/\/ While PHP barfs if you write: * print $color->getRgb()[0]; \/\/ NOT VALID * \/\/ You can pass in an optional index parameter for the same effect: * print $color->getRgb(2); \/\/ 255 * print $color->getRgb('type'); \/\/ 'rgb' * </code> * @param mixed $index An optional index value to select an element of * the array. A null value returns the entire array. * @return mixed An array if no index parameter is provided. Otherwise, * provided the index is valid, a member of the array. * @uses $_rgb Returns a copy of the array. */ public function getRgb($index = null) { // return an element if requested if (isset($index)) { return $this->_rgb[$index]; } else { return $this->_rgb; } } /** * Return the color in a color model dependant, array format. If the color * was specified as an RGB array this will return the same results as * getRgb(). Otherwise, the results depend on the underlying color model. * * <code> * $color = new Image_Color2(array(0,128,255)); * print_r($color->getArray()); \/\/ array(0, 128, 255, 'type' => 'rgb') * * \/\/ While PHP barfs if you write: * print $color->getArray()[0]; \/\/ NOT VALID * \/\/ You can pass in an optional index parameter for the same effect: * print $color->getArray(2); \/\/ 255 * print $color->getArray('type'); \/\/ 'rgb' * </code> * * @param mixed $index An optional index value to select an element of * the array. A null value returns the entire array. * @return mixed An array if no index is provided. Otherwise, provided the * index is valid, a member of the array. * @uses $_rgb If the color was specified as RGB. * @uses $_model If the color wasn't specified as RGB. * @uses Image_Color2_Model::getArray() For the color conversion if it * wasn't originally RGB. */ public function getArray($index = null) { // get the array if (is_null($this->_model)) { $ret = $this->_rgb; } else { $ret = $this->_model->getArray(); } // return an element if requested if (isset($index)) { return $ret[$index]; } else { return $ret; } } /** * Return a hex string representation of the color. * * <code> * $color = new Image_Color2(array(171, 205, 239)); * print $color->getHex(); \/\/ '#abcdef' * </code> * * To obtain a websafe color, convert it using the * {@link Image_Color2_Model_WebsafeHex websafe hex color model}: * <code> * $color = new Image_Color2('#abcdef'); * print $color->convertTo('WebsafeHex')->getString(); \/\/ '#99ccff' * </code> * * @return string Hex RGB string in the form #abcdef. * @uses Image_Color2_Model_Hex::getString() */ public function getHex() { return Image_Color2_Model_Hex::fromRgb($this->_rgb)->getString(); } /** * Return the color as a string. If the color was specified as an RGB array * this is exactly the same as calling getHex(). Otherwise, the results * depend on the underlying color model. * * <code> * $red = new Image_Color2(array(255,0,0)); * print $red->getString(); \/\/ '#ff0000' * * $orange = new Image_Color2('orange'); * print $orange->getString(); \/\/ 'orange' * * $hsl = $orange->convertTo('hsl'); * print $hsl->getString(); \/\/ '38 100% 50%' * </code> * * @return string * @uses Image_Color2_Model::getString() If the color wasn't originally * RGB. * @uses getHex() If the color was originally specified as RGB. */ public function getString() { if (is_null($this->_model)) { return $this->getHex(); } else { return $this->_model->getString(); } } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model.php +++ pear/Image_Color2/Image/Color2/Model.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Model.php is a PHP5 package to convert between RGB and various other * color models. * * PHP version 5 * * @category Image * @package Image_Color2 * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Model.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * An interface for color model classes to allow conversion of colors to and * from RGB. This class should be imutable, meaning that once it's constructed * the value is not changed. * * @category Image * @package Image_Color2 * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 */ interface Image_Color2_Model { /** * Create an Image_Color2_Model from an RGB array. * * {@internal * If there's a problem the implementation may either return null or throw * an exception. The exception is recommended as you are able to provide * more detailed information on the failure. * * Be aware that the array may contain an alpha channel. This can be safely * ignored as the alpha channel is a responsibility of the Image_Color2 * class.}} * * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_ColorModel * @throws PEAR_Exception On an error an exception maybe thrown or null * returned, it's up to the implementation. * @see getRgb() */ static public function fromRgb($rgb); /** * Create an Image_Color2_Model from an array of values. * * {@internal * If there's a problem the implementation may either return null or throw * an exception. The exception is recommended as you are able to provide * more detailed information on the failure. * * If a channel element in the array is a string with a trailing percent * sign (%) it should be divided by 100, then treated as a float and forced * into a 0.0 to 1.0 range.}} * * Color models like CMYK and HSL have some components that are typically * expressed as either percentages or floating point values from 0.0 to 1.0. * This function tries to be very accomidating, if the value is followed by * a percent sign (%) it will be divided by 100 and then treated as a * zero to one floating point value. * * @param array $array The values and order are specific to each color * model. There may be an optional type element at the end of the * array. i.e: array(0, 255, 0, 'type' => 'rgb') * @return Image_ColorModel * @throws PEAR_Exception On an error an exception maybe thrown or null * returned, it's up to the implementation. * @see getArray() */ static public function fromArray($array); /** * Create an Image_ColorModel from a color model specific string. * * {@internal * If there's a problem the implementation may either return null or throw * an exception. The exception is recommended as you are able to provide * more detailed information on the failure. * * When a channel's value is followed by a % (with no whitespace * separating the two) it should be divided by 100, then treated as a * floating point value and forced into the 0.0 to 1.0 range. * * If applicable, color models should allow the channel * components to be separated by spaces and commas (i.e. '50% 23% 0%' * should be equivalent to '.5, .23, 0' and '50%, 23%, 0%').}} * * Color models like CMYK and HSL have some components that are typically * expressed as either percentages or floating point values from 0.0 to 1.0. * This function tries to be very accomidating, if the value is followed by * a percent sign (%) it will be divided by 100 and then treated as a * zero to one floating point value. * * The color's channels can be separated using a comma, a space or both. * * Examples of string formats used by several implementations of this * interface: * <pre> * Hex: '#abcdef' * Named: 'black' * CMYK: '100% 0% 75% 20%' or '1, 0, .75, .2' * HSL: '135, 100%, 40%' or '135 1.0 0.4' * </pre> * * @param string $str a string in a color model dependant format. * @return Image_ColorModel * @throws PEAR_Exception On an error an exception maybe thrown or null * returned, it's up to the implementation. * @see getString() */ static public function fromString($str); /** * Get an RGB array of the color. * * The 'type' => 'rgb' element must be included as the last element. * * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @see fromRgb() */ public function getRgb(); /** * Get an array of values to represent a color in this color model. This * should be parseable by fromArray() and should include a type element. * * {@internal * The 'type' => 'foo' where foo is the color model, element must be included * as the last element.}} * * @return array An array in a color model dependant format with a * type element. * @see fromArray() */ public function getArray(); /** * Get a string to represent a color in this color model. The string should * be parsable by fromString(). * * {@internal * Color channels should be separated with a comma and then a space. * * Integer values (like the Hue in HSL) should be formated as integers. * Floating point values that have a range of 0.0 - 1.0 should be converted * to a percentage for output.}} * * <pre> * HSL: '135, 100%, 40%' * CMYK: '100%, 0%, 75%, 20%' * Hex: '#00cc33' * </pre> * * @return string A string in a color model dependant format. * @see fromString() */ public function getString(); } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Cmyk.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Cmyk.php +++ pear/Image_Color2/Image/Color2/Model/Cmyk.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Cmyk.php contains code for converting between the RGB and CMYK color models. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Cmyk.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * CMYK is a subtractive colormodel used in printing. * * Colors are represented using Cyan, Magenta, Yellow, and blacK components * (the K actually stands for key plate which is typically printed in black). * * The conversions done by this class are very crude. The results should only be * considered as a rough approximation. * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 * @link http://en.wikipedia.org/wiki/CMYK_color_space */ class Image_Color2_Model_Cmyk implements Image_Color2_Model { /** * Cyan component * @var float 0-1 */ protected $_c; /** * Magneta component * @var float 0-1 */ protected $_m; /** * Yellow component * @var float 0-1 */ protected $_y; /** * Black component * @var float 0-1 */ protected $_k; /** * Constuct from four floats. * @param float $c 0.0 - 1.0 Cyan * @param float $m 0.0 - 1.0 Magenta * @param float $y 0.0 - 1.0 Yellow * @param float $k 0.0 - 1.0 Black */ protected function __construct($c, $m, $y, $k) { // special case: black if ($k >= 1.0) { $this->_c = $this->_m = $this->_y = 0.0; $this->_k = 1.0; } else { $this->_c = (float) $c; $this->_m = (float) $m; $this->_y = (float) $y; $this->_k = (float) $k; } } /** * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Hsl */ static public function fromRgb($rgb) { // compute the tCMY temporary $tC = 1 - ($rgb[0] / 255); $tM = 1 - ($rgb[1] / 255); $tY = 1 - ($rgb[2] / 255); $min = min($tC, $tM, $tY) ; if ($min == 1) { return new self(0,0,0,1); } else { // compute the tK temporary $K = $min; $tK = 1 - $K; return new self( ($tC - $K) / $tK, ($tM - $K) / $tK, ($tY - $K) / $tK, $K ); } } /** * @param array $array An array with four integers between 0 and 100 for * the each of the Cyan, Magenta, Yellow and blacK color * components. * @return Image_Color2_Model_Hsl */ static public function fromArray($array) { return new self($array[0], $array[1], $array[2], $array[3]); } /** * {@inheritdoc} * @param string $str In the format 'C%, M%, Y%, K%'. The percent sign, * comma, or space may be used to separate the channels. * @return Image_Color2_Model_Hsl */ static public function fromString($str) { // split it by commas or spaces $array = preg_split('/[, ]/', $str, -1, PREG_SPLIT_NO_EMPTY); // for each channel, if it has a % sign divide by 100. for ($i = 0; $i < 4; $i++) { if (substr($array[$i], -1) == '%') { $array[$i] = ((float) $array[$i]) / 100; } else { $array[$i] = ((float) $array[$i]); } } return new self($array[0], $array[1], $array[2], $array[3]); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { $r = 1 - ($this->_c * (1 - $this->_k) + $this->_k); $g = 1 - ($this->_m * (1 - $this->_k) + $this->_k); $b = 1 - ($this->_y * (1 - $this->_k) + $this->_k); // round to an integer by adding half and then casting. return array( (integer) (($r * 255) + 0.5), (integer) (($g * 255) + 0.5), (integer) (($b * 255) + 0.5), 'type' => 'rgb' ); } /** * {@inheritdoc} * @return array */ public function getArray() { return array( $this->_c, $this->_m, $this->_y, $this->_k, 'type' => 'cmyk' ); } /** * {@inheritdoc} * @return string In the format 'C%, M%, Y%, K%'. */ public function getString() { return round($this->_c * 100) . '%, ' . round($this->_m * 100) . '%, ' . round($this->_y * 100) . '%, ' . round($this->_k * 100) . '%'; } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Grayscale.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Grayscale.php +++ pear/Image_Color2/Image/Color2/Model/Grayscale.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Grayscale.php contains code for converting RGB to grayscale. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Grayscale.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * Grayscale color model converts RGB values to gray scale using perceptive * proportional sum of the RGB channels. * * The formula was taken, indirectly, from the GIMP: * "The formula used in the GIMP is Y = 0.3R + 0.59G + 0.11B; this result is * known as luminance. The weights used to compute luminance are related to the * monitor's phosphors. The explanation for these weights is due to the fact * that for equal amounts of color the eye is most sensitive to green, then * red, and then blue. This means that for equal amounts of green and blue * light the green will, nevertheless, seem much brighter". * {@link http://gimp-savvy.com/BOOK/index.html?node54.html Gimp-Savvy.com} * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 */ class Image_Color2_Model_Grayscale implements Image_Color2_Model { /** * Grayscale component * @var float 0.0 - 1.0 */ protected $_val = 0; const WEIGHT_RED = 0.3; const WEIGHT_GREEN = 0.59; const WEIGHT_BLUE = 0.11; /** * Construct the Grayscale color from RGB components. * * @param float 0.0 - 1.0 */ protected function __construct($val) { if ($val <= 0.0) { $this->_val = 0.0; } else if ($val <= 1.0) { $this->_val = (float) $val; } else { $this->_val = 1.0; } } /** * Pass the RGB components to the constructor and return a new instance of * Image_Color2_Model. * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Grayscale */ static public function fromRgb($rgb) { // if all three values are the same, it's already grayscale. if ($rgb[0] == $rgb[1] && $rgb[1] == $rgb[2]) { $val = $rgb[0] / 255; } else { $val = ($rgb[0] / 255) * self::WEIGHT_RED; $val += ($rgb[1] / 255) * self::WEIGHT_GREEN; $val += ($rgb[2] / 255) * self::WEIGHT_BLUE; } return new self($val); } /** * Pull the grayscale component out of the array and return a instance of * Image_Color2_Model. * @param array $array An array with a floating point gray scale * component. * @return Image_Color2_Model_Grayscale */ static public function fromArray($array) { return new self($array[0]); } /** * Convert the Grayscale string into RGB components and return a new * instance of Image_Color2_Model. * @param string $str Grayscale expressed as a floating point value from * zero to one ('0.005', '.3333', '1') or percent string ('0.5%', * '33.33%', '100%'). * @return Image_Color2_Model_Grayscale */ static public function fromString($str) { // drop any trailing spaces $str = trim($str); // convert it to a float $val = (float) $str; // if it was a percentage, divide by 100 if (substr($str, -1) == '%') { $val /= 100; } return new self($val); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { $i = (integer) (($this->_val * 255) + 0.5); return array($i, $i, $i, 'type' => 'rgb'); } /** * {@inheritdoc} * @return array An array with two elements, the first is the color, a * float from 0 to 1 and the second type => named element. */ public function getArray() { return array(round($this->_val, 8), 'type' => 'Grayscale'); } /** * {@inheritdoc} * @return string */ public function getString() { return round($this->_val * 100, 2) . '%'; } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Hex.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Hex.php +++ pear/Image_Color2/Image/Color2/Model/Hex.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Hex.php contains code for representing RGB colors as hex strings. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Hex.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * Hex color model defines RGB colors using hexadecimal values for each * component. * * Typically, these values are concatenated into a string and * prefixed by the # character: * <pre>#339900 * #390</pre> * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 */ class Image_Color2_Model_Hex implements Image_Color2_Model { /** * Red component * @var integer 0-255 */ protected $_r = 0; /** * Green component * @var integer 0-255 */ protected $_g = 0; /** * Blue component * @var integer 0-255 */ protected $_b = 0; /** * Construct the hex color from RGB components. * * @param integer $r 0-255 * @param integer $g 0-255 * @param integer $b 0-255 */ protected function __construct($r, $g, $b) { $this->_r = (integer) $r; $this->_g = (integer) $g; $this->_b = (integer) $b; } /** * Pass the RGB components to the constructor and return a new instance of * Image_Color2_Model. * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Hex */ static public function fromRgb($rgb) { return new self($rgb[0], $rgb[1], $rgb[2]); } /** * Construct a new Image_Color2_Model from the components in an array. The * array format happens to be the PEAR style RGB array so we just hand it * off to {@link fromRgb}. * * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Hex * @uses fromRgb() becase both functions share the exact same parameter * format. */ static public function fromArray($array) { return self::fromRgb($array); } /** * Convert the hex string into RGB components and return a new instance of * Image_Color2_Model. Allow both #abc and #aabbcc forms. * @param string $str in the format 'AABBCC', 'ABC', '#ABCDEF', or '#ABC' * @return Image_Color2_Model_Hex */ static public function fromString($str) { $color = str_replace('#', '', $str); if (strlen($color) == 3) { // short #abc form return new self( hexdec($color{0} . $color{0}), hexdec($color{1} . $color{1}), hexdec($color{2} . $color{2}) ); } else { // long #aabbcc form return new self( hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)) ); } } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { return array($this->_r, $this->_g, $this->_b, 'type' => 'rgb'); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type'=>'rgb' element. * @uses getRgb() because both functions share the same return format. */ public function getArray() { return $this->getRgb(); } /** * {@inheritdoc} * @return string A string in the format '#RRGGBB' where each channel is * a hex byte. */ public function getString() { return sprintf('#%02x%02x%02x', $this->_r, $this->_g, $this->_b); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Hsl.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Hsl.php +++ pear/Image_Color2/Image/Color2/Model/Hsl.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Hsl.php contains code for converting between the RGB and HSL color models. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Hsl.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * HSL (also called HSI or HLS) color model defines colors in terms of Hue, * Saturation, and Lightness (also Luminance, Luminosity or Intensity). * * CSS3 allows the use of this color model to define colors. * * @todo Need to do more testing on the conversions done by this class. * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 * @link http://en.wikipedia.org/wiki/HLS_color_space */ class Image_Color2_Model_Hsl implements Image_Color2_Model { /** * Hue component * @var integer 0-360 */ protected $_h; /** * Saturation component * @var float 0.0-1.0 */ protected $_s; /** * Lightness component * @var float 0.0-1.0 */ protected $_l; /** * Constuct from a integer for Hue, and two floats for Saturation and * Lightness. * @param integer $h 0 - 360 * @param float $s 0.0 - 1.0 * @param float $l 0.0 - 1.0 */ protected function __construct($h, $s, $l) { $this->_h = (integer) $h; $this->_s = $s; $this->_l = $l; } /** * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Hsl */ static public function fromRgb($rgb) { $r = $rgb[0] / 255; $g = $rgb[1] / 255; $b = $rgb[2] / 255; $min = min($r, $g, $b); $max = max($r, $g, $b); switch ($max) { case 0: // it's black like my soul. // value = 0, hue and saturation are undefined $h = $s = $l = 0; break; case $min: // grey // saturation = 0, hue is undefined $h = $s = 0; $l = $max; break; default: // normal color... color $delta = $max - $min; // hue... if( $r == $max ) { // between yellow & magenta $h = 0 + ( $g - $b ) / $delta; } else if( $g == $max ) { // between cyan & yellow $h = 2 + ( $b - $r ) / $delta; } else { // between magenta & cyan $h = 4 + ( $r - $g ) / $delta; } // ...convert hue to degrees $h *= 60; if($h < 0 ) { $h += 360; } // saturation $s = $delta; // lightness $l = ($max + $min) / 2; } return new self($h, $s, $l); } /** * Pull the HSL components out of the array and return a color model * object. Hue is a integer degree between from 0 to 360 and the Saturation * and Lightness are floats between 0 and 1. * @param array $array An array with the Hue as an integer 0-360 in the * first element, Saturation and Lightness as floats 0.0-1.0 in the * second as the third elements respectively. * @return Image_Color2_Model_Hsl */ static public function fromArray($array) { return new self($array[0], $array[1], $array[2]); } /** * Pull the HSL components out of the string and return a color model * object. Hue is a integer degree between 0 and 360 while the Saturation * and Lightness are either percentages or values between 0 and 1. * @param string $str In the form '120, 25%, 50%' or '120, .25, .50' * @return Image_Color2_Model_Hsl */ static public function fromString($str) { // split it by commas or spaces $a = preg_split('/[, ]/', $str, -1, PREG_SPLIT_NO_EMPTY); $h = $a[0]; $s = substr($a[1], -1) == '%' ? ((float) $a[1]) / 100 : (float) $a[1]; $l = substr($a[2], -1) == '%' ? ((float) $a[2]) / 100 : (float) $a[2]; return new self($h, $s, $l); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { // copy the members to locals for ease of reading $h = $this->_h / 360; $s = $this->_s; $l = $this->_l; if ($s == 0.0) { // saturation is 0 so it's a grey $r = $g = $b = $l; } else { if ($l < 0.5) { $temp2 = $l * (1.0 + $s); } else { $temp2 = ($l + $s) - ($l * $s); } $temp1 = (2.0 * $l) - $temp2; $r = self::rgbFromHue($temp1, $temp2, $h + (1 / 3)); $g = self::rgbFromHue($temp1, $temp2, $h); $b = self::rgbFromHue($temp1, $temp2, $h - (1 / 3)); } // add .5 and cast to an int to round the values. calling round() // returns a float so you'd have to cast it back anyway. this avoids // the overhead of a function call. return array( (integer) ($r * 255 + 0.5), (integer) ($g * 255 + 0.5), (integer) ($b * 255 + 0.5), 'type' => 'rgb' ); } /** * This is a private function to convert the hue information into an RGB * component. * @param float * @param float * @param float * @return float */ private static function rgbFromHue( $v1, $v2, $vH ) { if ( $vH < 0 ) { $vH += 1; } if ( $vH > 1 ) { $vH -= 1; } if ( 6 * $vH < 1 ) { return $v1 + ( $v2 - $v1 ) * 6 * $vH; } else if ( 2 * $vH < 1 ) { return $v2; } else if ( 3 * $vH < 2 ) { return $v1 + ( $v2 - $v1 ) * ( ( 2 / 3 ) - $vH ) * 6; } else { return $v1; } } /** * {@inheritdoc} * @return array */ public function getArray() { return array( $this->_h, $this->_s, $this->_l, 'type' => 'hsl' ); } /** * {@inheritdoc} * @return string In the format 'H S% L%'. */ public function getString() { return sprintf( '%d, %d%%, %d%%', $this->_h, round($this->_s * 100), round($this->_l * 100) ); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Hsv.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Hsv.php +++ pear/Image_Color2/Image/Color2/Model/Hsv.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Hsv.php contains code for converting between the RGB and HSL color models. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Hsv.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * HSV (also called HSB) color model defines colors in terms of Hue, * Saturation, and Value (or Brightness). * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 * @link http://en.wikipedia.org/wiki/HSV_color_space */ class Image_Color2_Model_Hsv implements Image_Color2_Model { /** * Hue component * @var integer 0-360 */ protected $_h; /** * Saturation component * @var float 0.0-1.0 */ protected $_s; /** * Value component * @var float 0.0-1.0 */ protected $_v; /** * Construct from HSV components. * * @param integer $h 0 - 360 * @param float $s 0.0 - 1.0 * @param float $v 0.0 - 1.0 */ protected function __construct($h, $s, $v) { $this->_h = (integer) $h; $this->_s = (float) $s; $this->_v = (float) $v; } /** * Convert the RGB array into HSV components and return a new Model * class. * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Hsv */ static public function fromRgb($rgb) { $r = $rgb[0] / 255; $g = $rgb[1] / 255; $b = $rgb[2] / 255; $min = min($r, $g, $b); $max = max($r, $g, $b); switch ($max) { case 0: // it's black like my soul. // value = 0, hue and saturation are undefined $h = $s = $v = 0; break; case $min: // grey // saturation = 0, hue is undefined $h = $s = 0; $v = $max; break; default: // normal color... color $delta = $max - $min; // hue... if( $r == $max ) { // between yellow & magenta $h = 0 + ( $g - $b ) / $delta; } else if( $g == $max ) { // between cyan & yellow $h = 2 + ( $b - $r ) / $delta; } else { // between magenta & cyan $h = 4 + ( $r - $g ) / $delta; } // ...convert hue to degrees $h *= 60; if($h < 0 ) { $h += 360; } // saturation $s = $delta / $max; // value $v = $max; } return new self($h, $s, $v); } /** * Pull the HSV components out of the array and return a color model object. * Hue is a integer degree between from 0 to 360 and the Saturation and * Value are floats between 0 and 1. * @param array $array with Hue as first element, Saturation as the * second and Value as the third. * @return Image_Color2_Model_Hsv */ static public function fromArray($array) { return new self($array[0], $array[1], $array[2]); } /** * Pull the HSV components out of the string and return a color model * object. Hue is a integer degree between from 0 to 360 and the Saturation * and Value are either percentages or values between 0 and 1. * @param string $str in the form '120, 25%, 50%' or '120, .25, .50' * @return Image_Color2_Model_Hsv */ static public function fromString($str) { // split it by commas or spaces $a = preg_split('/[, ]/', $str, -1, PREG_SPLIT_NO_EMPTY); $h = $a[0]; $s = substr($a[1], -1) == '%' ? ((float) $a[1]) / 100 : (float) $a[1]; $v = substr($a[2], -1) == '%' ? ((float) $a[2]) / 100 : (float) $a[2]; return new self($h, $s, $v); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { if ($this->_s == 0) { // saturation is 0 so it's a grey $r = $g = $b = $this->_v; } else { $h = $this->_h / 60.0; $s = $this->_s; $v = $this->_v; $hi = floor($h); $f = $h - $hi; $p = ($v * (1.0 - $s)); $q = ($v * (1.0 - ($f * $s))); $t = ($v * (1.0 - ((1.0 - $f) * $s))); switch( $hi ) { case 0: $r = $v; $g = $t; $b = $p; break; case 1: $r = $q; $g = $v; $b = $p; break; case 2: $r = $p; $g = $v; $b = $t; break; case 3: $r = $p; $g = $q; $b = $v; break; case 4: $r = $t; $g = $p; $b = $v; break; default: $r = $v; $g = $p; $b = $q; break; } } // add .5 and cast to an int to round the values. calling round() // returns a float so you'd have to cast it back anyway. this avoids // the overhead of a function call. return array( (integer) ($r * 255 + 0.5), (integer) ($g * 255 + 0.5), (integer) ($b * 255 + 0.5), 'type' => 'rgb' ); } /** * {@inheritdoc} * @return array */ public function getArray() { return array( $this->_h, $this->_s, $this->_v, 'type' => 'hsv' ); } /** * {@inheritdoc} * @return string In the format 'H S% V%'. */ public function getString() { return $this->_h . ', ' . round($this->_s * 100) . '%, ' . round($this->_v * 100) . '%'; } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/Named.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/Named.php +++ pear/Image_Color2/Image/Color2/Model/Named.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Named.php contains code for matching RGB colors to their names. * * PHP version 5 * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @copyright 2005 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version $Id: Named.php,v 1.1 2005/10/14 08:17:38 drewish Exp $ * @link http://pear.php.net/package/Image_Color2 */ /** * This class implements the Image_Color2_Model interface. */ require_once 'Image/Color2/Model.php'; /** * Named color model represents colors by looking up the "webstandard" name * in a list of corresponding RGB values. * * This class is based on code written by Sebastian Bergmann for Image_Color. * * @category Image * @package Image_Color2 * @subpackage Model * @author andrew morton <drewish@katherinehouse.com> * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @copyright 2005 * @license http://opensource.org/licenses/lgpl-license.php * GNU Lesser General Public License, Version 2.1 * @link http://pear.php.net/package/Image_Color2 * @todo Find out and then explain where this list of names came from. * Are there any that we're missing? * @todo Look into rounding colors to the closest named value. */ class Image_Color2_Model_Named implements Image_Color2_Model { /** * Name of this color. It should exist in self::$_colornames. * @var string */ protected $_name = ''; /** * Array of named colors and values. * @var array * @ignore so this doesn't clutter up the PHPDocs. */ protected static $_colornames = array( 'aliceblue' => array(240, 248, 255), 'antiquewhite' => array(250, 235, 215), 'aqua' => array( 0, 255, 255), 'aquamarine' => array(127, 255, 212), 'azure' => array(240, 255, 255), 'beige' => array(245, 245, 220), 'bisque' => array(255, 228, 196), 'black' => array( 0, 0, 0), 'blanchedalmond' => array(255, 235, 205), 'blue' => array( 0, 0, 255), 'blueviolet' => array(138, 43, 226), 'brown' => array(165, 42, 42), 'burlywood' => array(222, 184, 135), 'cadetblue' => array( 95, 158, 160), 'chartreuse' => array(127, 255, 0), 'chocolate' => array(210, 105, 30), 'coral' => array(255, 127, 80), 'cornflowerblue' => array(100, 149, 237), 'cornsilk' => array(255, 248, 220), 'crimson' => array(220, 20, 60), 'cyan' => array( 0, 255, 255), 'darkblue' => array( 0, 0, 13), 'darkcyan' => array( 0, 139, 139), 'darkgoldenrod' => array(184, 134, 11), 'darkgray' => array(169, 169, 169), 'darkgreen' => array( 0, 100, 0), 'darkkhaki' => array(189, 183, 107), 'darkmagenta' => array(139, 0, 139), 'darkolivegreen' => array( 85, 107, 47), 'darkorange' => array(255, 140, 0), 'darkorchid' => array(153, 50, 204), 'darkred' => array(139, 0, 0), 'darksalmon' => array(233, 150, 122), 'darkseagreen' => array(143, 188, 143), 'darkslateblue' => array( 72, 61, 139), 'darkslategray' => array( 47, 79, 79), 'darkturquoise' => array( 0, 206, 209), 'darkviolet' => array(148, 0, 211), 'deeppink' => array(255, 20, 147), 'deepskyblue' => array( 0, 191, 255), 'dimgray' => array(105, 105, 105), 'dodgerblue' => array( 30, 144, 255), 'firebrick' => array(178, 34, 34), 'floralwhite' => array(255, 250, 240), 'forestgreen' => array( 34, 139, 34), 'fuchsia' => array(255, 0, 255), 'gainsboro' => array(220, 220, 220), 'ghostwhite' => array(248, 248, 255), 'gold' => array(255, 215, 0), 'goldenrod' => array(218, 165, 32), 'gray' => array(128, 128, 128), 'green' => array( 0, 128, 0), 'greenyellow' => array(173, 255, 47), 'honeydew' => array(240, 255, 240), 'hotpink' => array(255, 105, 180), 'indianred' => array(205, 92, 92), 'indigo' => array(75, 0, 130), 'ivory' => array(255, 255, 240), 'khaki' => array(240, 230, 140), 'lavender' => array(230, 230, 250), 'lavenderblush' => array(255, 240, 245), 'lawngreen' => array(124, 252, 0), 'lemonchiffon' => array(255, 250, 205), 'lightblue' => array(173, 216, 230), 'lightcoral' => array(240, 128, 128), 'lightcyan' => array(224, 255, 255), 'lightgoldenrodyellow' => array(250, 250, 210), 'lightgreen' => array(144, 238, 144), 'lightgrey' => array(211, 211, 211), 'lightpink' => array(255, 182, 193), 'lightsalmon' => array(255, 160, 122), 'lightseagreen' => array( 32, 178, 170), 'lightskyblue' => array(135, 206, 250), 'lightslategray' => array(119, 136, 153), 'lightsteelblue' => array(176, 196, 222), 'lightyellow' => array(255, 255, 224), 'lime' => array( 0, 255, 0), 'limegreen' => array( 50, 205, 50), 'linen' => array(250, 240, 230), 'magenta' => array(255, 0, 255), 'maroon' => array(128, 0, 0), 'mediumaquamarine' => array(102, 205, 170), 'mediumblue' => array( 0, 0, 205), 'mediumorchid' => array(186, 85, 211), 'mediumpurple' => array(147, 112, 219), 'mediumseagreen' => array( 60, 179, 113), 'mediumslateblue' => array(123, 104, 238), 'mediumspringgreen' => array( 0, 250, 154), 'mediumturquoise' => array(72, 209, 204), 'mediumvioletred' => array(199, 21, 133), 'midnightblue' => array( 25, 25, 112), 'mintcream' => array(245, 255, 250), 'mistyrose' => array(255, 228, 225), 'moccasin' => array(255, 228, 181), 'navajowhite' => array(255, 222, 173), 'navy' => array( 0, 0, 128), 'oldlace' => array(253, 245, 230), 'olive' => array(128, 128, 0), 'olivedrab' => array(107, 142, 35), 'orange' => array(255, 165, 0), 'orangered' => array(255, 69, 0), 'orchid' => array(218, 112, 214), 'palegoldenrod' => array(238, 232, 170), 'palegreen' => array(152, 251, 152), 'paleturquoise' => array(175, 238, 238), 'palevioletred' => array(219, 112, 147), 'papayawhip' => array(255, 239, 213), 'peachpuff' => array(255, 218, 185), 'peru' => array(205, 133, 63), 'pink' => array(255, 192, 203), 'plum' => array(221, 160, 221), 'powderblue' => array(176, 224, 230), 'purple' => array(128, 0, 128), 'red' => array(255, 0, 0), 'rosybrown' => array(188, 143, 143), 'royalblue' => array( 65, 105, 225), 'saddlebrown' => array(139, 69, 19), 'salmon' => array(250, 128, 114), 'sandybrown' => array(244, 164, 96), 'seagreen' => array( 46, 139, 87), 'seashell' => array(255, 245, 238), 'sienna' => array(160, 82, 45), 'silver' => array(192, 192, 192), 'skyblue' => array(135, 206, 235), 'slateblue' => array(106, 90, 205), 'slategray' => array(112, 128, 144), 'snow' => array(255, 250, 250), 'springgreen' => array( 0, 255, 127), 'steelblue' => array( 70, 130, 180), 'tan' => array(210, 180, 140), 'teal' => array( 0, 128, 128), 'thistle' => array(216, 191, 216), 'tomato' => array(255, 99, 71), 'turquoise' => array( 64, 224, 208), 'violet' => array(238, 130, 238), 'wheat' => array(245, 222, 179), 'white' => array(255, 255, 255), 'whitesmoke' => array(245, 245, 245), 'yellow' => array(255, 255, 0), 'yellowgreen' => array(154, 205, 50) ); /** * Construct a named color. * * @param string $name A color name in self::$_colorname. It will be * converted to lowercase and spaces will be removed before trying * to match it. */ protected function __construct($name) { $name = str_replace(' ', '', strtolower($name)); if (array_key_exists($name, self::$_colornames)) { $this->_name = $name; } else { throw new PEAR_Exception("The color '{$str}' is unknown."); } } /** * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_Named */ static public function fromRgb($rgb) { // remove the type so it'll match something in the list. unset($rgb['type']); $name = array_search($rgb, self::$_colornames, true); if ($name === false) { throw new PEAR_Exception('There is no name for this color. '); } return new self($name); } /** * @param array $array with the color name as a string in the first * element. The name will be converted to lowecase and spaces will * be removed before trying to match it against the list of known * colors. * @return Image_Color2_Model_Named */ static public function fromArray($array) { return new self($array[0]); } /** * * @param string $str A color name. The name will be converted to * lowecase and spaces will be removed before trying to match it * against the list of known colors. * @return Image_Color2_Model_Named */ static public function fromString($str) { return new self($str); } /** * {@inheritdoc} * @return array A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. */ public function getRgb() { $rgb = self::$_colornames[$this->_name]; $rgb['type'] = 'rgb'; return $rgb; } /** * {@inheritdoc} * @return array An array with two elements, the first is the color name * the second is the type => named. */ public function getArray() { return array($this->_name, 'type' => 'named'); } /** * {@inheritdoc} * @return string The name of the color. */ public function getString() { return $this->_name; } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Image/Color2/Model/WebsafeHex.php?r=1.1&p=1 Index: pear/Image_Color2/Image/Color2/Model/WebsafeHex.php +++ pear/Image_Color2/Image/Color2/Model/WebsafeHex.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * WebsafeHex.php contains code for websafe hex RGB colors. * * PHP version 5 * * @version $Id: WebsafeHex.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @category Image * @package Image_Color2 * @subpackage Model * @copyright 2005 * @author andrew morton <drewish@katherinehouse.com> */ /** * This class extends the Image_Color2_Model_Hex class. */ require_once 'Image/Color2/Model/Hex.php'; /** * Hex color model that is limited to websafe colors. * * @author andrew morton <drewish@katherinehouse.com> * @category Image * @package Image_Color2 * @subpackage Model */ class Image_Color2_Model_WebsafeHex extends Image_Color2_Model_Hex { /** * Construct the websafe hex color from RGB components. All values will be * rounded to the closest websafe value. * * @param integer $r Red 0-255 * @param integer $g Green 0-255 * @param integer $b Blue 0-255 * @uses round() to round to websafe values. */ protected function __construct($r, $g, $b) { $this->_r = self::round((integer) $r); $this->_g = self::round((integer) $g); $this->_b = self::round((integer) $b); } /** * Round a RGB element to the closest websafe value. * * Invalid values--those less than 0 or greater than 255--are forced to the * minimum or maximum values respectively. * * @param integer $int An RGB color component. * @return integer A websafe RGB component (0x00, 0x33, 0x66, 0x99, 0xcc, * or 0xff). */ static public function round($int) { if ($int < 0x1a ) { return 0x00; } else if ( $int < 0x4d ) { return 0x33; } else if ( $int < 0x80 ) { return 0x66; } else if ( $int < 0xB3 ) { return 0x99; } else if ( $int < 0xE6 ) { return 0xCC; } else { return 0xFF; } } /** * {@inheritdoc} * @param array $rgb A PEAR style RGB array containing three integers * from 0 to 255 for the Red, Green, and Blue color components * followed by a 'type' => 'rgb' element. * @return Image_Color2_Model_WebsafeHex */ static public function fromRgb($rgb) { return new self($rgb[0], $rgb[1], $rgb[2]); } /** * {@inheritdoc} * @param array $array * @return Image_Color2_Model_WebsafeHex */ static public function fromArray($array) { return new self( hexdec($array[0]), hexdec($array[1]), hexdec($array[2]) ); } /** * {@inheritdoc} * @param string $str In the format 'AABBCC', 'ABC', '#ABCDEF', or '#ABC'. * @return Image_Color2_Model_WebsafeHex */ static public function fromString($str) { $color = str_replace('#', '', $str); return new self( hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)) ); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/AllTests.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/AllTests.php +++ pear/Image_Color2/Tests/AllTests.php <?php /** * Runner for all ColorModel tests. * * @version $Id: AllTests.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ if (!defined('PHPUnit2_MAIN_METHOD')) { define('PHPUnit2_MAIN_METHOD', 'Image_Color2_Tests_AllTests::main'); } require_once 'PHPUnit2/Framework/TestSuite.php'; require_once 'PHPUnit2/TextUI/TestRunner.php'; require_once 'PHPUnit2/HtmlUI/TestRunner.php'; set_include_path(realpath('../') . PATH_SEPARATOR . get_include_path()); require_once 'Tests/Color2.php'; require_once 'Tests/Model/AllTests.php'; class Image_Color2_Tests_AllTests { public static function main() { if (php_sapi_name() == 'cli') { PHPUnit2_TextUI_TestRunner::run(self::suite()); } else { PHPUnit2_HtmlUI_TestRunner::run(self::suite()); } } public static function suite() { $suite = new PHPUnit2_Framework_TestSuite('Color2 Tests'); $suite->addTestSuite('Image_Color2_Tests_Color2'); $suite->addTest(Image_Color2_Tests_Model_AllTests::suite()); return $suite; } } if (PHPUnit2_MAIN_METHOD == 'Image_Color2_Tests_AllTests::main') { Image_Color2_Tests_AllTests::main(); } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Color2.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Color2.php +++ pear/Image_Color2/Tests/Color2.php <?php /** * Image_Color2 Tests * * @version $Id: Color2.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ define('COLOR2_BASEDIR', realpath(dirname(__FILE__) . '/..')); require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2.php'; require_once 'Image/Color2/Model/Hex.php'; require_once 'Image/Color2/Model/Hsv.php'; require_once 'Image/Color2/Model/Named.php'; class Image_Color2_Tests_Color2 extends PHPUnit2_Framework_TestCase { function testConstruct_FromArray_RgbWithoutType() { $color = new Image_Color2(array(0,0,0)); $this->assertNotNull($color); $this->assertEquals(array(0,0,0,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromArray_Rgb() { $color = new Image_Color2(array(1,2,3,'type'=>'rgb')); $this->assertNotNull($color); $this->assertEquals(array(1,2,3,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromArray_Rgba() { $color = new Image_Color2(array(1,2,3,4,'type'=>'rgb')); $this->assertNotNull($color); $this->assertEquals(array(1,2,3,4,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromArray_Hex() { $color = new Image_Color2(array(0xff,0xff, 0xff,'type'=>'hex')); $this->assertNotNull($color); $this->assertEquals(array(255,255,255,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromArray_Hsv() { $color = new Image_Color2(array(210, 0.28, 0.94, 'type'=>'hsv')); $this->assertNotNull($color); $this->assertEquals(array(173, 206, 240, 'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromArray_InvalidType() { try { $color = new Image_Color2(array(1, 'type'=>'badtype')); } catch (PEAR_Exception $ex) { return; } $this->fail('an exception should have been thrown.'); } function testConstruct_FromString_Hex() { $color = new Image_Color2('#FFFFFF'); $this->assertNotNull($color); $this->assertEquals(array(255,255,255,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromString_Named() { $color = new Image_Color2('white'); $this->assertNotNull($color); $this->assertEquals(array(255,255,255,'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromString_NamedInvalid() { try { $color = new Image_Color2('madeupcolor'); } catch (PEAR_Exception $ex) { return; } $this->fail('an exception should have been thrown.'); } function testConstruct_FromColorModel_Named() { $model = Image_Color2_Model_Named::fromString('teal'); $color = new Image_Color2($model); $this->assertNotNull($color); $this->assertEquals(array(0, 128, 128, 'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromColorModel_Hsv() { $model = Image_Color2_Model_Hsv::fromArray(array(210, 0.28, 0.94, 'type'=>'hsv')); $color = new Image_Color2($model); $this->assertNotNull($color); $this->assertEquals(array(173, 206, 240, 'type'=>'rgb'), $color->getRgb()); } function testConstruct_FromColorModel_Bad() { try { $color = new Image_Color2(null); } catch (PEAR_Exception $ex) { return; } $this->fail('an exception should have been thrown.'); } function testGetHex() { $color = new Image_Color2('white'); $result = $color->getHex(); $this->assertType('string', $result); $this->assertEquals('#ffffff', $result); } function testGetRgb() { $color = new Image_Color2(array(0,128,255)); $result = $color->getRgb(); $this->assertType('array', $result); $this->assertEquals(array(0,128,255,'type'=>'rgb'), $result); } function testGetRgb_UseIndex() { $color = new Image_Color2(array(0,128,255)); $result = $color->getRgb(0); $this->assertEquals(0, $result); $result = $color->getRgb(1); $this->assertEquals(128, $result); $result = $color->getRgb(2); $this->assertEquals(255, $result); $result = $color->getRgb('type'); $this->assertEquals('rgb', $result); } function testGetArray_Rgb() { $color = new Image_Color2(array(0,128,255)); $result = $color->getArray(); $this->assertType('array', $result); $this->assertEquals(array(0,128,255,'type'=>'rgb'), $result); } function testGetArray_Hex() { $color = new Image_Color2(array(0x0, 0x77, 0xFF,'type'=>'hex')); $result = $color->getArray(); $this->assertType('array', $result); $this->assertEquals(array(0x0, 0x77, 0xFF,'type'=>'rgb'), $result); } function testGetArray_Hsv() { $color = new Image_Color2(array(210, 0.28, 0.94, 'type'=>'hsv')); $result = $color->getArray(); $this->assertType('array', $result); $this->assertEquals(array(210, 0.28, 0.94, 'type'=>'hsv'), $result); } function testGetArray_UseIndex() { $color = new Image_Color2(array(1,128,255)); $result = $color->getArray(0); $this->assertEquals(1, $result); $result = $color->getArray(1); $this->assertEquals(128, $result); $result = $color->getArray(2); $this->assertEquals(255, $result); $result = $color->getArray('type'); $this->assertEquals('rgb', $result); } function testConvertTo_NamedToHex() { $color = new Image_Color2('orange'); $color2 = $color->convertTo('hex'); $this->assertType('Image_Color2', $color2); $result = $color2->getString(); $this->assertEquals('#ffa500', $result); } function testConvertTo_RgbToNamed() { $color = new Image_Color2(array(128,128,128)); $color2 = $color->convertTo('named'); $this->assertType('Image_Color2', $color2); $result = $color2->getString(); $this->assertEquals('gray', $result); } function testGetString_Rgb() { $color = new Image_Color2(array(0,128,255)); $result = $color->getString(); $this->assertEquals('#0080ff', $result); } function testGetString_Named() { $color = new Image_Color2('orange'); $result = $color->getString(); $this->assertEquals('orange', $result); } function testGetString_Hex() { $color = new Image_Color2('#abcdef'); $result = $color->getString(); $this->assertEquals('#abcdef', $result); } function testAverage_Rgb() { $red = new Image_Color2('red'); $blue = new Image_Color2('blue'); $result = Image_Color2::average($red, $blue); $this->assertType('Image_Color2', $result); $this->assertEquals('#800080', $result->getHex()); $this->assertEquals('purple', $result->convertTo('named')->getString()); } function testAverage_Rgba() { $red = new Image_Color2(array(255,0,0,255)); $blue = new Image_Color2(array(0,0,255,0)); $result = Image_Color2::average($red, $blue); $this->assertType('Image_Color2', $result); $this->assertEquals(array(128,0,128,128,'type'=>'rgb'), $result->getArray()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/AllTests.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/AllTests.php +++ pear/Image_Color2/Tests/Model/AllTests.php <?php /** * Runner for all ColorModel tests. * * @version $Id: AllTests.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ if (!defined('PHPUnit2_MAIN_METHOD')) { define('PHPUnit2_MAIN_METHOD', 'Image_Color2_Tests_Model_AllTests::main'); } require_once 'PHPUnit2/Framework/TestSuite.php'; require_once 'PHPUnit2/TextUI/TestRunner.php'; require_once dirname(__FILE__) . '/Cmyk.php'; require_once dirname(__FILE__) . '/Grayscale.php'; require_once dirname(__FILE__) . '/Hex.php'; require_once dirname(__FILE__) . '/Hsl.php'; require_once dirname(__FILE__) . '/Hsv.php'; require_once dirname(__FILE__) . '/Named.php'; require_once dirname(__FILE__) . '/WebsafeHex.php'; class Image_Color2_Tests_Model_AllTests { public static function main() { PHPUnit2_TextUI_TestRunner::run(self::suite()); } public static function suite() { $suite = new PHPUnit2_Framework_TestSuite('ColorModel Tests'); $suite->addTestSuite('Image_Color2_Tests_Model_Cmyk'); $suite->addTestSuite('Image_Color2_Tests_Model_Grayscale'); $suite->addTestSuite('Image_Color2_Tests_Model_Hex'); $suite->addTestSuite('Image_Color2_Tests_Model_Hsl'); $suite->addTestSuite('Image_Color2_Tests_Model_Hsv'); $suite->addTestSuite('Image_Color2_Tests_Model_Named'); $suite->addTestSuite('Image_Color2_Tests_Model_WebsafeHex'); return $suite; } } if (PHPUnit2_MAIN_METHOD == 'Image_Color2_Tests_Model_AllTests::main') { Image_Color2_Tests_Model_AllTests::main(); } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Cmyk.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Cmyk.php +++ pear/Image_Color2/Tests/Model/Cmyk.php <?php /** * Image_Color2 Tests * * @version $Id: Cmyk.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 * @link http://www.december.com/html/spec/colorsafecodes.html */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Cmyk.php'; class Image_Color2_Tests_Model_Cmyk extends PHPUnit2_Framework_TestCase { function testFromRgb_CCFF00() { $expected = '20%, 0%, 100%, 0%'; $model = Image_Color2_Model_Cmyk::fromRgb(array(204, 255, 0)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromRgb_9900CC() { $expected = '25%, 100%, 0%, 20%'; $model = Image_Color2_Model_Cmyk::fromRgb(array(153, 0, 204)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromRgb_FF9933() { $expected = '0%, 40%, 80%, 0%'; $model = Image_Color2_Model_Cmyk::fromRgb(array(255, 153, 51)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromArray_Color() { $expected = array(0.99, 0.10, 0.25, 0.50, 'type'=>'cmyk'); $model = Image_Color2_Model_Cmyk::fromArray(array(.99, .1, .25, .5)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getArray()); } function testFromArray_Black() { $expected = array(.0, .0, .0, 1.0, 'type'=>'cmyk'); $model = Image_Color2_Model_Cmyk::fromArray(array(.99, .1, .25, 1)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getArray()); } function testFromString_Percents_Black() { $expected = '0%, 0%, 0%, 100%'; $model = Image_Color2_Model_Cmyk::fromString('0% 33% 80% 100%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromString_Percent_NotBlack() { $expected = '100%, 33%, 80%, 0%'; $model = Image_Color2_Model_Cmyk::fromString($expected); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromString_Floats_Black() { $expected = '0%, 0%, 0%, 100%'; $model = Image_Color2_Model_Cmyk::fromString('0% 33% 80% 100%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testFromString_Floats_NotBlack() { $expected = '100%, 33%, 80%, 0%'; $model = Image_Color2_Model_Cmyk::fromString('1 .33 .8 0'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Cmyk, $model); $this->assertEquals($expected, $model->getString()); } function testGetRgb_Black() { $expected = array(0, 0, 0, 'type'=>'rgb'); $model = Image_Color2_Model_Cmyk::fromArray(array(0, 0.33, 0.80, 1)); $this->assertEquals($expected, $model->getRgb()); } function testGetRgb_FF3366() { $expected = array(255, 51, 102, 'type'=>'rgb'); $model = Image_Color2_Model_Cmyk::fromArray(array(0, 0.80, 0.60, 0)); $this->assertEquals($expected, $model->getRgb()); } function testGetRgb_3366CC() { $expected = array(51, 102, 204, 'type'=>'rgb'); $model = Image_Color2_Model_Cmyk::fromArray(array(0.75, 0.50, 0, 0.20)); $this->assertEquals($expected, $model->getRgb()); } function testGetArray() { $expected = array(.1, .2, .8, .9, 'type'=>'cmyk'); $model = Image_Color2_Model_Cmyk::fromArray($expected); $this->assertEquals($expected, $model->getArray()); } function testGetString_Black() { $expected = '0%, 0%, 0%, 100%'; $model = Image_Color2_Model_Cmyk::fromArray(array(0, 0.33, 0.8, 1)); $this->assertEquals($expected, $model->getString()); } function testGetString_NotBlack() { $expected = '100%, 33%, 80%, 0%'; $model = Image_Color2_Model_Cmyk::fromArray(array(1, 0.33, 0.8, 0)); $this->assertEquals($expected, $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Grayscale.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Grayscale.php +++ pear/Image_Color2/Tests/Model/Grayscale.php <?php /** * Image_Color2 Tests * * @version $Id: Grayscale.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Grayscale.php'; class Image_Color2_Tests_Model_Grayscale extends PHPUnit2_Framework_TestCase { function testFromRgb_Black() { $model = Image_Color2_Model_Grayscale::fromRgb(array(0, 0, 0)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('0%', $model->getString()); } function testFromRgb_Color() { $model = Image_Color2_Model_Grayscale::fromRgb(array(171, 205, 239)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('77.86%', $model->getString()); } function testFromRgb_Gray() { $model = Image_Color2_Model_Grayscale::fromRgb(array(128, 128, 128)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('50.2%', $model->getString()); } function testFromArray_Float() { $model = Image_Color2_Model_Grayscale::fromArray(array(0.1235)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('12.35%', $model->getString()); } function testFromString_Float_TooSmall() { $model = Image_Color2_Model_Grayscale::fromString('-1'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('0%', $model->getString()); } function testFromString_Float_TooBig() { $model = Image_Color2_Model_Grayscale::fromString('1.1'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('100%', $model->getString()); } function testFromString_Float_White() { $model = Image_Color2_Model_Grayscale::fromString('1'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('100%', $model->getString()); } function testFromString_Float_75Gray() { $model = Image_Color2_Model_Grayscale::fromString('0.75'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('75%', $model->getString()); } function testFromString_Float_Black() { $model = Image_Color2_Model_Grayscale::fromString('.0'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('0%', $model->getString()); } function testFromString_Percent_White() { $model = Image_Color2_Model_Grayscale::fromString('101%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('100%', $model->getString()); } function testFromString_Percent_75Gray() { $model = Image_Color2_Model_Grayscale::fromString('75%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('75%', $model->getString()); } function testFromString_Percent_Black() { $model = Image_Color2_Model_Grayscale::fromString('-1%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Grayscale, $model); $this->assertEquals('0%', $model->getString()); } function testGetRgb() { $model = Image_Color2_Model_Grayscale::fromString('50%'); $this->assertEquals(array(128, 128, 128,'type'=>'rgb'), $model->getRgb()); } function testGetArray_FromString() { $model = Image_Color2_Model_Grayscale::fromString('99.99%'); $this->assertEquals(array(0.9999,'type'=>'Grayscale'), $model->getArray()); } function testGetArray_FromFloat() { $model = Image_Color2_Model_Grayscale::fromArray(array(3/7)); $this->assertEquals(array(round(3/7, 8),'type'=>'Grayscale'), $model->getArray()); } function testGetString() { $model = Image_Color2_Model_Grayscale::fromString('12.3456%'); $this->assertEquals('12.35%', $model->getString()); } function testGetString_FromFloat() { $model = Image_Color2_Model_Grayscale::fromArray(array(2/3)); $this->assertEquals('66.67%', $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Hex.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Hex.php +++ pear/Image_Color2/Tests/Model/Hex.php <?php /** * Image_Color2 Tests * * @version $Id: Hex.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Hex.php'; class Image_Color2_Tests_Model_Hex extends PHPUnit2_Framework_TestCase { function testFromRgb() { $model = Image_Color2_Model_Hex::fromRgb(array(171, 205, 239)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hex, $model); $this->assertEquals('#abcdef', $model->getString()); } function testFromArray() { $model = Image_Color2_Model_Hex::fromArray(array(0xAB,0xcd,0xEF)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hex, $model); $this->assertEquals('#abcdef', $model->getString()); } function testFromString() { $model = Image_Color2_Model_Hex::fromString('#abCDef'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hex, $model); $this->assertEquals('#abcdef', $model->getString()); } function testFromString_Shortform() { $model = Image_Color2_Model_Hex::fromString('#abc'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hex, $model); $this->assertEquals('#aabbcc', $model->getString()); } function testFromString_Black() { $model = Image_Color2_Model_Hex::fromString('#000000'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hex, $model); $this->assertEquals('#000000', $model->getString()); } function testGetRgb() { $model = Image_Color2_Model_Hex::fromString('#abcdef'); $this->assertEquals(array(171, 205, 239,'type'=>'rgb'), $model->getRgb()); } function testGetArray() { $model = Image_Color2_Model_Hex::fromString('#abcdef'); $this->assertEquals(array(0xab, 0xcd, 0xef,'type'=>'rgb'), $model->getArray()); } function testGetString() { $model = Image_Color2_Model_Hex::fromString('#abcdef'); $this->assertEquals('#abcdef', $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Hsl.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Hsl.php +++ pear/Image_Color2/Tests/Model/Hsl.php <?php /** * Image_Color2 Tests * * @version $Id: Hsl.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 * @link http://www.december.com/html/spec/colorsafecodes.html */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Hsl.php'; class Image_Color2_Tests_Model_Hsl extends PHPUnit2_Framework_TestCase { function testFromRgb() { $model = Image_Color2_Model_Hsl::fromRgb(array(128, 255, 128)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsl, $model); $expected = '120, 50%, 75%'; $this->assertEquals($expected, $model->getString()); } function testFromArray() { $model = Image_Color2_Model_Hsl::fromArray(array(240, .50, .25)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsl, $model); $expected = array(240, .50, .25, 'type'=>'hsl'); $this->assertEquals($expected, $model->getArray()); } function testFromString_Black() { $expected = '0, 0%, 0%'; $model = Image_Color2_Model_Hsl::fromString($expected); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsl, $model); $this->assertEquals($expected, $model->getString()); } function testFromString_Colored() { $expected = '210, 20%, 90%'; $model = Image_Color2_Model_Hsl::fromString($expected); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsl, $model); $this->assertEquals($expected, $model->getString()); } function testGetRgb_Black() { $model = Image_Color2_Model_Hsl::fromArray(array(0, .5, 0)); $expected = array(0, 0, 0, 'type'=>'rgb'); $this->assertEquals($expected, $model->getRgb()); } function testGetRgb_Red() { $model = Image_Color2_Model_Hsl::fromArray(array(0, 1, .5)); $expected = array(255, 0, 0, 'type'=>'rgb'); $this->assertEquals($expected, $model->getRgb()); } function testGetRgb_Blue() { $model = Image_Color2_Model_Hsl::fromArray(array(240, 1, .5)); $expected = array(0, 0, 255, 'type'=>'rgb'); $this->assertEquals($expected, $model->getRgb()); } function testGetRgb_Greenish() { $model = Image_Color2_Model_Hsl::fromArray(array(120, .79, .52)); $expected = array(36, 229, 36, 'type'=>'rgb'); $this->assertEquals($expected, $model->getRgb()); } function testGetArray() { $expected = array(120, 0.50, 0.75, 'type'=>'hsl'); $model = Image_Color2_Model_Hsl::fromArray($expected); $this->assertEquals($expected, $model->getArray()); } function testGetString() { $model = Image_Color2_Model_Hsl::fromArray(array(210, 0.28, 0.94)); $expected = '210, 28%, 94%'; $this->assertEquals($expected, $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Hsv.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Hsv.php +++ pear/Image_Color2/Tests/Model/Hsv.php <?php /** * Image_Color2 Tests * * @version $Id: Hsv.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Hsv.php'; class Image_Color2_Tests_Model_Hsv extends PHPUnit2_Framework_TestCase { function testFromRgb_ProperType() { $model = Image_Color2_Model_Hsv::fromRgb(array(171, 205, 239)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsv, $model); } function testFromRgb_Blueish() { $model = Image_Color2_Model_Hsv::fromRgb(array(171, 205, 239)); $expected = '210, 28%, 94%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromRgb_Yellowish() { $model = Image_Color2_Model_Hsv::fromRgb(array(192, 175, 96)); $expected = '49, 50%, 75%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromRgb_Black() { $model = Image_Color2_Model_Hsv::fromRgb(array(0, 0, 0)); $expected = '0, 0%, 0%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromRgb_Gray() { $model = Image_Color2_Model_Hsv::fromRgb(array(128, 128, 128)); $expected = '0, 0%, 50%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromRgb_White() { $model = Image_Color2_Model_Hsv::fromRgb(array(255, 255, 255)); $expected = '0, 0%, 100%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromArray() { $model = Image_Color2_Model_Hsv::fromArray(array(210, 0.28, 0.94)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsv, $model); $this->assertEquals('210, 28%, 94%', $model->getString()); } function testFromArray_Gray() { $model = Image_Color2_Model_Hsv::fromArray(array(0, 0, 0.5)); $expected = '0, 0%, 50%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromArray_White() { $model = Image_Color2_Model_Hsv::fromArray(array(0, 0, 1)); $expected = '0, 0%, 100%'; $actual = $model->getString(); $this->assertEquals($expected, $actual); } function testFromString_Percents() { $model = Image_Color2_Model_Hsv::fromString('210, 2%, 9%'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsv, $model); $this->assertEquals('210, 2%, 9%', $model->getString()); } function testFromString_Floats() { $model = Image_Color2_Model_Hsv::fromString('210, .02, .09'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Hsv, $model); $this->assertEquals('210, 2%, 9%', $model->getString()); } function testGetRgb_Blueish() { $model = Image_Color2_Model_Hsv::fromArray(array(210, 0.28, 0.94)); $expected = array(173, 206, 240, 'type'=>'rgb'); $actual = $model->getRgb(); $this->assertEquals($expected, $actual); } function testGetRgb_Black() { $model = Image_Color2_Model_Hsv::fromArray(array(0, 0, 0)); $expected = array(0, 0, 0, 'type'=>'rgb'); $actual = $model->getRgb(); $this->assertEquals($expected, $actual); } function testGetRgb_Gray() { $model = Image_Color2_Model_Hsv::fromArray(array(0, 0, 0.5)); $expected = array(128, 128, 128, 'type'=>'rgb'); $actual = $model->getRgb(); $this->assertEquals($expected, $actual); } function testGetRgb_White() { $model = Image_Color2_Model_Hsv::fromArray(array(0, 0, 1)); $expected = array(255, 255, 255, 'type'=>'rgb'); $actual = $model->getRgb(); $this->assertEquals($expected, $actual); } function testGetArray() { $model = Image_Color2_Model_Hsv::fromArray(array(210, 0.28, 0.94)); $this->assertEquals(array(210, 0.28, 0.94, 'type'=>'hsv'), $model->getArray()); } function testGetString() { $model = Image_Color2_Model_Hsv::fromArray(array(210, 0.28, 0.94)); $this->assertEquals('210, 28%, 94%', $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/Named.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/Named.php +++ pear/Image_Color2/Tests/Model/Named.php <?php /** * Image_Color2 Tests * * @version $Id: Named.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/Named.php'; class Image_Color2_Tests_Model_Named extends PHPUnit2_Framework_TestCase { function testFromRgb() { $model = Image_Color2_Model_Named::fromRgb(array(255,255,255)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Named, $model); $this->assertEquals('white', $model->getString()); } function testFromArray() { $model = Image_Color2_Model_Named::fromArray(array('black', 'type'=>'named')); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Named, $model); $this->assertEquals('black', $model->getString()); } function testFromArray_Unknown() { try { $model = Image_Color2_Model_Named::fromArray(array('madeupcolor')); } catch (PEAR_Exception $ex) { return; } $this->fail('an exception should have been thrown.'); } function testFromString() { $model = Image_Color2_Model_Named::fromString('black'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Named, $model); $this->assertEquals('black', $model->getString()); } function testFromString_UppercaseWithSpaces() { $model = Image_Color2_Model_Named::fromString('Light Steel BLUE'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_Named, $model); $this->assertEquals('lightsteelblue', $model->getString()); } function testFromString_Unknown() { try { $model = Image_Color2_Model_Named::fromString('madeupcolor'); } catch (PEAR_Exception $ex) { return; } $this->fail('an exception should have been thrown.'); } function testGetRgb() { $model = Image_Color2_Model_Named::fromString('white'); $this->assertEquals(array(255,255,255,'type'=>'rgb'), $model->getRgb()); } function testGetArray() { $model = Image_Color2_Model_Named::fromString('black'); $this->assertEquals(array('black', 'type'=>'named'), $model->getArray()); } function testGetString() { $model = Image_Color2_Model_Named::fromString('black'); $this->assertEquals('black', $model->getString()); } } ?> http://cvs.php.net/co.php/pear/Image_Color2/Tests/Model/WebsafeHex.php?r=1.1&p=1 Index: pear/Image_Color2/Tests/Model/WebsafeHex.php +++ pear/Image_Color2/Tests/Model/WebsafeHex.php <?php /** * Image_Color2 Tests * * @version $Id: WebsafeHex.php,v 1.1 2005/10/14 08:17:39 drewish Exp $ * @copyright 2005 */ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'Image/Color2/Model/WebsafeHex.php'; class Image_Color2_Tests_Model_WebsafeHex extends PHPUnit2_Framework_TestCase { function testFromRgb() { $model = Image_Color2_Model_WebsafeHex::fromRgb(array(171, 205, 239)); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_WebsafeHex, $model); $this->assertEquals('#99ccff', $model->getString()); } function testFromArray() { $model = Image_Color2_Model_WebsafeHex::fromArray(array('AB','cd','EF')); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_WebsafeHex, $model); $this->assertEquals('#99ccff', $model->getString()); } function testFromString() { $model = Image_Color2_Model_WebsafeHex::fromString('#abCDef'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_WebsafeHex, $model); $this->assertEquals('#99ccff', $model->getString()); } function testFromString_Black() { $model = Image_Color2_Model_WebsafeHex::fromString('#000000'); $this->assertTrue($model instanceof Image_Color2_Model); $this->assertType(Image_Color2_Model_WebsafeHex, $model); $this->assertEquals('#000000', $model->getString()); } function testRound_00() { $this->assertEquals(0x00, Image_Color2_Model_WebsafeHex::round(-1)); $this->assertEquals(0x00, Image_Color2_Model_WebsafeHex::round(0)); $this->assertEquals(0x00, Image_Color2_Model_WebsafeHex::round(25)); } function testRound_33() { $this->assertEquals(0x33, Image_Color2_Model_WebsafeHex::round(26)); $this->assertEquals(0x33, Image_Color2_Model_WebsafeHex::round(76)); } function testRound_66() { $this->assertEquals(0x66, Image_Color2_Model_WebsafeHex::round(77)); $this->assertEquals(0x66, Image_Color2_Model_WebsafeHex::round(127)); } function testRound_99() { $this->assertEquals(0x99, Image_Color2_Model_WebsafeHex::round(128)); $this->assertEquals(0x99, Image_Color2_Model_WebsafeHex::round(178)); } function testRound_cc() { $this->assertEquals(0xcc, Image_Color2_Model_WebsafeHex::round(179)); $this->assertEquals(0xcc, Image_Color2_Model_WebsafeHex::round(229)); } function testRound_ff() { $this->assertEquals(0xff, Image_Color2_Model_WebsafeHex::round(230)); $this->assertEquals(0xff, Image_Color2_Model_WebsafeHex::round(255)); $this->assertEquals(0xFF, Image_Color2_Model_WebsafeHex::round(257)); } function testGetRgb() { $model = Image_Color2_Model_WebsafeHex::fromString('#abcdef'); $this->assertEquals(array(153, 204, 255,'type'=>'rgb'), $model->getRgb()); } function testGetArray() { $model = Image_Color2_Model_WebsafeHex::fromString('#abcdef'); $this->assertEquals(array(0x99, 0xcc, 0xff,'type'=>'rgb'), $model->getArray()); } function testGetString() { $model = Image_Color2_Model_WebsafeHex::fromString('#abcdef'); $this->assertEquals('#99ccff', $model->getString()); } } ?>
« previous php.pear.cvs (#35506) next »