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

/htdocs/phpmyadmin/libraries/PHPExcel/PHPExcel/Style/Color.php

http://github.com/jyr/MNPP
PHP | 408 lines | 221 code | 32 blank | 155 comment | 25 complexity | 34397102fdc474304ad09ad00ce29203 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.4, 2010-08-26
  26. */
  27. /**
  28. * PHPExcel_Style_Color
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2010 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
  81. */
  82. public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor = false)
  83. {
  84. // Supervisor?
  85. $this->_isSupervisor = $isSupervisor;
  86. // Initialise values
  87. $this->_argb = $pARGB;
  88. }
  89. /**
  90. * Bind parent. Only used for supervisor
  91. *
  92. * @param mixed $parent
  93. * @param string $parentPropertyName
  94. * @return PHPExcel_Style_Color
  95. */
  96. public function bindParent($parent, $parentPropertyName)
  97. {
  98. $this->_parent = $parent;
  99. $this->_parentPropertyName = $parentPropertyName;
  100. return $this;
  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 getSelectedCells()
  147. {
  148. return $this->getActiveSheet()->getSelectedCells();
  149. }
  150. /**
  151. * Get the currently active cell coordinate in currently active sheet.
  152. * Only used for supervisor
  153. *
  154. * @return string E.g. 'A1'
  155. */
  156. public function getActiveCell()
  157. {
  158. return $this->getActiveSheet()->getActiveCell();
  159. }
  160. /**
  161. * Build style array from subcomponents
  162. *
  163. * @param array $array
  164. * @return array
  165. */
  166. public function getStyleArray($array)
  167. {
  168. switch ($this->_parentPropertyName) {
  169. case '_endColor':
  170. $key = 'endcolor';
  171. break;
  172. case '_color':
  173. $key = 'color';
  174. break;
  175. case '_startColor':
  176. $key = 'startcolor';
  177. break;
  178. }
  179. return $this->_parent->getStyleArray(array($key => $array));
  180. }
  181. /**
  182. * Apply styles from array
  183. *
  184. * <code>
  185. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  186. * </code>
  187. *
  188. * @param array $pStyles Array containing style information
  189. * @throws Exception
  190. * @return PHPExcel_Style_Color
  191. */
  192. public function applyFromArray($pStyles = null) {
  193. if (is_array($pStyles)) {
  194. if ($this->_isSupervisor) {
  195. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  196. } else {
  197. if (array_key_exists('rgb', $pStyles)) {
  198. $this->setRGB($pStyles['rgb']);
  199. }
  200. if (array_key_exists('argb', $pStyles)) {
  201. $this->setARGB($pStyles['argb']);
  202. }
  203. }
  204. } else {
  205. throw new Exception("Invalid style array passed.");
  206. }
  207. return $this;
  208. }
  209. /**
  210. * Get ARGB
  211. *
  212. * @return string
  213. */
  214. public function getARGB() {
  215. if ($this->_isSupervisor) {
  216. return $this->getSharedComponent()->getARGB();
  217. }
  218. return $this->_argb;
  219. }
  220. /**
  221. * Set ARGB
  222. *
  223. * @param string $pValue
  224. * @return PHPExcel_Style_Color
  225. */
  226. public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
  227. if ($pValue == '') {
  228. $pValue = PHPExcel_Style_Color::COLOR_BLACK;
  229. }
  230. if ($this->_isSupervisor) {
  231. $styleArray = $this->getStyleArray(array('argb' => $pValue));
  232. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  233. } else {
  234. $this->_argb = $pValue;
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Get RGB
  240. *
  241. * @return string
  242. */
  243. public function getRGB() {
  244. if ($this->_isSupervisor) {
  245. return $this->getSharedComponent()->getRGB();
  246. }
  247. return substr($this->_argb, 2);
  248. }
  249. /**
  250. * Set RGB
  251. *
  252. * @param string $pValue
  253. * @return PHPExcel_Style_Color
  254. */
  255. public function setRGB($pValue = '000000') {
  256. if ($pValue == '') {
  257. $pValue = '000000';
  258. }
  259. if ($this->_isSupervisor) {
  260. $styleArray = $this->getStyleArray(array('argb' => 'FF' . $pValue));
  261. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  262. } else {
  263. $this->_argb = 'FF' . $pValue;
  264. }
  265. return $this;
  266. }
  267. /**
  268. * Get indexed color
  269. *
  270. * @param int $pIndex
  271. * @return PHPExcel_Style_Color
  272. */
  273. public static function indexedColor($pIndex) {
  274. // Clean parameter
  275. $pIndex = intval($pIndex);
  276. // Indexed colors
  277. if (is_null(self::$_indexedColors)) {
  278. self::$_indexedColors = array();
  279. self::$_indexedColors[] = '00000000';
  280. self::$_indexedColors[] = '00FFFFFF';
  281. self::$_indexedColors[] = '00FF0000';
  282. self::$_indexedColors[] = '0000FF00';
  283. self::$_indexedColors[] = '000000FF';
  284. self::$_indexedColors[] = '00FFFF00';
  285. self::$_indexedColors[] = '00FF00FF';
  286. self::$_indexedColors[] = '0000FFFF';
  287. self::$_indexedColors[] = '00000000';
  288. self::$_indexedColors[] = '00FFFFFF';
  289. self::$_indexedColors[] = '00FF0000';
  290. self::$_indexedColors[] = '0000FF00';
  291. self::$_indexedColors[] = '000000FF';
  292. self::$_indexedColors[] = '00FFFF00';
  293. self::$_indexedColors[] = '00FF00FF';
  294. self::$_indexedColors[] = '0000FFFF';
  295. self::$_indexedColors[] = '00800000';
  296. self::$_indexedColors[] = '00008000';
  297. self::$_indexedColors[] = '00000080';
  298. self::$_indexedColors[] = '00808000';
  299. self::$_indexedColors[] = '00800080';
  300. self::$_indexedColors[] = '00008080';
  301. self::$_indexedColors[] = '00C0C0C0';
  302. self::$_indexedColors[] = '00808080';
  303. self::$_indexedColors[] = '009999FF';
  304. self::$_indexedColors[] = '00993366';
  305. self::$_indexedColors[] = '00FFFFCC';
  306. self::$_indexedColors[] = '00CCFFFF';
  307. self::$_indexedColors[] = '00660066';
  308. self::$_indexedColors[] = '00FF8080';
  309. self::$_indexedColors[] = '000066CC';
  310. self::$_indexedColors[] = '00CCCCFF';
  311. self::$_indexedColors[] = '00000080';
  312. self::$_indexedColors[] = '00FF00FF';
  313. self::$_indexedColors[] = '00FFFF00';
  314. self::$_indexedColors[] = '0000FFFF';
  315. self::$_indexedColors[] = '00800080';
  316. self::$_indexedColors[] = '00800000';
  317. self::$_indexedColors[] = '00008080';
  318. self::$_indexedColors[] = '000000FF';
  319. self::$_indexedColors[] = '0000CCFF';
  320. self::$_indexedColors[] = '00CCFFFF';
  321. self::$_indexedColors[] = '00CCFFCC';
  322. self::$_indexedColors[] = '00FFFF99';
  323. self::$_indexedColors[] = '0099CCFF';
  324. self::$_indexedColors[] = '00FF99CC';
  325. self::$_indexedColors[] = '00CC99FF';
  326. self::$_indexedColors[] = '00FFCC99';
  327. self::$_indexedColors[] = '003366FF';
  328. self::$_indexedColors[] = '0033CCCC';
  329. self::$_indexedColors[] = '0099CC00';
  330. self::$_indexedColors[] = '00FFCC00';
  331. self::$_indexedColors[] = '00FF9900';
  332. self::$_indexedColors[] = '00FF6600';
  333. self::$_indexedColors[] = '00666699';
  334. self::$_indexedColors[] = '00969696';
  335. self::$_indexedColors[] = '00003366';
  336. self::$_indexedColors[] = '00339966';
  337. self::$_indexedColors[] = '00003300';
  338. self::$_indexedColors[] = '00333300';
  339. self::$_indexedColors[] = '00993300';
  340. self::$_indexedColors[] = '00993366';
  341. self::$_indexedColors[] = '00333399';
  342. self::$_indexedColors[] = '00333333';
  343. }
  344. if (array_key_exists($pIndex, self::$_indexedColors)) {
  345. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  346. }
  347. return new PHPExcel_Style_Color();
  348. }
  349. /**
  350. * Get hash code
  351. *
  352. * @return string Hash code
  353. */
  354. public function getHashCode() {
  355. if ($this->_isSupervisor) {
  356. return $this->getSharedComponent()->getHashCode();
  357. }
  358. return md5(
  359. $this->_argb
  360. . __CLASS__
  361. );
  362. }
  363. /**
  364. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  365. */
  366. public function __clone() {
  367. $vars = get_object_vars($this);
  368. foreach ($vars as $key => $value) {
  369. if ((is_object($value)) && ($key != '_parent')) {
  370. $this->$key = clone $value;
  371. } else {
  372. $this->$key = $value;
  373. }
  374. }
  375. }
  376. }