diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-05-09 10:04:46 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-05-09 10:04:46 +0000 |
commit | b1a7160bf0bda45f2a2e818e09446bd53e2162c1 (patch) | |
tree | d1f8493ecdf0c3ff0eaeb100e4a445104deab4bd | |
parent | ee8e254fcaa69679b828a78b7d6666157170224b (diff) | |
download | zetacomponents-graph-b1a7160bf0bda45f2a2e818e09446bd53e2162c1.zip zetacomponents-graph-b1a7160bf0bda45f2a2e818e09446bd53e2162c1.tar.gz |
- Completed color tests
- Implemented factory methods in ezcGraphColor
-rw-r--r-- | src/exceptions/unknown_color_definition.php | 21 | ||||
-rw-r--r-- | src/graph_autoload.php | 13 | ||||
-rw-r--r-- | src/structs/color.php | 92 | ||||
-rw-r--r-- | tests/color_test.php | 142 |
4 files changed, 260 insertions, 8 deletions
diff --git a/src/exceptions/unknown_color_definition.php b/src/exceptions/unknown_color_definition.php new file mode 100644 index 0000000..02001fa --- /dev/null +++ b/src/exceptions/unknown_color_definition.php @@ -0,0 +1,21 @@ +<?php +/** + * File containing the ezcGraphUnknownColorDefinitionException class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * ezcGraphUnknownChartTypeException is the exception which is thrown when the + * factory method tries to return an instance of an unknown chart type + * + * @package Graph + * @version //autogen// + */ +class ezcGraphUnknownColorDefinitionException extends ezcBaseException +{ +} + +?> diff --git a/src/graph_autoload.php b/src/graph_autoload.php index 0213985..be701e5 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -10,12 +10,15 @@ */ return array( - 'ezcGraph' => 'Graph/graph.php', - 'ezcGraphUnknownChartTypeException' => 'Graph/exceptions/unknown_chart_type.php', + 'ezcGraph' => 'Graph/graph.php', + 'ezcGraphUnknownChartTypeException' => 'Graph/exceptions/unknown_chart_type.php', - 'ezcGraphChart' => 'Graph/interfaces/chart.php', - 'ezcGraphPieChart' => 'Graph/charts/pie.php', - 'ezcGraphLineChart' => 'Graph/charts/line.php', + 'ezcGraphChart' => 'Graph/interfaces/chart.php', + 'ezcGraphPieChart' => 'Graph/charts/pie.php', + 'ezcGraphLineChart' => 'Graph/charts/line.php', + + 'ezcGraphColor' => 'Graph/structs/color.php', + 'ezcGraphUnknownColorDefinitionException' => 'Graph/exceptions/unknown_color_definition.php' ); ?> diff --git a/src/structs/color.php b/src/structs/color.php index 2ad24be..6fa5993 100644 --- a/src/structs/color.php +++ b/src/structs/color.php @@ -20,7 +20,7 @@ class ezcGraphColor /** * Throws a BasePropertyNotFound exception. */ - public function __set( $name, $value ) + public function __set( $name, $key ) { throw new ezcBasePropertyNotFoundException( $name ); } @@ -41,6 +41,32 @@ class 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 ) % 255; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; } /** @@ -51,6 +77,27 @@ class 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 ) % 255; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; } /** @@ -61,6 +108,27 @@ class 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 ) % 255; + } + } + + // Set missing values to zero + for ( ++$nr; $nr < count( $keys ); ++$nr ) + { + $key = $keys[$nr]; + $color->$key = 0; + } + + return $color; } /** @@ -72,9 +140,27 @@ class ezcGraphColor */ static public function create( $color ) { + if ( 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 ); + } } } -? - ?> diff --git a/tests/color_test.php b/tests/color_test.php new file mode 100644 index 0000000..90beced --- /dev/null +++ b/tests/color_test.php @@ -0,0 +1,142 @@ +<?php +/** + * ezcGraphColorTest + * + * @package Graph + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Tests for ezcGraph class. + * + * @package ImageAnalysis + * @subpackage Tests + */ +class ezcGraphColorTest extends ezcTestCase +{ + + public static function suite() + { + return new ezcTestSuite( "ezcGraphColorTest" ); + } + + /** + * setUp + * + * @access public + */ + public function setUp() + { + } + + /** + * tearDown + * + * @access public + */ + public function tearDown() + { + } + + public function testFactoryColorFromHex() + { + $color = ezcGraphColor::fromHex( '#05172A' ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryColorFromHexWithAlpha() + { + $color = ezcGraphColor::fromHex( '#05172A40' ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 64, 'Wrong alpha color value' ); + } + + public function testFactoryColorFromIntegerArray() + { + $color = ezcGraphColor::fromIntegerArray( array( 5, 23, 42 ) ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryColorFromFloatArray() + { + $color = ezcGraphColor::fromFloatArray( array( .02, .092, .165 ) ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryColorCreateFromHex() + { + $color = ezcGraphColor::create( '#05172A' ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryColorCreateFromHexWithAlpha() + { + $color = ezcGraphColor::create( '#05172A40' ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 64, 'Wrong alpha color value' ); + } + + public function testFactoryColorCreateFromIntegerArray() + { + $color = ezcGraphColor::create( array( 5, 23, 42 ) ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryColorCreateFromFloatArray() + { + $color = ezcGraphColor::create( array( .02, .092, .165 ) ); + + $this->assertEquals( $color->red, 5, 'Wrong red color value' ); + $this->assertEquals( $color->green, 23, 'Wrong green color value' ); + $this->assertEquals( $color->blue, 42, 'Wrong blue color value' ); + $this->assertEquals( $color->alpha, 0, 'Wrong alpha color value' ); + } + + public function testFactoryUnknownColorDefinition() + { + try + { + $color = ezcGraphColor::create( 1337 ); + } + catch ( ezcGraphUnknownColorDefinitionException $e ) + { + return true; + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + + $this->fail( 'Expected ezcGraphUnknownColorDefinitionException' ); + } +} +?> |