PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.6.0/Classes/PHPExcel/Style/Color.php

#
PHP | 255 lines | 141 code | 18 blank | 96 comment | 12 complexity | 7588925bed96301fdf00388af272594e 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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.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 - 2008 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 clors 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. * Create a new PHPExcel_Style_Color
  63. *
  64. * @param string $pARGB
  65. */
  66. public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK)
  67. {
  68. // Initialise values
  69. $this->_argb = $pARGB;
  70. }
  71. /**
  72. * Apply styles from array
  73. *
  74. * <code>
  75. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  76. * </code>
  77. *
  78. * @param array $pStyles Array containing style information
  79. * @throws Exception
  80. */
  81. public function applyFromArray($pStyles = null) {
  82. if (is_array($pStyles)) {
  83. if (array_key_exists('rgb', $pStyles)) {
  84. $this->setRGB($pStyles['rgb']);
  85. }
  86. if (array_key_exists('argb', $pStyles)) {
  87. $this->setARGB($pStyles['argb']);
  88. }
  89. } else {
  90. throw new Exception("Invalid style array passed.");
  91. }
  92. }
  93. /**
  94. * Get ARGB
  95. *
  96. * @return string
  97. */
  98. public function getARGB() {
  99. return $this->_argb;
  100. }
  101. /**
  102. * Set ARGB
  103. *
  104. * @param string $pValue
  105. */
  106. public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
  107. if ($pValue == '') {
  108. $pValue = PHPExcel_Style_Color::COLOR_BLACK;
  109. }
  110. $this->_argb = $pValue;
  111. }
  112. /**
  113. * Get RGB
  114. *
  115. * @return string
  116. */
  117. public function getRGB() {
  118. return substr($this->_argb, 2);
  119. }
  120. /**
  121. * Set RGB
  122. *
  123. * @param string $pValue
  124. */
  125. public function setRGB($pValue = '000000') {
  126. if ($pValue == '') {
  127. $pValue = '000000';
  128. }
  129. $this->_argb = 'FF' . $pValue;
  130. }
  131. /**
  132. * Get indexed color
  133. *
  134. * @param int $pIndex
  135. * @return PHPExcel_Style_Color
  136. */
  137. public static function indexedColor($pIndex) {
  138. // Clean parameter
  139. $pIndex = intval($pIndex);
  140. // Indexed colors
  141. if (is_null(self::$_indexedColors)) {
  142. self::$_indexedColors = array();
  143. self::$_indexedColors[] = '00000000';
  144. self::$_indexedColors[] = '00FFFFFF';
  145. self::$_indexedColors[] = '00FF0000';
  146. self::$_indexedColors[] = '0000FF00';
  147. self::$_indexedColors[] = '000000FF';
  148. self::$_indexedColors[] = '00FFFF00';
  149. self::$_indexedColors[] = '00FF00FF';
  150. self::$_indexedColors[] = '0000FFFF';
  151. self::$_indexedColors[] = '00000000';
  152. self::$_indexedColors[] = '00FFFFFF';
  153. self::$_indexedColors[] = '00FF0000';
  154. self::$_indexedColors[] = '0000FF00';
  155. self::$_indexedColors[] = '000000FF';
  156. self::$_indexedColors[] = '00FFFF00';
  157. self::$_indexedColors[] = '00FF00FF';
  158. self::$_indexedColors[] = '0000FFFF';
  159. self::$_indexedColors[] = '00800000';
  160. self::$_indexedColors[] = '00008000';
  161. self::$_indexedColors[] = '00000080';
  162. self::$_indexedColors[] = '00808000';
  163. self::$_indexedColors[] = '00800080';
  164. self::$_indexedColors[] = '00008080';
  165. self::$_indexedColors[] = '00C0C0C0';
  166. self::$_indexedColors[] = '00808080';
  167. self::$_indexedColors[] = '009999FF';
  168. self::$_indexedColors[] = '00993366';
  169. self::$_indexedColors[] = '00FFFFCC';
  170. self::$_indexedColors[] = '00CCFFFF';
  171. self::$_indexedColors[] = '00660066';
  172. self::$_indexedColors[] = '00FF8080';
  173. self::$_indexedColors[] = '000066CC';
  174. self::$_indexedColors[] = '00CCCCFF';
  175. self::$_indexedColors[] = '00000080';
  176. self::$_indexedColors[] = '00FF00FF';
  177. self::$_indexedColors[] = '00FFFF00';
  178. self::$_indexedColors[] = '0000FFFF';
  179. self::$_indexedColors[] = '00800080';
  180. self::$_indexedColors[] = '00800000';
  181. self::$_indexedColors[] = '00008080';
  182. self::$_indexedColors[] = '000000FF';
  183. self::$_indexedColors[] = '0000CCFF';
  184. self::$_indexedColors[] = '00CCFFFF';
  185. self::$_indexedColors[] = '00CCFFCC';
  186. self::$_indexedColors[] = '00FFFF99';
  187. self::$_indexedColors[] = '0099CCFF';
  188. self::$_indexedColors[] = '00FF99CC';
  189. self::$_indexedColors[] = '00CC99FF';
  190. self::$_indexedColors[] = '00FFCC99';
  191. self::$_indexedColors[] = '003366FF';
  192. self::$_indexedColors[] = '0033CCCC';
  193. self::$_indexedColors[] = '0099CC00';
  194. self::$_indexedColors[] = '00FFCC00';
  195. self::$_indexedColors[] = '00FF9900';
  196. self::$_indexedColors[] = '00FF6600';
  197. self::$_indexedColors[] = '00666699';
  198. self::$_indexedColors[] = '00969696';
  199. self::$_indexedColors[] = '00003366';
  200. self::$_indexedColors[] = '00339966';
  201. self::$_indexedColors[] = '00003300';
  202. self::$_indexedColors[] = '00333300';
  203. self::$_indexedColors[] = '00993300';
  204. self::$_indexedColors[] = '00993366';
  205. self::$_indexedColors[] = '00333399';
  206. self::$_indexedColors[] = '00333333';
  207. }
  208. if (array_key_exists($pIndex, self::$_indexedColors)) {
  209. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  210. }
  211. return new PHPExcel_Style_Color();
  212. }
  213. /**
  214. * Get hash code
  215. *
  216. * @return string Hash code
  217. */
  218. public function getHashCode() {
  219. return md5(
  220. $this->_argb
  221. . __CLASS__
  222. );
  223. }
  224. /**
  225. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  226. */
  227. public function __clone() {
  228. $vars = get_object_vars($this);
  229. foreach ($vars as $key => $value) {
  230. if (is_object($value)) {
  231. $this->$key = clone $value;
  232. } else {
  233. $this->$key = $value;
  234. }
  235. }
  236. }
  237. }