Failed Conditions
Push — develop ( a6bb49...7a4cbd )
by Adrien
36:15
created

Color::applyFromArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.7998
c 0
b 0
f 0
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();
1 ignored issue
show
Bug introduced by
The method getEndColor() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getEndColor();

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.

Loading history...
Bug introduced by
The method getSharedComponent() does not exist on PhpOffice\PhpSpreadsheet\Spreadsheet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
                return $this->parent->/** @scrutinizer ignore-call */ getSharedComponent()->getEndColor();

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.

Loading history...
68 1
            case 'color':
69
                return $this->parent->getSharedComponent()->getColor();
0 ignored issues
show
Bug introduced by
The method getColor() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getColor();

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.

Loading history...
70 1
            case 'startColor':
71 1
                return $this->parent->getSharedComponent()->getStartColor();
0 ignored issues
show
Bug introduced by
The method getStartColor() does not exist on PhpOffice\PhpSpreadsheet\Style\Style. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                return $this->parent->getSharedComponent()->/** @scrutinizer ignore-call */ getStartColor();

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.

Loading history...
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]);
1 ignored issue
show
Bug introduced by
The method getStyleArray() does not exist on PhpOffice\PhpSpreadsheet\Spreadsheet. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        return $this->parent->/** @scrutinizer ignore-call */ getStyleArray([$this->parentPropertyName => $array]);

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.

Loading history...
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) .
1 ignored issue
show
Bug introduced by
It seems like $red can also be of type double; however, parameter $number of dechex() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

292
            str_pad(dechex(/** @scrutinizer ignore-type */ $red), 2, '0', 0) .
Loading history...
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) {
0 ignored issues
show
introduced by
The condition self::indexedColors === null is always false. If self::indexedColors === null can have other possible types, add them to src/PhpSpreadsheet/Style/Color.php:22
Loading history...
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