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