File Source for Color.php

PHPExcel_Style

# Source for file Color.php

Documentation is available at Color.php

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. */
29. /**
30. * PHPExcel_Style_Color
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Style
34. * @copyright Copyright (c) 2006 - 2011 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';
50. /**
51. * Indexed colors array
52. *
53. * @var array
54. */
55. private static $_indexedColors;
57. /**
58. * ARGB - Alpha RGB
59. *
60. * @var string
61. */
62. private $_argb;
64. /**
65. * Supervisor?
66. *
67. * @var boolean
68. */
69. private $_isSupervisor;
71. /**
72. * Parent. Only used for supervisor
73. *
74. * @var mixed
75. */
76. private $_parent;
78. /**
79. * Parent property name
80. *
81. * @var string
82. */
83. private $_parentPropertyName;
85. /**
86. * Create a new PHPExcel_Style_Color
87. *
88. * @param string $pARGB
89. */
90. public function __construct($pARGB = PHPExcel_Style_Color:: COLOR_BLACK, $isSupervisor = false)
91. {
92. // Supervisor?
93. $this->_isSupervisor = $isSupervisor;
95. // Initialise values
96. $this->_argb = $pARGB;
97. }
99. /**
100. * Bind parent. Only used for supervisor
101. *
102. * @param mixed $parent
103. * @param string $parentPropertyName
104. * @return PHPExcel_Style_Color
105. */
106. public function bindParent($parent, $parentPropertyName)
107. {
108. $this->_parent = $parent;
109. $this->_parentPropertyName = $parentPropertyName;
110. return $this;
111. }
113. /**
114. * Is this a supervisor or a real style component?
115. *
116. * @return boolean
117. */
118. public function getIsSupervisor()
119. {
120. return $this->_isSupervisor;
121. }
123. /**
124. * Get the shared style component for the currently active cell in currently active sheet.
125. * Only used for style supervisor
126. *
127. * @return PHPExcel_Style_Color
128. */
129. public function getSharedComponent()
130. {
131. switch ($this->_parentPropertyName) {
132. case '_endColor':
133. return $this->_parent->getSharedComponent()->getEndColor();
134. break;
136. case '_color':
137. return $this->_parent->getSharedComponent()->getColor();
138. break;
140. case '_startColor':
141. return $this->_parent->getSharedComponent()->getStartColor();
142. break;
143. }
144. }
146. /**
147. * Get the currently active sheet. Only used for supervisor
148. *
149. * @return PHPExcel_Worksheet
150. */
151. public function getActiveSheet()
152. {
153. return $this->_parent->getActiveSheet();
154. }
156. /**
157. * Get the currently active cell coordinate in currently active sheet.
158. * Only used for supervisor
159. *
160. * @return string E.g. 'A1'
161. */
162. public function getSelectedCells()
163. {
164. return $this-> getActiveSheet()->getSelectedCells();
165. }
167. /**
168. * Get the currently active cell coordinate in currently active sheet.
169. * Only used for supervisor
170. *
171. * @return string E.g. 'A1'
172. */
173. public function getActiveCell()
174. {
175. return $this-> getActiveSheet()->getActiveCell();
176. }
178. /**
179. * Build style array from subcomponents
180. *
181. * @param array $array
182. * @return array
183. */
184. public function getStyleArray($array)
185. {
186. switch ($this->_parentPropertyName) {
187. case '_endColor':
188. $key = 'endcolor';
189. break;
191. case '_color':
192. $key = 'color';
193. break;
195. case '_startColor':
196. $key = 'startcolor';
197. break;
199. }
200. return $this->_parent->getStyleArray(array($key => $array));
201. }
203. /**
204. * Apply styles from array
205. *
206. * <code>
207. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
208. * </code>
209. *
210. * @param array $pStyles Array containing style information
211. * @throws Exception
212. * @return PHPExcel_Style_Color
213. */
214. public function applyFromArray($pStyles = null) {
215. if ( is_array($pStyles)) {
216. if ($this->_isSupervisor) {
217. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($this-> getStyleArray($pStyles));
218. } else {
219. if ( array_key_exists('rgb', $pStyles)) {
220. $this-> setRGB($pStyles['rgb']);
221. }
222. if ( array_key_exists('argb', $pStyles)) {
223. $this-> setARGB($pStyles['argb']);
224. }
225. }
226. } else {
227. throw new Exception("Invalid style array passed.");
228. }
229. return $this;
230. }
232. /**
233. * Get ARGB
234. *
235. * @return string
236. */
237. public function getARGB() {
238. if ($this->_isSupervisor) {
239. return $this-> getSharedComponent()->getARGB();
240. }
241. return $this->_argb;
242. }
244. /**
245. * Set ARGB
246. *
247. * @param string $pValue
248. * @return PHPExcel_Style_Color
249. */
250. public function setARGB($pValue = PHPExcel_Style_Color:: COLOR_BLACK) {
251. if ($pValue == '') {
252. $pValue = PHPExcel_Style_Color:: COLOR_BLACK;
253. }
254. if ($this->_isSupervisor) {
255. $styleArray = $this-> getStyleArray(array('argb' => $pValue));
256. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($styleArray);
257. } else {
258. $this->_argb = $pValue;
259. }
260. return $this;
261. }
263. /**
264. * Get RGB
265. *
266. * @return string
267. */
268. public function getRGB() {
269. if ($this->_isSupervisor) {
270. return $this-> getSharedComponent()->getRGB();
271. }
272. return substr($this->_argb, 2);
273. }
275. /**
276. * Set RGB
277. *
278. * @param string $pValue
279. * @return PHPExcel_Style_Color
280. */
281. public function setRGB($pValue = '000000') {
282. if ($pValue == '') {
283. $pValue = '000000';
284. }
285. if ($this->_isSupervisor) {
286. $styleArray = $this-> getStyleArray(array('argb' => 'FF' . $pValue));
287. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($styleArray);
288. } else {
289. $this->_argb = 'FF' . $pValue;
290. }
291. return $this;
292. }
294. private static function _getColourComponent($RGB,$offset,$hex=true) {
295. $colour = substr($RGB,$offset,2);
296. if (!$hex)
297. $colour = hexdec($colour);
298. return $colour;
299. }
301. public static function getRed($RGB,$hex=true) {
302. if ( strlen($RGB) == 8) {
303. return self::_getColourComponent($RGB,2,$hex);
304. } elseif ( strlen($RGB) == 6) {
305. return self::_getColourComponent($RGB,0,$hex);
306. }
307. }
309. public static function getGreen($RGB,$hex=true) {
310. if ( strlen($RGB) == 8) {
311. return self::_getColourComponent($RGB,4,$hex);
312. } elseif ( strlen($RGB) == 6) {
313. return self::_getColourComponent($RGB,2,$hex);
314. }
315. }
317. public static function getBlue($RGB,$hex=true) {
318. if ( strlen($RGB) == 8) {
319. return self::_getColourComponent($RGB,6,$hex);
320. } elseif ( strlen($RGB) == 6) {
321. return self::_getColourComponent($RGB,4,$hex);
322. }
323. }
325. /**
326. * Adjust the brightness of a color
327. *
328. * @param string $hex The colour as an RGB value (e.g. FF00CCCC or CCDDEE
329. * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
330. * @return string The adjusted colour as an RGB value (e.g. FF00CCCC or CCDDEE
331. */
332. public static function changeBrightness($hex, $adjustPercentage) {
333. $red = self:: getRed($hex,false);
334. $green = self:: getGreen($hex,false);
335. $blue = self:: getBlue($hex,false);
336. if ($adjustPercentage > 0) {
337. $red += (255 - $red) * $adjustPercentage;
338. $green += (255 - $green) * $adjustPercentage;
339. $blue += (255 - $blue) * $adjustPercentage;
340. } else {
341. $red += $red * $adjustPercentage;
342. $green += $green * $adjustPercentage;
343. $blue += $blue * $adjustPercentage;
344. }
346. if ($red < 0) $red = 0;
347. elseif ($red > 255) $red = 255;
348. if ($green < 0) $green = 0;
349. elseif ($green > 255) $green = 255;
350. if ($blue < 0) $blue = 0;
351. elseif ($blue > 255) $blue = 255;
353. return strtoupper( str_pad( dechex($red), 2, '0', 0) .
354. str_pad( dechex($green), 2, '0', 0) .
355. str_pad( dechex($blue), 2, '0', 0)
356. );
357. }
359. /**
360. * Get indexed color
361. *
362. * @param int $pIndex
363. * @return PHPExcel_Style_Color
364. */
365. public static function indexedColor($pIndex, $background=false) {
366. // Clean parameter
367. $pIndex = intval($pIndex);
369. // Indexed colors
370. if ( is_null(self::$_indexedColors)) {
371. self::$_indexedColors = array();
372. self::$_indexedColors[] = '00000000';
373. self::$_indexedColors[] = '00FFFFFF';
374. self::$_indexedColors[] = '00FF0000';
375. self::$_indexedColors[] = '0000FF00';
376. self::$_indexedColors[] = '000000FF';
377. self::$_indexedColors[] = '00FFFF00';
378. self::$_indexedColors[] = '00FF00FF';
379. self::$_indexedColors[] = '0000FFFF';
380. self::$_indexedColors[] = '00000000';
381. self::$_indexedColors[] = '00FFFFFF';
382. self::$_indexedColors[] = '00FF0000';
383. self::$_indexedColors[] = '0000FF00';
384. self::$_indexedColors[] = '000000FF';
385. self::$_indexedColors[] = '00FFFF00';
386. self::$_indexedColors[] = '00FF00FF';
387. self::$_indexedColors[] = '0000FFFF';
388. self::$_indexedColors[] = '00800000';
389. self::$_indexedColors[] = '00008000';
390. self::$_indexedColors[] = '00000080';
391. self::$_indexedColors[] = '00808000';
392. self::$_indexedColors[] = '00800080';
393. self::$_indexedColors[] = '00008080';
394. self::$_indexedColors[] = '00C0C0C0';
395. self::$_indexedColors[] = '00808080';
396. self::$_indexedColors[] = '009999FF';
397. self::$_indexedColors[] = '00993366';
398. self::$_indexedColors[] = '00FFFFCC';
399. self::$_indexedColors[] = '00CCFFFF';
400. self::$_indexedColors[] = '00660066';
401. self::$_indexedColors[] = '00FF8080';
402. self::$_indexedColors[] = '000066CC';
403. self::$_indexedColors[] = '00CCCCFF';
404. self::$_indexedColors[] = '00000080';
405. self::$_indexedColors[] = '00FF00FF';
406. self::$_indexedColors[] = '00FFFF00';
407. self::$_indexedColors[] = '0000FFFF';
408. self::$_indexedColors[] = '00800080';
409. self::$_indexedColors[] = '00800000';
410. self::$_indexedColors[] = '00008080';
411. self::$_indexedColors[] = '000000FF';
412. self::$_indexedColors[] = '0000CCFF';
413. self::$_indexedColors[] = '00CCFFFF';
414. self::$_indexedColors[] = '00CCFFCC';
415. self::$_indexedColors[] = '00FFFF99';
416. self::$_indexedColors[] = '0099CCFF';
417. self::$_indexedColors[] = '00FF99CC';
418. self::$_indexedColors[] = '00CC99FF';
419. self::$_indexedColors[] = '00FFCC99';
420. self::$_indexedColors[] = '003366FF';
421. self::$_indexedColors[] = '0033CCCC';
422. self::$_indexedColors[] = '0099CC00';
423. self::$_indexedColors[] = '00FFCC00';
424. self::$_indexedColors[] = '00FF9900';
425. self::$_indexedColors[] = '00FF6600';
426. self::$_indexedColors[] = '00666699';
427. self::$_indexedColors[] = '00969696';
428. self::$_indexedColors[] = '00003366';
429. self::$_indexedColors[] = '00339966';
430. self::$_indexedColors[] = '00003300';
431. self::$_indexedColors[] = '00333300';
432. self::$_indexedColors[] = '00993300';
433. self::$_indexedColors[] = '00993366';
434. self::$_indexedColors[] = '00333399';
435. self::$_indexedColors[] = '00333333';
436. }
438. if (array_key_exists($pIndex, self::$_indexedColors)) {
439. return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
440. }
442. if ($background) {
443. return new PHPExcel_Style_Color('FFFFFFFF');
444. }
445. return new PHPExcel_Style_Color('FF000000');
446. }
448. /**
449. * Get hash code
450. *
451. * @return string Hash code
452. */
453. public function getHashCode() {
454. if ($this->_isSupervisor) {
455. return $this-> getSharedComponent()->getHashCode();
456. }
457. return md5(
458. $this->_argb
459. . __CLASS__
460. );
461. }
463. /**
464. * Implement PHP __clone to create a deep clone, not just a shallow copy.
465. */
466. public function __clone() {
467. $vars = get_object_vars($this);
468. foreach ($vars as $key => $value) {
469. if (( is_object($value)) && ($key != '_parent')) {
470. $this->$key = clone $value;
471. } else {
472. $this->$key = $value;
473. }
474. }
475. }
476. }

Documentation generated on Sun, 27 Feb 2011 16:29:06 -0800 by phpDocumentor 1.4.3