PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/PHPExcel/Style/Color.php

https://github.com/iGroup/PHPExcel
PHP | 441 lines | 241 code | 31 blank | 169 comment | 39 complexity | ff425ede1c95728ab7963389a2e8c17f MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Style
  23. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Style_Color
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Color extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  35. {
  36. /* Colors */
  37. const COLOR_BLACK = 'FF000000';
  38. const COLOR_WHITE = 'FFFFFFFF';
  39. const COLOR_RED = 'FFFF0000';
  40. const COLOR_DARKRED = 'FF800000';
  41. const COLOR_BLUE = 'FF0000FF';
  42. const COLOR_DARKBLUE = 'FF000080';
  43. const COLOR_GREEN = 'FF00FF00';
  44. const COLOR_DARKGREEN = 'FF008000';
  45. const COLOR_YELLOW = 'FFFFFF00';
  46. const COLOR_DARKYELLOW = 'FF808000';
  47. /**
  48. * Indexed colors array
  49. *
  50. * @var array
  51. */
  52. protected static $_indexedColors;
  53. /**
  54. * ARGB - Alpha RGB
  55. *
  56. * @var string
  57. */
  58. protected $_argb = NULL;
  59. /**
  60. * Parent property name
  61. *
  62. * @var string
  63. */
  64. protected $_parentPropertyName;
  65. /**
  66. * Create a new PHPExcel_Style_Color
  67. *
  68. * @param string $pARGB ARGB value for the colour
  69. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  70. * Leave this value at default unless you understand exactly what
  71. * its ramifications are
  72. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  73. * Leave this value at default unless you understand exactly what
  74. * its ramifications are
  75. */
  76. public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor = FALSE, $isConditional = FALSE)
  77. {
  78. // Supervisor?
  79. parent::__construct($isSupervisor);
  80. // Initialise values
  81. if (!$isConditional) {
  82. $this->_argb = $pARGB;
  83. }
  84. }
  85. /**
  86. * Bind parent. Only used for supervisor
  87. *
  88. * @param mixed $parent
  89. * @param string $parentPropertyName
  90. * @return PHPExcel_Style_Color
  91. */
  92. public function bindParent($parent, $parentPropertyName=NULL)
  93. {
  94. $this->_parent = $parent;
  95. $this->_parentPropertyName = $parentPropertyName;
  96. return $this;
  97. }
  98. /**
  99. * Get the shared style component for the currently active cell in currently active sheet.
  100. * Only used for style supervisor
  101. *
  102. * @return PHPExcel_Style_Color
  103. */
  104. public function getSharedComponent()
  105. {
  106. switch ($this->_parentPropertyName) {
  107. case '_endColor':
  108. return $this->_parent->getSharedComponent()->getEndColor(); break;
  109. case '_color':
  110. return $this->_parent->getSharedComponent()->getColor(); break;
  111. case '_startColor':
  112. return $this->_parent->getSharedComponent()->getStartColor(); break;
  113. }
  114. }
  115. /**
  116. * Build style array from subcomponents
  117. *
  118. * @param array $array
  119. * @return array
  120. */
  121. public function getStyleArray($array)
  122. {
  123. switch ($this->_parentPropertyName) {
  124. case '_endColor':
  125. $key = 'endcolor';
  126. break;
  127. case '_color':
  128. $key = 'color';
  129. break;
  130. case '_startColor':
  131. $key = 'startcolor';
  132. break;
  133. }
  134. return $this->_parent->getStyleArray(array($key => $array));
  135. }
  136. /**
  137. * Apply styles from array
  138. *
  139. * <code>
  140. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  141. * </code>
  142. *
  143. * @param array $pStyles Array containing style information
  144. * @throws PHPExcel_Exception
  145. * @return PHPExcel_Style_Color
  146. */
  147. public function applyFromArray($pStyles = NULL) {
  148. if (is_array($pStyles)) {
  149. if ($this->_isSupervisor) {
  150. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  151. } else {
  152. if (array_key_exists('rgb', $pStyles)) {
  153. $this->setRGB($pStyles['rgb']);
  154. }
  155. if (array_key_exists('argb', $pStyles)) {
  156. $this->setARGB($pStyles['argb']);
  157. }
  158. }
  159. } else {
  160. throw new PHPExcel_Exception("Invalid style array passed.");
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Get ARGB
  166. *
  167. * @return string
  168. */
  169. public function getARGB() {
  170. if ($this->_isSupervisor) {
  171. return $this->getSharedComponent()->getARGB();
  172. }
  173. return $this->_argb;
  174. }
  175. /**
  176. * Set ARGB
  177. *
  178. * @param string $pValue
  179. * @return PHPExcel_Style_Color
  180. */
  181. public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
  182. if ($pValue == '') {
  183. $pValue = PHPExcel_Style_Color::COLOR_BLACK;
  184. }
  185. if ($this->_isSupervisor) {
  186. $styleArray = $this->getStyleArray(array('argb' => $pValue));
  187. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  188. } else {
  189. $this->_argb = $pValue;
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Get RGB
  195. *
  196. * @return string
  197. */
  198. public function getRGB() {
  199. if ($this->_isSupervisor) {
  200. return $this->getSharedComponent()->getRGB();
  201. }
  202. return substr($this->_argb, 2);
  203. }
  204. /**
  205. * Set RGB
  206. *
  207. * @param string $pValue RGB value
  208. * @return PHPExcel_Style_Color
  209. */
  210. public function setRGB($pValue = '000000') {
  211. if ($pValue == '') {
  212. $pValue = '000000';
  213. }
  214. if ($this->_isSupervisor) {
  215. $styleArray = $this->getStyleArray(array('argb' => 'FF' . $pValue));
  216. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  217. } else {
  218. $this->_argb = 'FF' . $pValue;
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Get a specified colour component of an RGB value
  224. *
  225. * @private
  226. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  227. * @param int $offset Position within the RGB value to extract
  228. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  229. * decimal value
  230. * @return string The extracted colour component
  231. */
  232. private static function _getColourComponent($RGB,$offset,$hex=TRUE) {
  233. $colour = substr($RGB, $offset, 2);
  234. if (!$hex)
  235. $colour = hexdec($colour);
  236. return $colour;
  237. }
  238. /**
  239. * Get the red colour component of an RGB value
  240. *
  241. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  242. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  243. * decimal value
  244. * @return string The red colour component
  245. */
  246. public static function getRed($RGB,$hex=TRUE) {
  247. if (strlen($RGB) == 8) {
  248. return self::_getColourComponent($RGB, 2, $hex);
  249. } elseif (strlen($RGB) == 6) {
  250. return self::_getColourComponent($RGB, 0, $hex);
  251. }
  252. }
  253. /**
  254. * Get the green colour component of an RGB value
  255. *
  256. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  257. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  258. * decimal value
  259. * @return string The green colour component
  260. */
  261. public static function getGreen($RGB,$hex=TRUE) {
  262. if (strlen($RGB) == 8) {
  263. return self::_getColourComponent($RGB, 4, $hex);
  264. } elseif (strlen($RGB) == 6) {
  265. return self::_getColourComponent($RGB, 2, $hex);
  266. }
  267. }
  268. /**
  269. * Get the blue colour component of an RGB value
  270. *
  271. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  272. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  273. * decimal value
  274. * @return string The blue colour component
  275. */
  276. public static function getBlue($RGB,$hex=TRUE) {
  277. if (strlen($RGB) == 8) {
  278. return self::_getColourComponent($RGB, 6, $hex);
  279. } elseif (strlen($RGB) == 6) {
  280. return self::_getColourComponent($RGB, 4, $hex);
  281. }
  282. }
  283. /**
  284. * Adjust the brightness of a color
  285. *
  286. * @param string $hex The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  287. * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
  288. * @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  289. */
  290. public static function changeBrightness($hex, $adjustPercentage) {
  291. $rgba = (strlen($hex) == 8);
  292. $red = self::getRed($hex, FALSE);
  293. $green = self::getGreen($hex, FALSE);
  294. $blue = self::getBlue($hex, FALSE);
  295. if ($adjustPercentage > 0) {
  296. $red += (255 - $red) * $adjustPercentage;
  297. $green += (255 - $green) * $adjustPercentage;
  298. $blue += (255 - $blue) * $adjustPercentage;
  299. } else {
  300. $red += $red * $adjustPercentage;
  301. $green += $green * $adjustPercentage;
  302. $blue += $blue * $adjustPercentage;
  303. }
  304. if ($red < 0) $red = 0;
  305. elseif ($red > 255) $red = 255;
  306. if ($green < 0) $green = 0;
  307. elseif ($green > 255) $green = 255;
  308. if ($blue < 0) $blue = 0;
  309. elseif ($blue > 255) $blue = 255;
  310. $rgb = strtoupper( str_pad(dechex($red), 2, '0', 0) .
  311. str_pad(dechex($green), 2, '0', 0) .
  312. str_pad(dechex($blue), 2, '0', 0)
  313. );
  314. return (($rgba) ? 'FF' : '') . $rgb;
  315. }
  316. /**
  317. * Get indexed color
  318. *
  319. * @param int $pIndex Index entry point into the colour array
  320. * @param boolean $background Flag to indicate whether default background or foreground colour
  321. * should be returned if the indexed colour doesn't exist
  322. * @return PHPExcel_Style_Color
  323. */
  324. public static function indexedColor($pIndex, $background=FALSE) {
  325. // Clean parameter
  326. $pIndex = intval($pIndex);
  327. // Indexed colors
  328. if (is_null(self::$_indexedColors)) {
  329. self::$_indexedColors = array(
  330. 1 => 'FF000000', // System Colour #1 - Black
  331. 2 => 'FFFFFFFF', // System Colour #2 - White
  332. 3 => 'FFFF0000', // System Colour #3 - Red
  333. 4 => 'FF00FF00', // System Colour #4 - Green
  334. 5 => 'FF0000FF', // System Colour #5 - Blue
  335. 6 => 'FFFFFF00', // System Colour #6 - Yellow
  336. 7 => 'FFFF00FF', // System Colour #7- Magenta
  337. 8 => 'FF00FFFF', // System Colour #8- Cyan
  338. 9 => 'FF800000', // Standard Colour #9
  339. 10 => 'FF008000', // Standard Colour #10
  340. 11 => 'FF000080', // Standard Colour #11
  341. 12 => 'FF808000', // Standard Colour #12
  342. 13 => 'FF800080', // Standard Colour #13
  343. 14 => 'FF008080', // Standard Colour #14
  344. 15 => 'FFC0C0C0', // Standard Colour #15
  345. 16 => 'FF808080', // Standard Colour #16
  346. 17 => 'FF9999FF', // Chart Fill Colour #17
  347. 18 => 'FF993366', // Chart Fill Colour #18
  348. 19 => 'FFFFFFCC', // Chart Fill Colour #19
  349. 20 => 'FFCCFFFF', // Chart Fill Colour #20
  350. 21 => 'FF660066', // Chart Fill Colour #21
  351. 22 => 'FFFF8080', // Chart Fill Colour #22
  352. 23 => 'FF0066CC', // Chart Fill Colour #23
  353. 24 => 'FFCCCCFF', // Chart Fill Colour #24
  354. 25 => 'FF000080', // Chart Line Colour #25
  355. 26 => 'FFFF00FF', // Chart Line Colour #26
  356. 27 => 'FFFFFF00', // Chart Line Colour #27
  357. 28 => 'FF00FFFF', // Chart Line Colour #28
  358. 29 => 'FF800080', // Chart Line Colour #29
  359. 30 => 'FF800000', // Chart Line Colour #30
  360. 31 => 'FF008080', // Chart Line Colour #31
  361. 32 => 'FF0000FF', // Chart Line Colour #32
  362. 33 => 'FF00CCFF', // Standard Colour #33
  363. 34 => 'FFCCFFFF', // Standard Colour #34
  364. 35 => 'FFCCFFCC', // Standard Colour #35
  365. 36 => 'FFFFFF99', // Standard Colour #36
  366. 37 => 'FF99CCFF', // Standard Colour #37
  367. 38 => 'FFFF99CC', // Standard Colour #38
  368. 39 => 'FFCC99FF', // Standard Colour #39
  369. 40 => 'FFFFCC99', // Standard Colour #40
  370. 41 => 'FF3366FF', // Standard Colour #41
  371. 42 => 'FF33CCCC', // Standard Colour #42
  372. 43 => 'FF99CC00', // Standard Colour #43
  373. 44 => 'FFFFCC00', // Standard Colour #44
  374. 45 => 'FFFF9900', // Standard Colour #45
  375. 46 => 'FFFF6600', // Standard Colour #46
  376. 47 => 'FF666699', // Standard Colour #47
  377. 48 => 'FF969696', // Standard Colour #48
  378. 49 => 'FF003366', // Standard Colour #49
  379. 50 => 'FF339966', // Standard Colour #50
  380. 51 => 'FF003300', // Standard Colour #51
  381. 52 => 'FF333300', // Standard Colour #52
  382. 53 => 'FF993300', // Standard Colour #53
  383. 54 => 'FF993366', // Standard Colour #54
  384. 55 => 'FF333399', // Standard Colour #55
  385. 56 => 'FF333333' // Standard Colour #56
  386. );
  387. }
  388. if (array_key_exists($pIndex, self::$_indexedColors)) {
  389. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  390. }
  391. if ($background) {
  392. return new PHPExcel_Style_Color('FFFFFFFF');
  393. }
  394. return new PHPExcel_Style_Color('FF000000');
  395. }
  396. /**
  397. * Get hash code
  398. *
  399. * @return string Hash code
  400. */
  401. public function getHashCode() {
  402. if ($this->_isSupervisor) {
  403. return $this->getSharedComponent()->getHashCode();
  404. }
  405. return md5(
  406. $this->_argb
  407. . __CLASS__
  408. );
  409. }
  410. }