Image_Color2
[ class tree: Image_Color2 ] [ index: Image_Color2 ] [ all elements ]

Class: Image_Color2

Source Location: /Image_Color2-0.1.5/Image/Color2.php

Class Overview


Image_Color2 is a PHP5 package to convert between RGB and various other color models.


Author(s):

Version:

  • Release: 0.1.2

Copyright:

  • 2005 Andrew Mortin

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 90]
Image_Color2 is a PHP5 package to convert between RGB and various other color models.

Here it is in action:

  1.  <?php
  2.  require_once 'Image/Color2.php';
  3.  
  4.  \/\/ load a named string and view it as a hex string
  5.  $red = new Image_Color2('red');
  6.  print $red->getHex("\n";     # '#ff0000'
  7.  
  8.   \/\/ load a hex string and view it as an RGB array
  9.  $blue = new Image_Color2('#0000ff');
  10.  var_dump($blue->getRgb());       # array(0, 0, 255, 'type' => 'rgb')
  11.  
  12.  \/\/ find the average of red and blue (i.e. mix them)...
  13.  $avg Image_Color2::average($red$blue);
  14.  \/\/ ...then convert it to named color
  15.  print $avg->convertTo('named')->getString("\n";  # 'purple'
  16.  
  17.  \/\/ convert blue from RGB to HSV...
  18.  $hsv $blue->convertTo('hsv');
  19.  \/\/ ...then display it as an HSV string and array
  20.  print $hsv->getString("\n";  # '240 100% 100%'
  21.  print_r($hsv->getArray());       # array(240, 100, 100, 'type' => 'hsv')
  22.  ?>

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.

  • Author: andrew morton <drewish@katherinehouse.com>
  • Version: Release: 0.1.2
  • Copyright: 2005 Andrew Mortin
  • See: Image_Color
  • Link: http://pear.php.net/package/Image_Color2
  • 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.
  • License: GNU Lesser General Public License, Version 2.1


[ Top ]


Class Variables

$model =  null

[line 104]

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.

Type:   Image_Color2_Model


[ Top ]

$rgb =

[line 97]

RGB value of the color. After it's assigned by the constructor it should never be null.

Type:   array


[ Top ]



Method Detail

__construct (Constructor)   [line 133]

Image_Color2 __construct( array|string|Image_Color2_Model $src)

Construct a color from a string, array, or an instance of Image_Color2_Model.

  1.  \/\/ from a named string
  2.  $red = new Image_Color2('red');
  3.  print $red->getHex();    \/\/ '#ff0000'
  4.  
  5.  \/\/ from a hex string
  6.  $blue = new Image_Color2('#0000ff');
  7.  print $blue->getHex();   \/\/ '#0000ff'
  8.  
  9.  \/\/ from an array
  10.  $black = new Image_Color2(array(0,0,0));
  11.  print $black->getHex();  \/\/ '#000000'

  • Throws: PEAR_Exception if the color cannot be loaded.
  • Access: public
  • Uses: Image_Color2::createModelReflectionMethod() - If the color is non-RGB the function is used to construct an Image_Color2_Model for conversion.

Parameters:

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.

[ Top ]

average   [line 219]

Image_Color2 average( Image_Color2 $left, Image_Color2 $right)

Return the average of the RGB value of two Image_Color2 objects. If both objects have an alpha channel it will be averaged too.

  1.  $red = new Image_Color2('red');
  2.  $blue = new Image_Color2('blue');
  3.  $color Image_Color2::average($red$blue);
  4.  print $color->convertTo('named')->getString(); \/\/ 'purple'

  • Access: public

Parameters:

Image_Color2   $left     Left color
Image_Color2   $right     Right color

[ Top ]

convertTo   [line 258]

Image_Color2 convertTo( string $type)

Return a copy of this color converted to another color model.

  1.  $blue = new Image_Color2('#0000ff');
  2.  $hsv $blue->convertTo('hsv');
  3.  print $hsv->getString(); \/\/ '240 100% 100%'

  • Throws: PEAR_Exception if the desired color model cannot be found or it cannot convert the color.
  • Access: public
  • Uses: Image_Color2::createModelReflectionMethod() - The function is used to construct an Image_Color2_Model that is passed back to the constructor.

Parameters:

string   $type     Name of a color model. If this variable is foo then a class named Image_Color2_Model_Foo is required.

[ Top ]

createModelReflectionMethod   [line 183]

ReflectionMethod createModelReflectionMethod( string $type, string $methodName)

Return a ReflectionMethod of a Image_Color2_Model implementation found in the Image/Color2/Model directory.
  • Throws: PEAR_Exception if the class cannot be loaded, or it does not implement the Image_Color2_Model interface.
  • Access: protected
  • Usedby: Image_Color2::convertTo() - The function is used to construct an Image_Color2_Model that is passed back to the constructor.
  • Usedby: Image_Color2::__construct() - If the color is non-RGB the function is used to construct an Image_Color2_Model for conversion.
  • Uses: Image_Color2_Model - As the interface for color conversion.

Parameters:

string   $type     Name of a ColorModel implementation (i.e. for Image_Color2_Model_Hsv this would be 'hsv').
string   $methodName     Name of a static factory method on the ColorModel interface ('fromArray', 'fromString', or 'fromRgb').

[ Top ]

getArray   [line 329]

mixed getArray( [mixed $index = null])

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.

  1.  $color = new Image_Color2(array(0,128,255));
  2.  print_r($color->getArray());     \/\/ array(0128255'type' => 'rgb')
  3.  
  4.  \/\/ While PHP barfs if you write:
  5.  print $color->getArray()[0];     \/\/ NOT VALID
  6.  \/\/ You can pass in an optional index parameter for the same effect:
  7.  print $color->getArray(2);       \/\/ 255
  8.  print $color->getArray('type');  \/\/ 'rgb'

  • Return: An array if no index is provided. Otherwise, provided the index is valid, a member of the array.
  • Access: public
  • Uses: Image_Color2_Model::getArray() - For the color conversion if it wasn't originally RGB.
  • Uses: Image_Color2::$rgb - If the color was specified as RGB.
  • Uses: Image_Color2::$model - If the color wasn't specified as RGB.

Parameters:

mixed   $index     An optional index value to select an element of the array. A null value returns the entire array.

[ Top ]

getHex   [line 364]

string getHex( )

Return a hex string representation of the color.

  1.  $color = new Image_Color2(array(171205239));
  2.  print $color->getHex(); \/\/ '#abcdef'

To obtain a websafe color, convert it using the websafe hex color model:

  1.  $color = new Image_Color2('#abcdef');
  2.  print $color->convertTo('WebsafeHex')->getString(); \/\/ '#99ccff'


[ Top ]

getRgb   [line 293]

mixed getRgb( [mixed $index = null])

Return the color as a PEAR style RGB array.

The optional 'type' => 'rgb' element should always be included.

  1.  $color = new Image_Color2(array(0,128,255));
  2.  print_r($color->getRgb()); \/\/ array(0128255'type' => 'rgb')
  3.  
  4.  \/\/ While PHP barfs if you write:
  5.  print $color->getRgb()[0];     \/\/ NOT VALID
  6.  \/\/ You can pass in an optional index parameter for the same effect:
  7.  print $color->getRgb(2);       \/\/ 255
  8.  print $color->getRgb('type');  \/\/ 'rgb'

  • Return: An array if no index parameter is provided. Otherwise, provided the index is valid, a member of the array.
  • Access: public
  • Uses: Image_Color2::$rgb - Returns a copy of the array.

Parameters:

mixed   $index     An optional index value to select an element of the array. A null value returns the entire array.

[ Top ]

getString   [line 390]

string 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.

  1.  $red = new Image_Color2(array(255,0,0));
  2.  print $red->getString();     \/\/ '#ff0000'
  3.  
  4.  $orange = new Image_Color2('orange');
  5.  print $orange->getString();  \/\/ 'orange'
  6.  
  7.  $hsl $orange->convertTo('hsl');
  8.  print $hsl->getString();     \/\/ '38 100% 50%'


[ Top ]


Documentation generated on Mon, 20 Sep 2010 10:30:02 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.