Completed
Pull Request — develop (#583)
by
unknown
17:10
created

Color::setAlpha()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 10
cp 0.8
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3.072
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 487
    public function __construct($pARGB = self::COLOR_BLACK)
59
    {
60
        // Initialise values
61 487
        $this->argb            = $pARGB;
62 487
    }
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
     * Get RGB
107
     *
108
     * @return string
109
     */
110 3
    public function getRGB()
111
    {
112 3
        if (strlen($this->argb) == 6) {
113
            return $this->argb;
114
        } else {
115 3
            return substr($this->argb, 2);
116
        }
117
    }
118 3
119 3
    /**
120 3
     * Set RGB
121 3
     *
122 3
     * @param  string $pValue
123
     * @param  string $pAlpha
124
     * @return \PhpOffice\PhpPresentation\Style\Color
125
     */
126
    public function setRGB($pValue = '000000', $pAlpha = 'FF')
127
    {
128
        if ($pValue == '') {
129
            $pValue = '000000';
130 159
        }
131
        if ($pAlpha == '') {
132 159
            $pAlpha = 'FF';
133 1
        }
134
        $this->argb = $pAlpha . $pValue;
135 159
136
        return $this;
137
    }
138
139
    /**
140
     * Get hash code
141
     *
142
     * @return string Hash code
143
     */
144
    public function getHashCode()
145
    {
146 232
        return md5(
147
            $this->argb
148 232
            . __CLASS__
149 1
        );
150
    }
151 232
152
    /**
153
     * Get hash index
154 232
     *
155
     * Note that this index may vary during script execution! Only reliable moment is
156 232
     * while doing a write of a workbook and when changes are not allowed.
157
     *
158
     * @return string Hash index
159
     */
160
    public function getHashIndex()
161
    {
162
        return $this->hashIndex;
163
    }
164 123
165
    /**
166 123
     * Set hash index
167 123
     *
168 123
     * Note that this index may vary during script execution! Only reliable moment is
169
     * while doing a write of a workbook and when changes are not allowed.
170
     *
171
     * @param string $value Hash index
172
     */
173
    public function setHashIndex($value)
174
    {
175
        $this->hashIndex = $value;
176
    }
177
}
178