| [ Index ] |
PHP Cross Reference of MantisBT |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * File containing the ezcGraphColor class 4 * 5 * @package Graph 6 * @version 1.5 7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. 8 * @license http://ez.no/licenses/new_bsd New BSD License 9 */ 10 11 /** 12 * ezcGraphColor 13 * 14 * Struct for representing colors in ezcGraph. A color is defined using the 15 * common RGBA model with integer values between 0 and 255. An alpha value 16 * of zero means full opacity, while 255 means full transparency. 17 * 18 * @property integer $red 19 * Red RGBA value of color. 20 * @property integer $green 21 * Green RGBA value of color. 22 * @property integer $blue 23 * Blue RGBA value of color. 24 * @property integer $alpha 25 * Alpha RGBA value of color. 26 * 27 * @version 1.5 28 * @package Graph 29 */ 30 class ezcGraphColor extends ezcBaseOptions 31 { 32 /** 33 * Constructor 34 * 35 * @param array $options Default option array 36 * @return void 37 * @ignore 38 */ 39 public function __construct( array $options = array() ) 40 { 41 $this->properties['red'] = 0; 42 $this->properties['green'] = 0; 43 $this->properties['blue'] = 0; 44 $this->properties['alpha'] = 0; 45 46 parent::__construct( $options ); 47 } 48 49 /** 50 * __set 51 * 52 * @param mixed $propertyName 53 * @param mixed $propertyValue 54 * @throws ezcBaseValueException 55 * If a submitted parameter was out of range or type. 56 * @throws ezcBasePropertyNotFoundException 57 * If a the value for the property options is not an instance of 58 * @return void 59 * @ignore 60 */ 61 public function __set( $propertyName, $propertyValue ) 62 { 63 switch ( $propertyName ) 64 { 65 case 'red': 66 case 'green': 67 case 'blue': 68 case 'alpha': 69 if ( !is_numeric( $propertyValue ) || 70 ( $propertyValue < 0 ) || 71 ( $propertyValue > 255 ) ) 72 { 73 throw new ezcBaseValueException( $propertyName, $propertyValue, '0 <= int <= 255' ); 74 } 75 76 $this->properties[$propertyName] = (int) $propertyValue; 77 break; 78 default: 79 throw new ezcBasePropertyNotFoundException( $propertyName ); 80 break; 81 } 82 } 83 84 /** 85 * Creates an ezcGraphColor object from a hexadecimal color representation 86 * 87 * @param mixed $string Hexadecimal color representation 88 * @return ezcGraphColor 89 */ 90 static public function fromHex( $string ) 91 { 92 // Remove trailing # 93 if ( $string[0] === '#' ) 94 { 95 $string = substr( $string, 1 ); 96 } 97 98 // Iterate over chunks and convert to integer 99 $color = new ezcGraphColor(); 100 $keys = array( 'red', 'green', 'blue', 'alpha' ); 101 foreach ( str_split( $string, 2) as $nr => $hexValue ) 102 { 103 if ( isset( $keys[$nr] ) ) 104 { 105 $key = $keys[$nr]; 106 $color->$key = hexdec( $hexValue ) % 256; 107 } 108 } 109 110 // Set missing values to zero 111 for ( ++$nr; $nr < count( $keys ); ++$nr ) 112 { 113 $key = $keys[$nr]; 114 $color->$key = 0; 115 } 116 117 return $color; 118 } 119 120 /** 121 * Creates an ezcGraphColor object from an array of integers 122 * 123 * @param array $array Array of integer color values 124 * @return ezcGraphColor 125 */ 126 static public function fromIntegerArray( array $array ) 127 { 128 // Iterate over array elements 129 $color = new ezcGraphColor(); 130 $keys = array( 'red', 'green', 'blue', 'alpha' ); 131 $nr = 0; 132 foreach ( $array as $colorValue ) 133 { 134 if ( isset( $keys[$nr] ) ) 135 { 136 $key = $keys[$nr++]; 137 $color->$key = ( (int) $colorValue ) % 256; 138 } 139 } 140 141 // Set missing values to zero 142 for ( $nr; $nr < count( $keys ); ++$nr ) 143 { 144 $key = $keys[$nr]; 145 $color->$key = 0; 146 } 147 148 return $color; 149 } 150 151 /** 152 * Creates an ezcGraphColor object from an array of floats 153 * 154 * @param array $array Array of float color values 155 * @return ezcGraphColor 156 */ 157 static public function fromFloatArray( array $array ) 158 { 159 // Iterate over array elements 160 $color = new ezcGraphColor(); 161 $keys = array( 'red', 'green', 'blue', 'alpha' ); 162 $nr = 0; 163 foreach ( $array as $colorValue ) 164 { 165 if ( isset( $keys[$nr] ) ) 166 { 167 $key = $keys[$nr++]; 168 $color->$key = ( (float) $colorValue * 255 ) % 256; 169 } 170 } 171 172 // Set missing values to zero 173 for ( $nr; $nr < count( $keys ); ++$nr ) 174 { 175 $key = $keys[$nr]; 176 $color->$key = 0; 177 } 178 179 return $color; 180 } 181 182 /** 183 * Tries to parse provided color value 184 * 185 * This method can be used to create a color struct from arbritrary color 186 * representations. The following values are accepted 187 * 188 * - Hexadecimal color definitions, like known from HTML, CSS and SVG 189 * 190 * Color definitions like #FF0000, with and and without number sign, 191 * where each pair of bytes is interpreted as a color value for the 192 * channels RGB(A). These color values may contain up to 4 values, where 193 * the last value is considered as the alpha channel. 194 * 195 * - Array of integers 196 * 197 * If an array of integers is provided as input teh value in each channel 198 * may be in the span [0 - 255] and is assigned to the color channels 199 * RGB(A). Up to four values are used from the array. 200 * 201 * - Array of floats 202 * 203 * If an array of floats is provided as input teh value in each channel 204 * may be in the span [0 - 1] and is assigned to the color channels 205 * RGB(A). Up to four values are used from the array. 206 * 207 * @param mixed $color Some kind of color definition 208 * @return ezcGraphColor 209 */ 210 static public function create( $color ) 211 { 212 if ( $color instanceof ezcGraphColor ) 213 { 214 return $color; 215 } 216 elseif ( is_string( $color ) ) 217 { 218 return ezcGraphColor::fromHex( $color ); 219 } 220 elseif ( is_array( $color ) ) 221 { 222 $testElement = reset( $color ); 223 if ( is_int( $testElement ) ) 224 { 225 return ezcGraphColor::fromIntegerArray( $color ); 226 } 227 else 228 { 229 return ezcGraphColor::fromFloatArray( $color ); 230 } 231 } 232 else 233 { 234 throw new ezcGraphUnknownColorDefinitionException( $color ); 235 } 236 } 237 238 /** 239 * Returns a copy of the current color made more transparent by the given 240 * factor 241 * 242 * @param mixed $value Percent to make color mor transparent 243 * @return ezcGraphColor New color 244 */ 245 public function transparent( $value ) 246 { 247 $color = clone $this; 248 249 $color->alpha = 255 - (int) round( ( 255 - $this->alpha ) * ( 1 - $value ) ); 250 251 return $color; 252 } 253 254 /** 255 * Inverts and returns a copy of the current color 256 * 257 * @return ezcGraphColor New Color 258 */ 259 public function invert() 260 { 261 $color = new ezcGraphColor(); 262 263 $color->red = 255 - $this->red; 264 $color->green = 255 - $this->green; 265 $color->blue = 255 - $this->blue; 266 $color->alpha = $this->alpha; 267 268 return $color; 269 } 270 271 /** 272 * Returns a copy of the current color darkened by the given factor 273 * 274 * @param float $value Percent to darken the color 275 * @return ezcGraphColor New color 276 */ 277 public function darken( $value ) 278 { 279 $color = clone $this; 280 281 $value = 1 - $value; 282 $color->red = min( 255, max( 0, (int) round( $this->red * $value ) ) ); 283 $color->green = min( 255, max( 0, (int) round( $this->green * $value ) ) ); 284 $color->blue = min( 255, max( 0, (int) round( $this->blue * $value ) ) ); 285 286 return $color; 287 } 288 } 289 290 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jul 28 15:48:31 2011 | Cross-referenced by PHPXref 0.7 |