PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/Style/Color.php

https://github.com/jcplat/console-seolan
PHP | 451 lines | 232 code | 38 blank | 181 comment | 24 complexity | 7a5cf98f0d4fde13e05e695ac2d4f79f MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_IComparable */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  36. /**
  37. * PHPExcel_Style_Color
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Style
  41. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Style_Color implements PHPExcel_IComparable
  44. {
  45. /* Colors */
  46. const COLOR_BLACK = 'FF000000';
  47. const COLOR_WHITE = 'FFFFFFFF';
  48. const COLOR_RED = 'FFFF0000';
  49. const COLOR_DARKRED = 'FF800000';
  50. const COLOR_BLUE = 'FF0000FF';
  51. const COLOR_DARKBLUE = 'FF000080';
  52. const COLOR_GREEN = 'FF00FF00';
  53. const COLOR_DARKGREEN = 'FF008000';
  54. const COLOR_YELLOW = 'FFFFFF00';
  55. const COLOR_DARKYELLOW = 'FF808000';
  56. /**
  57. * Indexed colors array
  58. *
  59. * @var array
  60. */
  61. private static $_indexedColors;
  62. /**
  63. * ARGB - Alpha RGB
  64. *
  65. * @var string
  66. */
  67. private $_argb;
  68. /**
  69. * Supervisor?
  70. *
  71. * @var boolean
  72. */
  73. private $_isSupervisor;
  74. /**
  75. * Parent. Only used for supervisor
  76. *
  77. * @var mixed
  78. */
  79. private $_parent;
  80. /**
  81. * Parent property name
  82. *
  83. * @var string
  84. */
  85. private $_parentPropertyName;
  86. /**
  87. * Create a new PHPExcel_Style_Color
  88. *
  89. * @param string $pARGB
  90. */
  91. public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor = false)
  92. {
  93. // Supervisor?
  94. $this->_isSupervisor = $isSupervisor;
  95. // Initialise values
  96. $this->_argb = $pARGB;
  97. }
  98. /**
  99. * Bind parent. Only used for supervisor
  100. *
  101. * @param mixed $parent
  102. * @param string $parentPropertyName
  103. * @return PHPExcel_Style_Color
  104. */
  105. public function bindParent($parent, $parentPropertyName)
  106. {
  107. $this->_parent = $parent;
  108. $this->_parentPropertyName = $parentPropertyName;
  109. return $this;
  110. }
  111. /**
  112. * Is this a supervisor or a real style component?
  113. *
  114. * @return boolean
  115. */
  116. public function getIsSupervisor()
  117. {
  118. return $this->_isSupervisor;
  119. }
  120. /**
  121. * Get the shared style component for the currently active cell in currently active sheet.
  122. * Only used for style supervisor
  123. *
  124. * @return PHPExcel_Style_Color
  125. */
  126. public function getSharedComponent()
  127. {
  128. switch ($this->_parentPropertyName) {
  129. case '_endColor':
  130. return $this->_parent->getSharedComponent()->getEndColor();
  131. break;
  132. case '_color':
  133. return $this->_parent->getSharedComponent()->getColor();
  134. break;
  135. case '_startColor':
  136. return $this->_parent->getSharedComponent()->getStartColor();
  137. break;
  138. }
  139. }
  140. /**
  141. * Get the currently active sheet. Only used for supervisor
  142. *
  143. * @return PHPExcel_Worksheet
  144. */
  145. public function getActiveSheet()
  146. {
  147. return $this->_parent->getActiveSheet();
  148. }
  149. /**
  150. * Get the currently active cell coordinate in currently active sheet.
  151. * Only used for supervisor
  152. *
  153. * @return string E.g. 'A1'
  154. */
  155. public function getXSelectedCells()
  156. {
  157. return $this->getActiveSheet()->getXSelectedCells();
  158. }
  159. /**
  160. * Get the currently active cell coordinate in currently active sheet.
  161. * Only used for supervisor
  162. *
  163. * @return string E.g. 'A1'
  164. */
  165. public function getXActiveCell()
  166. {
  167. return $this->getActiveSheet()->getXActiveCell();
  168. }
  169. /**
  170. * Build style array from subcomponents
  171. *
  172. * @param array $array
  173. * @return array
  174. */
  175. public function getStyleArray($array)
  176. {
  177. switch ($this->_parentPropertyName) {
  178. case '_endColor':
  179. $key = 'endcolor';
  180. break;
  181. case '_color':
  182. $key = 'color';
  183. break;
  184. case '_startColor':
  185. $key = 'startcolor';
  186. break;
  187. }
  188. return $this->_parent->getStyleArray(array($key => $array));
  189. }
  190. /**
  191. * Apply styles from array
  192. *
  193. * <code>
  194. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  195. * </code>
  196. *
  197. * @param array $pStyles Array containing style information
  198. * @throws Exception
  199. * @return PHPExcel_Style_Color
  200. */
  201. public function applyFromArray($pStyles = null) {
  202. if (is_array($pStyles)) {
  203. if ($this->_isSupervisor) {
  204. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  205. } else {
  206. if (array_key_exists('rgb', $pStyles)) {
  207. $this->setRGB($pStyles['rgb']);
  208. }
  209. if (array_key_exists('argb', $pStyles)) {
  210. $this->setARGB($pStyles['argb']);
  211. }
  212. }
  213. } else {
  214. throw new Exception("Invalid style array passed.");
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Get ARGB
  220. *
  221. * @return string
  222. */
  223. public function getARGB() {
  224. if ($this->_isSupervisor) {
  225. return $this->getSharedComponent()->getARGB();
  226. }
  227. return $this->_argb;
  228. }
  229. /**
  230. * Set ARGB
  231. *
  232. * @param string $pValue
  233. * @return PHPExcel_Style_Color
  234. */
  235. public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
  236. if ($pValue == '') {
  237. $pValue = PHPExcel_Style_Color::COLOR_BLACK;
  238. }
  239. if ($this->_isSupervisor) {
  240. $styleArray = $this->getStyleArray(array('argb' => $pValue));
  241. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  242. } else {
  243. $this->_argb = $pValue;
  244. }
  245. return $this;
  246. }
  247. /**
  248. * Get RGB
  249. *
  250. * @return string
  251. */
  252. public function getRGB() {
  253. if ($this->_isSupervisor) {
  254. return $this->getSharedComponent()->getRGB();
  255. }
  256. return substr($this->_argb, 2);
  257. }
  258. /**
  259. * Set RGB
  260. *
  261. * @param string $pValue
  262. * @return PHPExcel_Style_Color
  263. */
  264. public function setRGB($pValue = '000000') {
  265. if ($pValue == '') {
  266. $pValue = '000000';
  267. }
  268. if ($this->_isSupervisor) {
  269. $styleArray = $this->getStyleArray(array('argb' => 'FF' . $pValue));
  270. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  271. } else {
  272. $this->_argb = 'FF' . $pValue;
  273. }
  274. return $this;
  275. }
  276. /**
  277. * Get indexed color
  278. *
  279. * @param int $pIndex
  280. * @return PHPExcel_Style_Color
  281. */
  282. public static function indexedColor($pIndex) {
  283. // Clean parameter
  284. $pIndex = intval($pIndex);
  285. // Indexed colors
  286. if (is_null(self::$_indexedColors)) {
  287. self::$_indexedColors = array();
  288. self::$_indexedColors[] = '00000000';
  289. self::$_indexedColors[] = '00FFFFFF';
  290. self::$_indexedColors[] = '00FF0000';
  291. self::$_indexedColors[] = '0000FF00';
  292. self::$_indexedColors[] = '000000FF';
  293. self::$_indexedColors[] = '00FFFF00';
  294. self::$_indexedColors[] = '00FF00FF';
  295. self::$_indexedColors[] = '0000FFFF';
  296. self::$_indexedColors[] = '00000000';
  297. self::$_indexedColors[] = '00FFFFFF';
  298. self::$_indexedColors[] = '00FF0000';
  299. self::$_indexedColors[] = '0000FF00';
  300. self::$_indexedColors[] = '000000FF';
  301. self::$_indexedColors[] = '00FFFF00';
  302. self::$_indexedColors[] = '00FF00FF';
  303. self::$_indexedColors[] = '0000FFFF';
  304. self::$_indexedColors[] = '00800000';
  305. self::$_indexedColors[] = '00008000';
  306. self::$_indexedColors[] = '00000080';
  307. self::$_indexedColors[] = '00808000';
  308. self::$_indexedColors[] = '00800080';
  309. self::$_indexedColors[] = '00008080';
  310. self::$_indexedColors[] = '00C0C0C0';
  311. self::$_indexedColors[] = '00808080';
  312. self::$_indexedColors[] = '009999FF';
  313. self::$_indexedColors[] = '00993366';
  314. self::$_indexedColors[] = '00FFFFCC';
  315. self::$_indexedColors[] = '00CCFFFF';
  316. self::$_indexedColors[] = '00660066';
  317. self::$_indexedColors[] = '00FF8080';
  318. self::$_indexedColors[] = '000066CC';
  319. self::$_indexedColors[] = '00CCCCFF';
  320. self::$_indexedColors[] = '00000080';
  321. self::$_indexedColors[] = '00FF00FF';
  322. self::$_indexedColors[] = '00FFFF00';
  323. self::$_indexedColors[] = '0000FFFF';
  324. self::$_indexedColors[] = '00800080';
  325. self::$_indexedColors[] = '00800000';
  326. self::$_indexedColors[] = '00008080';
  327. self::$_indexedColors[] = '000000FF';
  328. self::$_indexedColors[] = '0000CCFF';
  329. self::$_indexedColors[] = '00CCFFFF';
  330. self::$_indexedColors[] = '00CCFFCC';
  331. self::$_indexedColors[] = '00FFFF99';
  332. self::$_indexedColors[] = '0099CCFF';
  333. self::$_indexedColors[] = '00FF99CC';
  334. self::$_indexedColors[] = '00CC99FF';
  335. self::$_indexedColors[] = '00FFCC99';
  336. self::$_indexedColors[] = '003366FF';
  337. self::$_indexedColors[] = '0033CCCC';
  338. self::$_indexedColors[] = '0099CC00';
  339. self::$_indexedColors[] = '00FFCC00';
  340. self::$_indexedColors[] = '00FF9900';
  341. self::$_indexedColors[] = '00FF6600';
  342. self::$_indexedColors[] = '00666699';
  343. self::$_indexedColors[] = '00969696';
  344. self::$_indexedColors[] = '00003366';
  345. self::$_indexedColors[] = '00339966';
  346. self::$_indexedColors[] = '00003300';
  347. self::$_indexedColors[] = '00333300';
  348. self::$_indexedColors[] = '00993300';
  349. self::$_indexedColors[] = '00993366';
  350. self::$_indexedColors[] = '00333399';
  351. self::$_indexedColors[] = '00333333';
  352. }
  353. if (array_key_exists($pIndex, self::$_indexedColors)) {
  354. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  355. }
  356. return new PHPExcel_Style_Color();
  357. }
  358. /**
  359. * Get hash code
  360. *
  361. * @return string Hash code
  362. */
  363. public function getHashCode() {
  364. if ($this->_isSupervisor) {
  365. return $this->getSharedComponent()->getHashCode();
  366. }
  367. return md5(
  368. $this->_argb
  369. . __CLASS__
  370. );
  371. }
  372. /**
  373. * Hash index
  374. *
  375. * @var string
  376. */
  377. private $_hashIndex;
  378. /**
  379. * Get hash index
  380. *
  381. * Note that this index may vary during script execution! Only reliable moment is
  382. * while doing a write of a workbook and when changes are not allowed.
  383. *
  384. * @return string Hash index
  385. */
  386. public function getHashIndex() {
  387. return $this->_hashIndex;
  388. }
  389. /**
  390. * Set hash index
  391. *
  392. * Note that this index may vary during script execution! Only reliable moment is
  393. * while doing a write of a workbook and when changes are not allowed.
  394. *
  395. * @param string $value Hash index
  396. */
  397. public function setHashIndex($value) {
  398. $this->_hashIndex = $value;
  399. }
  400. /**
  401. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  402. */
  403. public function __clone() {
  404. $vars = get_object_vars($this);
  405. foreach ($vars as $key => $value) {
  406. if (is_object($value)) {
  407. $this->$key = clone $value;
  408. } else {
  409. $this->$key = $value;
  410. }
  411. }
  412. }
  413. }