/lib/ezc/Graph/src/colors/color.php

https://github.com/Proudio-Interactive/phpUnderControl · PHP · 290 lines · 148 code · 25 blank · 117 comment · 14 complexity · b5681236ec14550015f04c80242a854c MD5 · raw file

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