Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
33 / 36
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Color
91.67% covered (success)
91.67%
33 / 36
80.00% covered (warning)
80.00%
8 / 10
17.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getARGB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setARGB
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getAlpha
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setAlpha
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
3.10
 getRGB
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setRGB
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 getHashCode
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getHashIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHashIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Style;
21
22use PhpOffice\PhpPresentation\ComparableInterface;
23
24/**
25 * \PhpOffice\PhpPresentation\Style\Color.
26 */
27class Color implements ComparableInterface
28{
29    // Colors
30    public const COLOR_BLACK = 'FF000000';
31    public const COLOR_WHITE = 'FFFFFFFF';
32    public const COLOR_RED = 'FFFF0000';
33    public const COLOR_DARKRED = 'FF800000';
34    public const COLOR_BLUE = 'FF0000FF';
35    public const COLOR_DARKBLUE = 'FF000080';
36    public const COLOR_GREEN = 'FF00FF00';
37    public const COLOR_DARKGREEN = 'FF008000';
38    public const COLOR_YELLOW = 'FFFFFF00';
39    public const COLOR_DARKYELLOW = 'FF808000';
40
41    /**
42     * ARGB - Alpha RGB.
43     *
44     * @var string
45     */
46    private $argb;
47
48    /**
49     * Hash index.
50     *
51     * @var int
52     */
53    private $hashIndex;
54
55    /**
56     * Create a new \PhpOffice\PhpPresentation\Style\Color.
57     *
58     * @param string $pARGB
59     */
60    public function __construct($pARGB = self::COLOR_BLACK)
61    {
62        // Initialise values
63        $this->argb = $pARGB;
64    }
65
66    /**
67     * Get ARGB.
68     *
69     * @return string
70     */
71    public function getARGB()
72    {
73        return $this->argb;
74    }
75
76    /**
77     * Set ARGB.
78     *
79     * @param string $pValue
80     *
81     * @return Color
82     */
83    public function setARGB($pValue = self::COLOR_BLACK)
84    {
85        if ('' == $pValue) {
86            $pValue = self::COLOR_BLACK;
87        }
88        $this->argb = $pValue;
89
90        return $this;
91    }
92
93    /**
94     * Get the alpha % of the ARGB
95     * Will return 100 if no ARGB.
96     */
97    public function getAlpha(): int
98    {
99        $alpha = 100;
100        if (strlen($this->argb) >= 6) {
101            $dec = hexdec(substr($this->argb, 0, 2));
102            $alpha = (int) number_format(($dec / 255) * 100, 0);
103        }
104
105        return $alpha;
106    }
107
108    /**
109     * Set the alpha % of the ARGB.
110     *
111     * @return $this
112     */
113    public function setAlpha(int $alpha = 100): self
114    {
115        if ($alpha < 0) {
116            $alpha = 0;
117        }
118        if ($alpha > 100) {
119            $alpha = 100;
120        }
121        $alpha = round(($alpha / 100) * 255);
122        $alpha = dechex((int) $alpha);
123        $alpha = str_pad($alpha, 2, '0', STR_PAD_LEFT);
124        $this->argb = $alpha . substr($this->argb, 2);
125
126        return $this;
127    }
128
129    /**
130     * Get RGB.
131     *
132     * @return string
133     */
134    public function getRGB()
135    {
136        if (6 == strlen($this->argb)) {
137            return $this->argb;
138        }
139
140        return substr($this->argb, 2);
141    }
142
143    /**
144     * Set RGB.
145     *
146     * @param string $pValue
147     * @param string $pAlpha
148     *
149     * @return Color
150     */
151    public function setRGB($pValue = '000000', $pAlpha = 'FF')
152    {
153        if ('' == $pValue) {
154            $pValue = '000000';
155        }
156        if ('' == $pAlpha) {
157            $pAlpha = 'FF';
158        }
159        $this->argb = $pAlpha . $pValue;
160
161        return $this;
162    }
163
164    /**
165     * Get hash code.
166     *
167     * @return string Hash code
168     */
169    public function getHashCode(): string
170    {
171        return md5(
172            $this->argb
173            . __CLASS__
174        );
175    }
176
177    /**
178     * Get hash index.
179     *
180     * Note that this index may vary during script execution! Only reliable moment is
181     * while doing a write of a workbook and when changes are not allowed.
182     *
183     * @return null|int Hash index
184     */
185    public function getHashIndex(): ?int
186    {
187        return $this->hashIndex;
188    }
189
190    /**
191     * Set hash index.
192     *
193     * Note that this index may vary during script execution! Only reliable moment is
194     * while doing a write of a workbook and when changes are not allowed.
195     *
196     * @param int $value Hash index
197     *
198     * @return $this
199     */
200    public function setHashIndex(int $value)
201    {
202        $this->hashIndex = $value;
203
204        return $this;
205    }
206}