PageRenderTime 99ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/PHPExcel/PHPExcel/Style/Color.php

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