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()); }
/** * 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.'); } }
function testGetString() { $model = Image_Color2_Model_Named::fromString('black'); $this->assertEquals('black', $model->getString()); }