PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Color.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 510 lines | 269 code | 37 blank | 204 comment | 43 complexity | d629e5e491b67788b4a25f6b1a5d94ad MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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 1.7.8, 2012-10-12
  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 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. private static $_indexedColors;
  53. /**
  54. * ARGB - Alpha RGB
  55. *
  56. * @var string
  57. */
  58. private $_argb = NULL;
  59. /**
  60. * Supervisor?
  61. *
  62. * @var boolean
  63. */
  64. private $_isSupervisor;
  65. /**
  66. * Parent. Only used for supervisor
  67. *
  68. * @var mixed
  69. */
  70. private $_parent;
  71. /**
  72. * Parent property name
  73. *
  74. * @var string
  75. */
  76. private $_parentPropertyName;
  77. /**
  78. * Create a new PHPExcel_Style_Color
  79. *
  80. * @param string $pARGB ARGB value for the colour
  81. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  82. * Leave this value at default unless you understand exactly what
  83. * its ramifications are
  84. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  85. * Leave this value at default unless you understand exactly what
  86. * its ramifications are
  87. */
  88. public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
  89. {
  90. // Supervisor?
  91. $this->_isSupervisor = $isSupervisor;
  92. // Initialise values
  93. if (!$isConditional) {
  94. $this->_argb = $pARGB;
  95. }
  96. }
  97. /**
  98. * Bind parent. Only used for supervisor
  99. *
  100. * @param mixed $parent
  101. * @param string $parentPropertyName
  102. * @return PHPExcel_Style_Color
  103. */
  104. public function bindParent($parent, $parentPropertyName)
  105. {
  106. $this->_parent = $parent;
  107. $this->_parentPropertyName = $parentPropertyName;
  108. return $this;
  109. }
  110. /**
  111. * Is this a supervisor or a real style component?
  112. *
  113. * @return boolean
  114. */
  115. public function getIsSupervisor()
  116. {
  117. return $this->_isSupervisor;
  118. }
  119. /**
  120. * Get the shared style component for the currently active cell in currently active sheet.
  121. * Only used for style supervisor
  122. *
  123. * @return PHPExcel_Style_Color
  124. */
  125. public function getSharedComponent()
  126. {
  127. switch ($this->_parentPropertyName) {
  128. case '_endColor':
  129. return $this->_parent->getSharedComponent()->getEndColor(); break;
  130. case '_color':
  131. return $this->_parent->getSharedComponent()->getColor(); break;
  132. case '_startColor':
  133. return $this->_parent->getSharedComponent()->getStartColor(); break;
  134. }
  135. }
  136. /**
  137. * Get the currently active sheet. Only used for supervisor
  138. *
  139. * @return PHPExcel_Worksheet
  140. */
  141. public function getActiveSheet()
  142. {
  143. return $this->_parent->getActiveSheet();
  144. }
  145. /**
  146. * Get the currently active cell coordinate in currently active sheet.
  147. * Only used for supervisor
  148. *
  149. * @return string E.g. 'A1'
  150. */
  151. public function getSelectedCells()
  152. {
  153. return $this->getActiveSheet()->getSelectedCells();
  154. }
  155. /**
  156. * Get the currently active cell coordinate in currently active sheet.
  157. * Only used for supervisor
  158. *
  159. * @return string E.g. 'A1'
  160. */
  161. public function getActiveCell()
  162. {
  163. return $this->getActiveSheet()->getActiveCell();
  164. }
  165. /**
  166. * Build style array from subcomponents
  167. *
  168. * @param array $array
  169. * @return array
  170. */
  171. public function getStyleArray($array)
  172. {
  173. switch ($this->_parentPropertyName) {
  174. case '_endColor':
  175. $key = 'endcolor';
  176. break;
  177. case '_color':
  178. $key = 'color';
  179. break;
  180. case '_startColor':
  181. $key = 'startcolor';
  182. break;
  183. }
  184. return $this->_parent->getStyleArray(array($key => $array));
  185. }
  186. /**
  187. * Apply styles from array
  188. *
  189. * <code>
  190. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  191. * </code>
  192. *
  193. * @param array $pStyles Array containing style information
  194. * @throws Exception
  195. * @return PHPExcel_Style_Color
  196. */
  197. public function applyFromArray($pStyles = NULL) {
  198. if (is_array($pStyles)) {
  199. if ($this->_isSupervisor) {
  200. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  201. } else {
  202. if (array_key_exists('rgb', $pStyles)) {
  203. $this->setRGB($pStyles['rgb']);
  204. }
  205. if (array_key_exists('argb', $pStyles)) {
  206. $this->setARGB($pStyles['argb']);
  207. }
  208. }
  209. } else {
  210. throw new Exception("Invalid style array passed.");
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Get ARGB
  216. *
  217. * @return string
  218. */
  219. public function getARGB() {
  220. if ($this->_isSupervisor) {
  221. return $this->getSharedComponent()->getARGB();
  222. }
  223. return $this->_argb;
  224. }
  225. /**
  226. * Set ARGB
  227. *
  228. * @param string $pValue
  229. * @return PHPExcel_Style_Color
  230. */
  231. public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
  232. if ($pValue == '') {
  233. $pValue = PHPExcel_Style_Color::COLOR_BLACK;
  234. }
  235. if ($this->_isSupervisor) {
  236. $styleArray = $this->getStyleArray(array('argb' => $pValue));
  237. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  238. } else {
  239. $this->_argb = $pValue;
  240. }
  241. return $this;
  242. }
  243. /**
  244. * Get RGB
  245. *
  246. * @return string
  247. */
  248. public function getRGB() {
  249. if ($this->_isSupervisor) {
  250. return $this->getSharedComponent()->getRGB();
  251. }
  252. return substr($this->_argb, 2);
  253. }
  254. /**
  255. * Set RGB
  256. *
  257. * @param string $pValue RGB value
  258. * @return PHPExcel_Style_Color
  259. */
  260. public function setRGB($pValue = '000000') {
  261. if ($pValue == '') {
  262. $pValue = '000000';
  263. }
  264. if ($this->_isSupervisor) {
  265. $styleArray = $this->getStyleArray(array('argb' => 'FF' . $pValue));
  266. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  267. } else {
  268. $this->_argb = 'FF' . $pValue;
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Get a specified colour component of an RGB value
  274. *
  275. * @private
  276. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  277. * @param int $offset Position within the RGB value to extract
  278. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  279. * decimal value
  280. * @return string The extracted colour component
  281. */
  282. private static function _getColourComponent($RGB,$offset,$hex=TRUE) {
  283. $colour = substr($RGB, $offset, 2);
  284. if (!$hex)
  285. $colour = hexdec($colour);
  286. return $colour;
  287. }
  288. /**
  289. * Get the red colour component of an RGB value
  290. *
  291. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  292. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  293. * decimal value
  294. * @return string The red colour component
  295. */
  296. public static function getRed($RGB,$hex=TRUE) {
  297. if (strlen($RGB) == 8) {
  298. return self::_getColourComponent($RGB, 2, $hex);
  299. } elseif (strlen($RGB) == 6) {
  300. return self::_getColourComponent($RGB, 0, $hex);
  301. }
  302. }
  303. /**
  304. * Get the green colour component of an RGB value
  305. *
  306. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  307. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  308. * decimal value
  309. * @return string The green colour component
  310. */
  311. public static function getGreen($RGB,$hex=TRUE) {
  312. if (strlen($RGB) == 8) {
  313. return self::_getColourComponent($RGB, 4, $hex);
  314. } elseif (strlen($RGB) == 6) {
  315. return self::_getColourComponent($RGB, 2, $hex);
  316. }
  317. }
  318. /**
  319. * Get the blue colour component of an RGB value
  320. *
  321. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  322. * @param boolean $hex Flag indicating whether the component should be returned as a hex or a
  323. * decimal value
  324. * @return string The blue colour component
  325. */
  326. public static function getBlue($RGB,$hex=TRUE) {
  327. if (strlen($RGB) == 8) {
  328. return self::_getColourComponent($RGB, 6, $hex);
  329. } elseif (strlen($RGB) == 6) {
  330. return self::_getColourComponent($RGB, 4, $hex);
  331. }
  332. }
  333. /**
  334. * Adjust the brightness of a color
  335. *
  336. * @param string $hex The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  337. * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
  338. * @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  339. */
  340. public static function changeBrightness($hex, $adjustPercentage) {
  341. $rgba = (strlen($hex) == 8);
  342. $red = self::getRed($hex, FALSE);
  343. $green = self::getGreen($hex, FALSE);
  344. $blue = self::getBlue($hex, FALSE);
  345. if ($adjustPercentage > 0) {
  346. $red += (255 - $red) * $adjustPercentage;
  347. $green += (255 - $green) * $adjustPercentage;
  348. $blue += (255 - $blue) * $adjustPercentage;
  349. } else {
  350. $red += $red * $adjustPercentage;
  351. $green += $green * $adjustPercentage;
  352. $blue += $blue * $adjustPercentage;
  353. }
  354. if ($red < 0) $red = 0;
  355. elseif ($red > 255) $red = 255;
  356. if ($green < 0) $green = 0;
  357. elseif ($green > 255) $green = 255;
  358. if ($blue < 0) $blue = 0;
  359. elseif ($blue > 255) $blue = 255;
  360. $rgb = strtoupper( str_pad(dechex($red), 2, '0', 0) .
  361. str_pad(dechex($green), 2, '0', 0) .
  362. str_pad(dechex($blue), 2, '0', 0)
  363. );
  364. return (($rgba) ? 'FF' : '') . $rgb;
  365. }
  366. /**
  367. * Get indexed color
  368. *
  369. * @param int $pIndex Index entry point into the colour array
  370. * @param boolean $background Flag to indicate whether default background or foreground colour
  371. * should be returned if the indexed colour doesn't exist
  372. * @return PHPExcel_Style_Color
  373. */
  374. public static function indexedColor($pIndex, $background=FALSE) {
  375. // Clean parameter
  376. $pIndex = intval($pIndex);
  377. // Indexed colors
  378. if (is_null(self::$_indexedColors)) {
  379. self::$_indexedColors = array(
  380. 1 => 'FF000000', // System Colour #1 - Black
  381. 2 => 'FFFFFFFF', // System Colour #2 - White
  382. 3 => 'FFFF0000', // System Colour #3 - Red
  383. 4 => 'FF00FF00', // System Colour #4 - Green
  384. 5 => 'FF0000FF', // System Colour #5 - Blue
  385. 6 => 'FFFFFF00', // System Colour #6 - Yellow
  386. 7 => 'FFFF00FF', // System Colour #7- Magenta
  387. 8 => 'FF00FFFF', // System Colour #8- Cyan
  388. 9 => 'FF800000', // Standard Colour #9
  389. 10 => 'FF008000', // Standard Colour #10
  390. 11 => 'FF000080', // Standard Colour #11
  391. 12 => 'FF808000', // Standard Colour #12
  392. 13 => 'FF800080', // Standard Colour #13
  393. 14 => 'FF008080', // Standard Colour #14
  394. 15 => 'FFC0C0C0', // Standard Colour #15
  395. 16 => 'FF808080', // Standard Colour #16
  396. 17 => 'FF9999FF', // Chart Fill Colour #17
  397. 18 => 'FF993366', // Chart Fill Colour #18
  398. 19 => 'FFFFFFCC', // Chart Fill Colour #19
  399. 20 => 'FFCCFFFF', // Chart Fill Colour #20
  400. 21 => 'FF660066', // Chart Fill Colour #21
  401. 22 => 'FFFF8080', // Chart Fill Colour #22
  402. 23 => 'FF0066CC', // Chart Fill Colour #23
  403. 24 => 'FFCCCCFF', // Chart Fill Colour #24
  404. 25 => 'FF000080', // Chart Line Colour #25
  405. 26 => 'FFFF00FF', // Chart Line Colour #26
  406. 27 => 'FFFFFF00', // Chart Line Colour #27
  407. 28 => 'FF00FFFF', // Chart Line Colour #28
  408. 29 => 'FF800080', // Chart Line Colour #29
  409. 30 => 'FF800000', // Chart Line Colour #30
  410. 31 => 'FF008080', // Chart Line Colour #31
  411. 32 => 'FF0000FF', // Chart Line Colour #32
  412. 33 => 'FF00CCFF', // Standard Colour #33
  413. 34 => 'FFCCFFFF', // Standard Colour #34
  414. 35 => 'FFCCFFCC', // Standard Colour #35
  415. 36 => 'FFFFFF99', // Standard Colour #36
  416. 37 => 'FF99CCFF', // Standard Colour #37
  417. 38 => 'FFFF99CC', // Standard Colour #38
  418. 39 => 'FFCC99FF', // Standard Colour #39
  419. 40 => 'FFFFCC99', // Standard Colour #40
  420. 41 => 'FF3366FF', // Standard Colour #41
  421. 42 => 'FF33CCCC', // Standard Colour #42
  422. 43 => 'FF99CC00', // Standard Colour #43
  423. 44 => 'FFFFCC00', // Standard Colour #44
  424. 45 => 'FFFF9900', // Standard Colour #45
  425. 46 => 'FFFF6600', // Standard Colour #46
  426. 47 => 'FF666699', // Standard Colour #47
  427. 48 => 'FF969696', // Standard Colour #48
  428. 49 => 'FF003366', // Standard Colour #49
  429. 50 => 'FF339966', // Standard Colour #50
  430. 51 => 'FF003300', // Standard Colour #51
  431. 52 => 'FF333300', // Standard Colour #52
  432. 53 => 'FF993300', // Standard Colour #53
  433. 54 => 'FF993366', // Standard Colour #54
  434. 55 => 'FF333399', // Standard Colour #55
  435. 56 => 'FF333333' // Standard Colour #56
  436. );
  437. }
  438. if (array_key_exists($pIndex, self::$_indexedColors)) {
  439. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  440. }
  441. if ($background) {
  442. return new PHPExcel_Style_Color('FFFFFFFF');
  443. }
  444. return new PHPExcel_Style_Color('FF000000');
  445. }
  446. /**
  447. * Get hash code
  448. *
  449. * @return string Hash code
  450. */
  451. public function getHashCode() {
  452. if ($this->_isSupervisor) {
  453. return $this->getSharedComponent()->getHashCode();
  454. }
  455. return md5(
  456. $this->_argb
  457. . __CLASS__
  458. );
  459. }
  460. /**
  461. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  462. */
  463. public function __clone() {
  464. $vars = get_object_vars($this);
  465. foreach ($vars as $key => $value) {
  466. if ((is_object($value)) && ($key != '_parent')) {
  467. $this->$key = clone $value;
  468. } else {
  469. $this->$key = $value;
  470. }
  471. }
  472. }
  473. }