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

/branches/wi6857-memory/Classes/PHPExcel/Style/Color.php

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