PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/phpexcel/PHPExcel/Style/Color.php

https://github.com/alonerage/Hudo
PHP | 476 lines | 275 code | 39 blank | 162 comment | 41 complexity | 2485034176c6d09e7b087e3ce6a6e9ab MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Style_Color
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2011 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. private static function _getColourComponent($RGB,$offset,$hex=true) {
  268. $colour = substr($RGB,$offset,2);
  269. if (!$hex)
  270. $colour = hexdec($colour);
  271. return $colour;
  272. }
  273. public static function getRed($RGB,$hex=true) {
  274. if (strlen($RGB) == 8) {
  275. return self::_getColourComponent($RGB,2,$hex);
  276. } elseif (strlen($RGB) == 6) {
  277. return self::_getColourComponent($RGB,0,$hex);
  278. }
  279. }
  280. public static function getGreen($RGB,$hex=true) {
  281. if (strlen($RGB) == 8) {
  282. return self::_getColourComponent($RGB,4,$hex);
  283. } elseif (strlen($RGB) == 6) {
  284. return self::_getColourComponent($RGB,2,$hex);
  285. }
  286. }
  287. public static function getBlue($RGB,$hex=true) {
  288. if (strlen($RGB) == 8) {
  289. return self::_getColourComponent($RGB,6,$hex);
  290. } elseif (strlen($RGB) == 6) {
  291. return self::_getColourComponent($RGB,4,$hex);
  292. }
  293. }
  294. /**
  295. * Adjust the brightness of a color
  296. *
  297. * @param string $hex The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  298. * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
  299. * @return string The adjusted colour as an RGB value (e.g. FF00CCCC or CCDDEE
  300. */
  301. public static function changeBrightness($hex, $adjustPercentage) {
  302. $red = self::getRed($hex,false);
  303. $green = self::getGreen($hex,false);
  304. $blue = self::getBlue($hex,false);
  305. if ($adjustPercentage > 0) {
  306. $red += (255 - $red) * $adjustPercentage;
  307. $green += (255 - $green) * $adjustPercentage;
  308. $blue += (255 - $blue) * $adjustPercentage;
  309. } else {
  310. $red += $red * $adjustPercentage;
  311. $green += $green * $adjustPercentage;
  312. $blue += $blue * $adjustPercentage;
  313. }
  314. if ($red < 0) $red = 0;
  315. elseif ($red > 255) $red = 255;
  316. if ($green < 0) $green = 0;
  317. elseif ($green > 255) $green = 255;
  318. if ($blue < 0) $blue = 0;
  319. elseif ($blue > 255) $blue = 255;
  320. return strtoupper( str_pad(dechex($red), 2, '0', 0) .
  321. str_pad(dechex($green), 2, '0', 0) .
  322. str_pad(dechex($blue), 2, '0', 0)
  323. );
  324. }
  325. /**
  326. * Get indexed color
  327. *
  328. * @param int $pIndex
  329. * @return PHPExcel_Style_Color
  330. */
  331. public static function indexedColor($pIndex, $background=false) {
  332. // Clean parameter
  333. $pIndex = intval($pIndex);
  334. // Indexed colors
  335. if (is_null(self::$_indexedColors)) {
  336. self::$_indexedColors = array();
  337. self::$_indexedColors[] = '00000000';
  338. self::$_indexedColors[] = '00FFFFFF';
  339. self::$_indexedColors[] = '00FF0000';
  340. self::$_indexedColors[] = '0000FF00';
  341. self::$_indexedColors[] = '000000FF';
  342. self::$_indexedColors[] = '00FFFF00';
  343. self::$_indexedColors[] = '00FF00FF';
  344. self::$_indexedColors[] = '0000FFFF';
  345. self::$_indexedColors[] = '00000000';
  346. self::$_indexedColors[] = '00FFFFFF';
  347. self::$_indexedColors[] = '00FF0000';
  348. self::$_indexedColors[] = '0000FF00';
  349. self::$_indexedColors[] = '000000FF';
  350. self::$_indexedColors[] = '00FFFF00';
  351. self::$_indexedColors[] = '00FF00FF';
  352. self::$_indexedColors[] = '0000FFFF';
  353. self::$_indexedColors[] = '00800000';
  354. self::$_indexedColors[] = '00008000';
  355. self::$_indexedColors[] = '00000080';
  356. self::$_indexedColors[] = '00808000';
  357. self::$_indexedColors[] = '00800080';
  358. self::$_indexedColors[] = '00008080';
  359. self::$_indexedColors[] = '00C0C0C0';
  360. self::$_indexedColors[] = '00808080';
  361. self::$_indexedColors[] = '009999FF';
  362. self::$_indexedColors[] = '00993366';
  363. self::$_indexedColors[] = '00FFFFCC';
  364. self::$_indexedColors[] = '00CCFFFF';
  365. self::$_indexedColors[] = '00660066';
  366. self::$_indexedColors[] = '00FF8080';
  367. self::$_indexedColors[] = '000066CC';
  368. self::$_indexedColors[] = '00CCCCFF';
  369. self::$_indexedColors[] = '00000080';
  370. self::$_indexedColors[] = '00FF00FF';
  371. self::$_indexedColors[] = '00FFFF00';
  372. self::$_indexedColors[] = '0000FFFF';
  373. self::$_indexedColors[] = '00800080';
  374. self::$_indexedColors[] = '00800000';
  375. self::$_indexedColors[] = '00008080';
  376. self::$_indexedColors[] = '000000FF';
  377. self::$_indexedColors[] = '0000CCFF';
  378. self::$_indexedColors[] = '00CCFFFF';
  379. self::$_indexedColors[] = '00CCFFCC';
  380. self::$_indexedColors[] = '00FFFF99';
  381. self::$_indexedColors[] = '0099CCFF';
  382. self::$_indexedColors[] = '00FF99CC';
  383. self::$_indexedColors[] = '00CC99FF';
  384. self::$_indexedColors[] = '00FFCC99';
  385. self::$_indexedColors[] = '003366FF';
  386. self::$_indexedColors[] = '0033CCCC';
  387. self::$_indexedColors[] = '0099CC00';
  388. self::$_indexedColors[] = '00FFCC00';
  389. self::$_indexedColors[] = '00FF9900';
  390. self::$_indexedColors[] = '00FF6600';
  391. self::$_indexedColors[] = '00666699';
  392. self::$_indexedColors[] = '00969696';
  393. self::$_indexedColors[] = '00003366';
  394. self::$_indexedColors[] = '00339966';
  395. self::$_indexedColors[] = '00003300';
  396. self::$_indexedColors[] = '00333300';
  397. self::$_indexedColors[] = '00993300';
  398. self::$_indexedColors[] = '00993366';
  399. self::$_indexedColors[] = '00333399';
  400. self::$_indexedColors[] = '00333333';
  401. }
  402. if (array_key_exists($pIndex, self::$_indexedColors)) {
  403. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  404. }
  405. if ($background) {
  406. return new PHPExcel_Style_Color('FFFFFFFF');
  407. }
  408. return new PHPExcel_Style_Color('FF000000');
  409. }
  410. /**
  411. * Get hash code
  412. *
  413. * @return string Hash code
  414. */
  415. public function getHashCode() {
  416. if ($this->_isSupervisor) {
  417. return $this->getSharedComponent()->getHashCode();
  418. }
  419. return md5(
  420. $this->_argb
  421. . __CLASS__
  422. );
  423. }
  424. /**
  425. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  426. */
  427. public function __clone() {
  428. $vars = get_object_vars($this);
  429. foreach ($vars as $key => $value) {
  430. if ((is_object($value)) && ($key != '_parent')) {
  431. $this->$key = clone $value;
  432. } else {
  433. $this->$key = $value;
  434. }
  435. }
  436. }
  437. }