PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPExcel/Style/Color.php

https://bitbucket.org/nfredricks/wp-employee-time
PHP | 510 lines | 269 code | 37 blank | 204 comment | 43 complexity | d629e5e491b67788b4a25f6b1a5d94ad MD5 | raw file
  1<?php
  2/**
  3 * PHPExcel
  4 *
  5 * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 25 * @version 1.7.8, 2012-10-12
 26 */
 27
 28
 29/**
 30 * PHPExcel_Style_Color
 31 *
 32 * @category   PHPExcel
 33 * @package	PHPExcel_Style
 34 * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 35 */
 36class 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	/**
 51	 * Indexed colors array
 52	 *
 53	 * @var array
 54	 */
 55	private static $_indexedColors;
 56
 57	/**
 58	 * ARGB - Alpha RGB
 59	 *
 60	 * @var string
 61	 */
 62	private $_argb	= NULL;
 63
 64	/**
 65	 * Supervisor?
 66	 *
 67	 * @var boolean
 68	 */
 69	private $_isSupervisor;
 70
 71	/**
 72	 * Parent. Only used for supervisor
 73	 *
 74	 * @var mixed
 75	 */
 76	private $_parent;
 77
 78	/**
 79	 * Parent property name
 80	 *
 81	 * @var string
 82	 */
 83	private $_parentPropertyName;
 84
 85
 86	/**
 87	 * Create a new PHPExcel_Style_Color
 88	 *
 89	 * @param	string	$pARGB			ARGB value for the colour
 90	 * @param	boolean	$isSupervisor	Flag indicating if this is a supervisor or not
 91	 *									Leave this value at default unless you understand exactly what
 92	 *										its ramifications are
 93	 * @param	boolean	$isConditional	Flag indicating if this is a conditional style or not
 94	 *									Leave this value at default unless you understand exactly what
 95	 *										its ramifications are
 96	 */
 97	public function __construct($pARGB = PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
 98	{
 99		//	Supervisor?
100		$this->_isSupervisor = $isSupervisor;
101
102		//	Initialise values
103		if (!$isConditional) {
104			$this->_argb = $pARGB;
105		}
106	}
107
108	/**
109	 * Bind parent. Only used for supervisor
110	 *
111	 * @param mixed $parent
112	 * @param string $parentPropertyName
113	 * @return PHPExcel_Style_Color
114	 */
115	public function bindParent($parent, $parentPropertyName)
116	{
117		$this->_parent = $parent;
118		$this->_parentPropertyName = $parentPropertyName;
119		return $this;
120	}
121
122	/**
123	 * Is this a supervisor or a real style component?
124	 *
125	 * @return boolean
126	 */
127	public function getIsSupervisor()
128	{
129		return $this->_isSupervisor;
130	}
131
132	/**
133	 * Get the shared style component for the currently active cell in currently active sheet.
134	 * Only used for style supervisor
135	 *
136	 * @return PHPExcel_Style_Color
137	 */
138	public function getSharedComponent()
139	{
140		switch ($this->_parentPropertyName) {
141			case '_endColor':
142				return $this->_parent->getSharedComponent()->getEndColor();		break;
143			case '_color':
144				return $this->_parent->getSharedComponent()->getColor();		break;
145			case '_startColor':
146				return $this->_parent->getSharedComponent()->getStartColor();	break;
147		}
148	}
149
150	/**
151	 * Get the currently active sheet. Only used for supervisor
152	 *
153	 * @return PHPExcel_Worksheet
154	 */
155	public function getActiveSheet()
156	{
157		return $this->_parent->getActiveSheet();
158	}
159
160	/**
161	 * Get the currently active cell coordinate in currently active sheet.
162	 * Only used for supervisor
163	 *
164	 * @return string E.g. 'A1'
165	 */
166	public function getSelectedCells()
167	{
168		return $this->getActiveSheet()->getSelectedCells();
169	}
170
171	/**
172	 * Get the currently active cell coordinate in currently active sheet.
173	 * Only used for supervisor
174	 *
175	 * @return string E.g. 'A1'
176	 */
177	public function getActiveCell()
178	{
179		return $this->getActiveSheet()->getActiveCell();
180	}
181
182	/**
183	 * Build style array from subcomponents
184	 *
185	 * @param array $array
186	 * @return array
187	 */
188	public function getStyleArray($array)
189	{
190		switch ($this->_parentPropertyName) {
191			case '_endColor':
192				$key = 'endcolor';
193				break;
194			case '_color':
195				$key = 'color';
196				break;
197			case '_startColor':
198				$key = 'startcolor';
199				break;
200
201		}
202		return $this->_parent->getStyleArray(array($key => $array));
203	}
204
205	/**
206	 * Apply styles from array
207	 *
208	 * <code>
209	 * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
210	 * </code>
211	 *
212	 * @param	array	$pStyles	Array containing style information
213	 * @throws	Exception
214	 * @return PHPExcel_Style_Color
215	 */
216	public function applyFromArray($pStyles = NULL) {
217		if (is_array($pStyles)) {
218			if ($this->_isSupervisor) {
219				$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
220			} else {
221				if (array_key_exists('rgb', $pStyles)) {
222					$this->setRGB($pStyles['rgb']);
223				}
224				if (array_key_exists('argb', $pStyles)) {
225					$this->setARGB($pStyles['argb']);
226				}
227			}
228		} else {
229			throw new Exception("Invalid style array passed.");
230		}
231		return $this;
232	}
233
234	/**
235	 * Get ARGB
236	 *
237	 * @return string
238	 */
239	public function getARGB() {
240		if ($this->_isSupervisor) {
241			return $this->getSharedComponent()->getARGB();
242		}
243		return $this->_argb;
244	}
245
246	/**
247	 * Set ARGB
248	 *
249	 * @param string $pValue
250	 * @return PHPExcel_Style_Color
251	 */
252	public function setARGB($pValue = PHPExcel_Style_Color::COLOR_BLACK) {
253		if ($pValue == '') {
254			$pValue = PHPExcel_Style_Color::COLOR_BLACK;
255		}
256		if ($this->_isSupervisor) {
257			$styleArray = $this->getStyleArray(array('argb' => $pValue));
258			$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
259		} else {
260			$this->_argb = $pValue;
261		}
262		return $this;
263	}
264
265	/**
266	 * Get RGB
267	 *
268	 * @return string
269	 */
270	public function getRGB() {
271		if ($this->_isSupervisor) {
272			return $this->getSharedComponent()->getRGB();
273		}
274		return substr($this->_argb, 2);
275	}
276
277	/**
278	 * Set RGB
279	 *
280	 * @param	string	$pValue	RGB value
281	 * @return PHPExcel_Style_Color
282	 */
283	public function setRGB($pValue = '000000') {
284		if ($pValue == '') {
285			$pValue = '000000';
286		}
287		if ($this->_isSupervisor) {
288			$styleArray = $this->getStyleArray(array('argb' => 'FF' . $pValue));
289			$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
290		} else {
291			$this->_argb = 'FF' . $pValue;
292		}
293		return $this;
294	}
295
296	/**
297	 * Get a specified colour component of an RGB value
298	 *
299	 * @private
300	 * @param	string		$RGB		The colour as an RGB value (e.g. FF00CCCC or CCDDEE
301	 * @param	int			$offset		Position within the RGB value to extract
302	 * @param	boolean		$hex		Flag indicating whether the component should be returned as a hex or a
303	 *									decimal value
304	 * @return	string		The extracted colour component
305	 */
306	private static function _getColourComponent($RGB,$offset,$hex=TRUE) {
307		$colour = substr($RGB, $offset, 2);
308		if (!$hex)
309			$colour = hexdec($colour);
310		return $colour;
311	}
312
313	/**
314	 * Get the red colour component of an RGB value
315	 *
316	 * @param	string		$RGB		The colour as an RGB value (e.g. FF00CCCC or CCDDEE
317	 * @param	boolean		$hex		Flag indicating whether the component should be returned as a hex or a
318	 *									decimal value
319	 * @return	string		The red colour component
320	 */
321	public static function getRed($RGB,$hex=TRUE) {
322		if (strlen($RGB) == 8) {
323			return self::_getColourComponent($RGB, 2, $hex);
324		} elseif (strlen($RGB) == 6) {
325			return self::_getColourComponent($RGB, 0, $hex);
326		}
327	}
328
329	/**
330	 * Get the green colour component of an RGB value
331	 *
332	 * @param	string		$RGB		The colour as an RGB value (e.g. FF00CCCC or CCDDEE
333	 * @param	boolean		$hex		Flag indicating whether the component should be returned as a hex or a
334	 *									decimal value
335	 * @return	string		The green colour component
336	 */
337	public static function getGreen($RGB,$hex=TRUE) {
338		if (strlen($RGB) == 8) {
339			return self::_getColourComponent($RGB, 4, $hex);
340		} elseif (strlen($RGB) == 6) {
341			return self::_getColourComponent($RGB, 2, $hex);
342		}
343	}
344
345	/**
346	 * Get the blue colour component of an RGB value
347	 *
348	 * @param	string		$RGB		The colour as an RGB value (e.g. FF00CCCC or CCDDEE
349	 * @param	boolean		$hex		Flag indicating whether the component should be returned as a hex or a
350	 *									decimal value
351	 * @return	string		The blue colour component
352	 */
353	public static function getBlue($RGB,$hex=TRUE) {
354		if (strlen($RGB) == 8) {
355			return self::_getColourComponent($RGB, 6, $hex);
356		} elseif (strlen($RGB) == 6) {
357			return self::_getColourComponent($RGB, 4, $hex);
358		}
359	}
360
361	/**
362	 * Adjust the brightness of a color
363	 *
364	 * @param	string		$hex	The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
365	 * @param	float		$adjustPercentage	The percentage by which to adjust the colour as a float from -1 to 1
366	 * @return	string		The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
367	 */
368	public static function changeBrightness($hex, $adjustPercentage) {
369		$rgba = (strlen($hex) == 8);
370
371		$red	= self::getRed($hex, FALSE);
372		$green	= self::getGreen($hex, FALSE);
373		$blue	= self::getBlue($hex, FALSE);
374		if ($adjustPercentage > 0) {
375			$red	+= (255 - $red) * $adjustPercentage;
376			$green	+= (255 - $green) * $adjustPercentage;
377			$blue	+= (255 - $blue) * $adjustPercentage;
378		} else {
379			$red	+= $red * $adjustPercentage;
380			$green	+= $green * $adjustPercentage;
381			$blue	+= $blue * $adjustPercentage;
382		}
383
384		if ($red < 0) $red = 0;
385		elseif ($red > 255) $red = 255;
386		if ($green < 0) $green = 0;
387		elseif ($green > 255) $green = 255;
388		if ($blue < 0) $blue = 0;
389		elseif ($blue > 255) $blue = 255;
390
391		$rgb = strtoupper(	str_pad(dechex($red), 2, '0', 0) .
392							str_pad(dechex($green), 2, '0', 0) .
393							str_pad(dechex($blue), 2, '0', 0)
394						 );
395		return (($rgba) ? 'FF' : '') . $rgb;
396	}
397
398	/**
399	 * Get indexed color
400	 *
401	 * @param	int			$pIndex			Index entry point into the colour array
402	 * @param	boolean		$background		Flag to indicate whether default background or foreground colour
403	 *											should be returned if the indexed colour doesn't exist
404	 * @return	PHPExcel_Style_Color
405	 */
406	public static function indexedColor($pIndex, $background=FALSE) {
407		// Clean parameter
408		$pIndex = intval($pIndex);
409
410		// Indexed colors
411		if (is_null(self::$_indexedColors)) {
412			self::$_indexedColors = array(
413					1	=> 'FF000000',	//	System Colour #1 - Black
414					2	=> 'FFFFFFFF',	//	System Colour #2 - White
415					3	=> 'FFFF0000',	//	System Colour #3 - Red
416					4	=> 'FF00FF00',	//	System Colour #4 - Green
417					5	=> 'FF0000FF',	//	System Colour #5 - Blue
418					6	=> 'FFFFFF00',	//	System Colour #6 - Yellow
419					7	=> 'FFFF00FF',	//	System Colour #7- Magenta
420					8	=> 'FF00FFFF',	//	System Colour #8- Cyan
421					9	=> 'FF800000',	//	Standard Colour #9
422					10	=> 'FF008000',	//	Standard Colour #10
423					11	=> 'FF000080',	//	Standard Colour #11
424					12	=> 'FF808000',	//	Standard Colour #12
425					13	=> 'FF800080',	//	Standard Colour #13
426					14	=> 'FF008080',	//	Standard Colour #14
427					15	=> 'FFC0C0C0',	//	Standard Colour #15
428					16	=> 'FF808080',	//	Standard Colour #16
429					17	=> 'FF9999FF',	//	Chart Fill Colour #17
430					18	=> 'FF993366',	//	Chart Fill Colour #18
431					19	=> 'FFFFFFCC',	//	Chart Fill Colour #19
432					20	=> 'FFCCFFFF',	//	Chart Fill Colour #20
433					21	=> 'FF660066',	//	Chart Fill Colour #21
434					22	=> 'FFFF8080',	//	Chart Fill Colour #22
435					23	=> 'FF0066CC',	//	Chart Fill Colour #23
436					24	=> 'FFCCCCFF',	//	Chart Fill Colour #24
437					25	=> 'FF000080',	//	Chart Line Colour #25
438					26	=> 'FFFF00FF',	//	Chart Line Colour #26
439					27	=> 'FFFFFF00',	//	Chart Line Colour #27
440					28	=> 'FF00FFFF',	//	Chart Line Colour #28
441					29	=> 'FF800080',	//	Chart Line Colour #29
442					30	=> 'FF800000',	//	Chart Line Colour #30
443					31	=> 'FF008080',	//	Chart Line Colour #31
444					32	=> 'FF0000FF',	//	Chart Line Colour #32
445					33	=> 'FF00CCFF',	//	Standard Colour #33
446					34	=> 'FFCCFFFF',	//	Standard Colour #34
447					35	=> 'FFCCFFCC',	//	Standard Colour #35
448					36	=> 'FFFFFF99',	//	Standard Colour #36
449					37	=> 'FF99CCFF',	//	Standard Colour #37
450					38	=> 'FFFF99CC',	//	Standard Colour #38
451					39	=> 'FFCC99FF',	//	Standard Colour #39
452					40	=> 'FFFFCC99',	//	Standard Colour #40
453					41	=> 'FF3366FF',	//	Standard Colour #41
454					42	=> 'FF33CCCC',	//	Standard Colour #42
455					43	=> 'FF99CC00',	//	Standard Colour #43
456					44	=> 'FFFFCC00',	//	Standard Colour #44
457					45	=> 'FFFF9900',	//	Standard Colour #45
458					46	=> 'FFFF6600',	//	Standard Colour #46
459					47	=> 'FF666699',	//	Standard Colour #47
460					48	=> 'FF969696',	//	Standard Colour #48
461					49	=> 'FF003366',	//	Standard Colour #49
462					50	=> 'FF339966',	//	Standard Colour #50
463					51	=> 'FF003300',	//	Standard Colour #51
464					52	=> 'FF333300',	//	Standard Colour #52
465					53	=> 'FF993300',	//	Standard Colour #53
466					54	=> 'FF993366',	//	Standard Colour #54
467					55	=> 'FF333399',	//	Standard Colour #55
468					56	=> 'FF333333'	//	Standard Colour #56
469				);
470		}
471
472		if (array_key_exists($pIndex, self::$_indexedColors)) {
473			return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
474		}
475
476		if ($background) {
477			return new PHPExcel_Style_Color('FFFFFFFF');
478		}
479		return new PHPExcel_Style_Color('FF000000');
480	}
481
482	/**
483	 * Get hash code
484	 *
485	 * @return string	Hash code
486	 */
487	public function getHashCode() {
488		if ($this->_isSupervisor) {
489			return $this->getSharedComponent()->getHashCode();
490		}
491		return md5(
492			  $this->_argb
493			. __CLASS__
494		);
495	}
496
497	/**
498	 * Implement PHP __clone to create a deep clone, not just a shallow copy.
499	 */
500	public function __clone() {
501		$vars = get_object_vars($this);
502		foreach ($vars as $key => $value) {
503			if ((is_object($value)) && ($key != '_parent')) {
504				$this->$key = clone $value;
505			} else {
506				$this->$key = $value;
507			}
508		}
509	}
510}