Completed
Push — develop ( 7bd91a...e67d8e )
by Franck
14s
created

Color::setAlpha()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3
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
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Style;
19
20
use PhpOffice\PhpPresentation\ComparableInterface;
21
22
/**
23
 * \PhpOffice\PhpPresentation\Style\Color
24
 */
25
class Color implements ComparableInterface
26
{
27
    /* Colors */
28
    const COLOR_BLACK                       = 'FF000000';
29
    const COLOR_WHITE                       = 'FFFFFFFF';
30
    const COLOR_RED                         = 'FFFF0000';
31
    const COLOR_DARKRED                     = 'FF800000';
32
    const COLOR_BLUE                        = 'FF0000FF';
33
    const COLOR_DARKBLUE                    = 'FF000080';
34
    const COLOR_GREEN                       = 'FF00FF00';
35
    const COLOR_DARKGREEN                   = 'FF008000';
36
    const COLOR_YELLOW                      = 'FFFFFF00';
37
    const COLOR_DARKYELLOW                  = 'FF808000';
38
39
    /**
40
     * ARGB - Alpha RGB
41
     *
42
     * @var string
43
     */
44
    private $argb;
45
46
    /**
47
     * Hash index
48
     *
49
     * @var string
50
     */
51
    private $hashIndex;
52
53
    /**
54
     * Create a new \PhpOffice\PhpPresentation\Style\Color
55
     *
56
     * @param string $pARGB
57
     */
58 481
    public function __construct($pARGB = self::COLOR_BLACK)
59
    {
60
        // Initialise values
61 481
        $this->argb            = $pARGB;
62 481
    }
63
64
    /**
65
     * Get ARGB
66
     *
67
     * @return string
68
     */
69 15
    public function getARGB()
70
    {
71 15
        return $this->argb;
72
    }
73
74
    /**
75
     * Set ARGB
76
     *
77
     * @param  string                    $pValue
78
     * @return \PhpOffice\PhpPresentation\Style\Color
79
     */
80 3
    public function setARGB($pValue = self::COLOR_BLACK)
81
    {
82 3
        if ($pValue == '') {
83 1
            $pValue = self::COLOR_BLACK;
84
        }
85 3
        $this->argb = $pValue;
86
87 3
        return $this;
88
    }
89
90
    /**
91
     * Get the alpha % of the ARGB
92
     * Will return 100 if no ARGB
93
     * @return integer
94
     */
95 71
    public function getAlpha()
96
    {
97 71
        $alpha = 100;
98 71
        if (strlen($this->argb) >= 6) {
99 71
            $dec = hexdec(substr($this->argb, 0, 2));
100 71
            $alpha = number_format(($dec/255) * 100, 2);
101
        }
102 71
        return $alpha;
103
    }
104
105
    /**
106
     * Set the alpha % of the ARGB
107
     * @param int $alpha
108
     * @return $this
109
     */
110 156
    public function setAlpha($alpha = 100)
111
    {
112 156
        if ($alpha < 0) {
113 1
            $alpha = 0;
114
        }
115 156
        if ($alpha > 100) {
116
            $alpha = 100;
117
        }
118
        $alpha = round(($alpha / 100) * 255);
119
        $alpha = dechex($alpha);
120
        $alpha = str_pad($alpha, 2, '0', STR_PAD_LEFT);
121
        $this->argb = $alpha . substr($this->argb, 2);
122
        return $this;
123
    }
124
125
    /**
126 230
     * Get RGB
127
     *
128 230
     * @return string
129 1
     */
130
    public function getRGB()
131 230
    {
132
        if (strlen($this->argb) == 6) {
133
            return $this->argb;
134 230
        } else {
135
            return substr($this->argb, 2);
136 230
        }
137
    }
138
139
    /**
140
     * Set RGB
141
     *
142
     * @param  string $pValue
143
     * @param  string $pAlpha
144 118
     * @return \PhpOffice\PhpPresentation\Style\Color
145
     */
146 118
    public function setRGB($pValue = '000000', $pAlpha = 'FF')
147 118
    {
148 118
        if ($pValue == '') {
149
            $pValue = '000000';
150
        }
151
        if ($pAlpha == '') {
152
            $pAlpha = 'FF';
153
        }
154
        $this->argb = $pAlpha . $pValue;
155
156
        return $this;
157
    }
158
159
    /**
160 1
     * Get hash code
161
     *
162 1
     * @return string Hash code
163
     */
164
    public function getHashCode()
165
    {
166
        return md5(
167
            $this->argb
168
            . __CLASS__
169
        );
170
    }
171
172
    /**
173 1
     * Get hash index
174
     *
175 1
     * Note that this index may vary during script execution! Only reliable moment is
176 1
     * while doing a write of a workbook and when changes are not allowed.
177
     *
178
     * @return string Hash index
179
     */
180
    public function getHashIndex()
181
    {
182
        return $this->hashIndex;
183
    }
184
185
    /**
186
     * Set hash index
187
     *
188
     * Note that this index may vary during script execution! Only reliable moment is
189
     * while doing a write of a workbook and when changes are not allowed.
190
     *
191
     * @param string $value Hash index
192
     */
193
    public function setHashIndex($value)
194
    {
195
        $this->hashIndex = $value;
196
    }
197
}
198