diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-08-25 01:20:36 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-08-25 01:20:36 +0000 |
commit | 4e50ab6291dc2430ef9ce4cc42c100c021e1a3b0 (patch) | |
tree | b476fac1f2f6caf72c476fa2cb71d496878d911f /src/colors | |
parent | 5298e85976086eeaac3517c359e41f2c2ee444d5 (diff) | |
download | zetacomponents-graph-4e50ab6291dc2430ef9ce4cc42c100c021e1a3b0.zip zetacomponents-graph-4e50ab6291dc2430ef9ce4cc42c100c021e1a3b0.tar.gz |
- Pimped pie charts
Look at Graph/tests/data/compare/ezcGraphRenderer3dTest_testRenderLabeledPieSegmentWithGleamAndShadow.svg
# Caused by a 25h travel from Dortmund to Vancover and commited on an open
# WLAN in Osoyoos in Okanagan Valley, Canada
Diffstat (limited to 'src/colors')
-rw-r--r-- | src/colors/color.php | 244 | ||||
-rw-r--r-- | src/colors/linear_gradient.php | 152 | ||||
-rw-r--r-- | src/colors/radial_gradient.php | 158 |
3 files changed, 554 insertions, 0 deletions
diff --git a/src/colors/color.php b/src/colors/color.php new file mode 100644 index 0000000..9784388 --- /dev/null +++ b/src/colors/color.php @@ -0,0 +1,244 @@ +<?php +/** + * File containing the ezcGraphColor class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * ezcGraphColor + * + * Struct for representing colors in ezcGraph. A color is defined using the + * common RGBA model with integer values between 0 and 255. An alpha value + * of zero means full opacity, while 255 means full transparency. + * + * @property integer $red + * Red RGBA value of color. + * @property integer $green + * Green RGBA value of color. + * @property integer $blue + * Blue RGBA value of color. + * @property integer $alpha + * Alpha RGBA value of color. + * + * @package Graph + */ +class ezcGraphColor extends ezcBaseOptions +{ + /** + * Constructor + * + * @param array $options Default option array + * @return void + * @ignore + */ + public function __construct( array $options = array() ) + { + $this->properties['red'] = 0; + $this->properties['green'] = 0; + $this->properties['blue'] = 0; + $this->properties['alpha'] = 0; + + parent::__construct( $options ); + } + + /** + * __set + * + * @param mixed $propertyName + * @param mixed $propertyValue + * @throws ezcBaseValueException + * If a submitted parameter was out of range or type. + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return void + * @ignore + */ + public function __set( $propertyName, $propertyValue ) + { + switch ( $propertyName ) + { + case 'red': + case 'green': + case 'blue': + case 'alpha': + $this->properties[$propertyName] = max( 0, min( 255, (int) $propertyValue ) ); + break; + default: + throw new ezcGraphNoSuchDataSetException( $propertyName ); + break; + } + } + + /** + * Creates an ezcGraphColor object from a hexadecimal color representation + * + * @param mixed $string Hexadecimal color representation + * @return ezcGraphColor + */ + static public function fromHex( $string ) + { + // Remove trailing # + if ( $string[0] === '#' ) + { + $string = substr( $string, 1 ); + } + + // Iterate over chunks and convert to integer + $color = new ezcGraphColor(); + $keys = array( 'red', 'green', 'blue', 'alpha' ); + foreach ( str_split( $string, 2) as $nr => $hexValue ) + { + if ( isset( $keys[$nr] ) ) + { + $key = $keys[$nr]; + $color->$key = hexdec( $hexValue ) % 256; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; + } + + /** + * Creates an ezcGraphColor object from an array of integers + * + * @param array $array Array of integer color values + * @return ezcGraphColor + */ + static public function fromIntegerArray( array $array ) + { + // Iterate over array elements + $color = new ezcGraphColor(); + $keys = array( 'red', 'green', 'blue', 'alpha' ); + $nr = 0; + foreach ( $array as $colorValue ) + { + if ( isset( $keys[$nr] ) ) + { + $key = $keys[$nr++]; + $color->$key = ( (int) $colorValue ) % 256; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; + } + + /** + * Creates an ezcGraphColor object from an array of floats + * + * @param array $array Array of float color values + * @return ezcGraphColor + */ + static public function fromFloatArray( array $array ) + { + // Iterate over array elements + $color = new ezcGraphColor(); + $keys = array( 'red', 'green', 'blue', 'alpha' ); + $nr = 0; + foreach ( $array as $colorValue ) + { + if ( isset( $keys[$nr] ) ) + { + $key = $keys[$nr++]; + $color->$key = ( (float) $colorValue * 255 ) % 256; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; + } + + /** + * Tries to detect type of color color definition and returns an + * ezcGraphColor object + * + * @param mixed $color Some kind of color definition + * @return ezcGraphColor + */ + static public function create( $color ) + { + if ( $color instanceof ezcGraphColor ) + { + return $color; + } + elseif ( is_string( $color ) ) + { + return ezcGraphColor::fromHex( $color ); + } + elseif ( is_array( $color ) ) + { + $testElement = reset( $color ); + if ( is_int( $testElement ) ) + { + return ezcGraphColor::fromIntegerArray( $color ); + } + else + { + return ezcGraphColor::fromFloatArray( $color ); + } + } + else + { + throw new ezcGraphUnknownColorDefinitionException( $color ); + } + } + + /** + * Returns a copy of the current color made more transparent by the given + * factor + * + * @param mixed $value Percent to make color mor transparent + * @return ezcGraphColor New color + */ + public function transparent( $value ) + { + $color = clone $this; + + $color->alpha = 255 - (int) round( ( 255 - $this->alpha ) * ( 1 - $value ) ); + + return $color; + } + + /** + * Returns a copy of the current color darkened by the given factor + * + * @param float $value Percent to darken the color + * @return ezcGraphColor New color + */ + public function darken( $value ) + { + $color = clone $this; + + $value = 1 - $value; + $color->red = (int) round( $this->red * $value ); + $color->green = (int) round( $this->green * $value ); + $color->blue = (int) round( $this->blue * $value ); + + return $color; + } +} + +?> diff --git a/src/colors/linear_gradient.php b/src/colors/linear_gradient.php new file mode 100644 index 0000000..3bbde96 --- /dev/null +++ b/src/colors/linear_gradient.php @@ -0,0 +1,152 @@ +<?php +/** + * File containing the ezcGraphLinearGradient class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class representing linear gradient fills. For drivers which cannot draw + * gradients it falls back to a native ezcGraphColor + * + * @property ezcGraphCoordinate $startPoint + * Starting point of the gradient. + * @property ezcGraphCoordinate $endPoint + * Ending point of the gradient. + * @property ezcGraphColor $startColor + * Starting color of the gradient. + * @property ezcGraphColor $endColor + * Ending color of the gradient. + * + * @package Graph + */ +class ezcGraphLinearGradient extends ezcGraphColor +{ + /** + * Constructor + * + * @param ezcGraphCoordinate $startPoint + * @param ezcGraphCoordinate $endPoint + * @param ezcGraphColor $startColor + * @param ezcGraphColor $endColor + * @return void + */ + public function __construct( ezcGraphCoordinate $startPoint, ezcGraphCoordinate $endPoint, ezcGraphColor $startColor, ezcGraphColor $endColor ) + { + $this->properties['startColor'] = $startColor; + $this->properties['endColor'] = $endColor; + $this->properties['startPoint'] = $startPoint; + $this->properties['endPoint'] = $endPoint; + } + + /** + * __set + * + * @param mixed $propertyName + * @param mixed $propertyValue + * @throws ezcBaseValueException + * If a submitted parameter was out of range or type. + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return void + * @ignore + */ + public function __set( $propertyName, $propertyValue ) + { + switch ( $propertyName ) + { + case 'startPoint': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['startPoint'] = $propertyValue; + } + break; + case 'endPoint': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['endPoint'] = $propertyValue; + } + break; + case 'startColor': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['startColor'] = $propertyValue; + } + break; + case 'endColor': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['endColor'] = $propertyValue; + } + break; + } + } + + /** + * __get + * + * @param mixed $propertyName + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return mixed + * @ignore + */ + public function __get( $propertyName ) + { + switch ( $propertyName ) + { + case 'red': + case 'green': + case 'blue': + case 'alpha': + // Fallback to native color + return $this->properties['startColor']->$propertyName; + default: + if ( isset( $this->properties[$propertyName] ) ) + { + return $this->properties[$propertyName]; + } + else + { + throw new ezcBasePropertyNotFoundException( $propertyName ); + } + } + } + + public function __toString() + { + return sprintf( 'LinearGradient_%d_%d_%d_%d_%02x%02x%02x%02x_%02x%02x%02x%02x', + $this->properties['startPoint']->x, + $this->properties['startPoint']->y, + $this->properties['endPoint']->x, + $this->properties['endPoint']->y, + $this->properties['startColor']->red, + $this->properties['startColor']->green, + $this->properties['startColor']->blue, + $this->properties['startColor']->alpha, + $this->properties['endColor']->red, + $this->properties['endColor']->green, + $this->properties['endColor']->blue, + $this->properties['endColor']->alpha + ); + } +} diff --git a/src/colors/radial_gradient.php b/src/colors/radial_gradient.php new file mode 100644 index 0000000..78b22ca --- /dev/null +++ b/src/colors/radial_gradient.php @@ -0,0 +1,158 @@ +<?php +/** + * File containing the ezcGraphRadialGradient class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class representing linear gradient fills. For drivers which cannot draw + * gradients it falls back to a native ezcGraphColor + * + * @property ezcGraphCoordinate $center + * Center point of the gradient. + * @property int $width + * Width of ellipse + * @property int $height + * Width of ellipse + * @property int $offset + * Offset for starting color + * @property ezcGraphColor $startColor + * Starting color of the gradient. + * @property ezcGraphColor $endColor + * Ending color of the gradient. + * + * @package Graph + */ +class ezcGraphRadialGradient extends ezcGraphColor +{ + /** + * Constructor + * + * @param ezcGraphCoordinate $startPoint + * @param ezcGraphCoordinate $endPoint + * @param ezcGraphColor $startColor + * @param ezcGraphColor $endColor + * @return void + */ + public function __construct( ezcGraphCoordinate $center, $width, $height, ezcGraphColor $startColor, ezcGraphColor $endColor ) + { + $this->properties['center'] = $center; + $this->properties['width'] = (float) $width; + $this->properties['height'] = (float) $height; + $this->properties['offset'] = 0; + $this->properties['startColor'] = $startColor; + $this->properties['endColor'] = $endColor; + } + + /** + * __set + * + * @param mixed $propertyName + * @param mixed $propertyValue + * @throws ezcBaseValueException + * If a submitted parameter was out of range or type. + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return void + * @ignore + */ + public function __set( $propertyName, $propertyValue ) + { + switch ( $propertyName ) + { + case 'center': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['center'] = $propertyValue; + } + break; + case 'width': + $this->properties['width'] = (float) $propertyValue; + break; + case 'height': + $this->properties['height'] = (float) $propertyValue; + break; + case 'offset': + $this->properties['offset'] = min( 0, max( 1, (float) $propertyValue ) ); + break; + case 'startColor': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['startColor'] = $propertyValue; + } + break; + case 'endColor': + if ( !$propertyValue instanceof ezcGraphCoordinate ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + else + { + $this->properties['endColor'] = $propertyValue; + } + break; + } + } + + /** + * __get + * + * @param mixed $propertyName + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return mixed + * @ignore + */ + public function __get( $propertyName ) + { + switch ( $propertyName ) + { + case 'red': + case 'green': + case 'blue': + case 'alpha': + // Fallback to native color + return $this->properties['startColor']->$propertyName; + default: + if ( isset( $this->properties[$propertyName] ) ) + { + return $this->properties[$propertyName]; + } + else + { + throw new ezcBasePropertyNotFoundException( $propertyName ); + } + } + } + + public function __toString() + { + return sprintf( 'RadialGradient_%d_%d_%d_%d_%.2f_%02x%02x%02x%02x_%02x%02x%02x%02x', + $this->properties['center']->x, + $this->properties['center']->y, + $this->properties['width'], + $this->properties['height'], + $this->properties['offset'], + $this->properties['startColor']->red, + $this->properties['startColor']->green, + $this->properties['startColor']->blue, + $this->properties['startColor']->alpha, + $this->properties['endColor']->red, + $this->properties['endColor']->green, + $this->properties['endColor']->blue, + $this->properties['endColor']->alpha + ); + } +} |