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