PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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