Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
16 / 16 |
CRAP | |
100.00% |
1 / 1 |
Grayscale | |
100.00% |
36 / 36 |
|
100.00% |
16 / 16 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setGray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
getGray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toCmyk | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toRgb | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
render | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__unset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetGet | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
offsetSet | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
offsetUnset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (https://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@noladev.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. |
8 | * @license https://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Color\Color; |
15 | |
16 | use OutOfRangeException; |
17 | |
18 | /** |
19 | * Image gray color class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Image |
23 | * @author Nick Sagona, III <dev@noladev.com> |
24 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. |
25 | * @license https://www.popphp.org/license New BSD License |
26 | * @version 1.0.2 |
27 | */ |
28 | class Grayscale extends AbstractColor implements \ArrayAccess |
29 | { |
30 | |
31 | /** |
32 | * Gray |
33 | * @var float |
34 | */ |
35 | protected float $gray = 0; |
36 | |
37 | /** |
38 | * Constructor |
39 | * |
40 | * Instantiate a PERCENT Gray Color object |
41 | * |
42 | * @param mixed $gray 0 - 100 |
43 | */ |
44 | public function __construct(mixed $gray) |
45 | { |
46 | $this->setGray($gray); |
47 | } |
48 | |
49 | /** |
50 | * Set the gray value |
51 | * |
52 | * @param mixed $gray |
53 | * @throws OutOfRangeException |
54 | * @return Grayscale |
55 | */ |
56 | public function setGray(mixed $gray): Grayscale |
57 | { |
58 | $gray = (float)$gray; |
59 | if (((int)$gray < 0) || ((int)$gray > 100)) { |
60 | throw new OutOfRangeException('Error: The value must be between 0 and 100'); |
61 | } else if ($gray < 1) { |
62 | $gray = (float)($gray * 100); |
63 | } |
64 | $this->gray = $gray; |
65 | return $this; |
66 | } |
67 | |
68 | /** |
69 | * Get the gray value |
70 | * |
71 | * @return float |
72 | */ |
73 | public function getGray(): float |
74 | { |
75 | return $this->gray; |
76 | } |
77 | |
78 | /** |
79 | * Convert to CMYK |
80 | * |
81 | * @return Cmyk |
82 | */ |
83 | public function toCmyk(): Cmyk |
84 | { |
85 | return new Cmyk(0, 0, 0, $this->gray); |
86 | } |
87 | |
88 | /** |
89 | * Convert to RGB |
90 | * |
91 | * @return Rgb |
92 | */ |
93 | public function toRgb(): Rgb |
94 | { |
95 | return new Rgb($this->gray, $this->gray, $this->gray); |
96 | } |
97 | |
98 | /** |
99 | * Convert to array |
100 | * |
101 | * @param bool $assoc |
102 | * @return array |
103 | */ |
104 | public function toArray(bool $assoc = true): array |
105 | { |
106 | $gray = []; |
107 | |
108 | if ($assoc) { |
109 | $gray['gray'] = $this->gray; |
110 | } else { |
111 | $gray[] = $this->gray; |
112 | } |
113 | |
114 | return $gray; |
115 | } |
116 | |
117 | /** |
118 | * Convert to readable string |
119 | * |
120 | * @param ?string $format |
121 | * @return string |
122 | */ |
123 | public function render(?string $format = null): string |
124 | { |
125 | if (($format == self::CSS) || ($format == self::COMMA)) { |
126 | $rgb = $this->toRgb(); |
127 | return $rgb->render($format); |
128 | } else if ($format == self::PERCENT) { |
129 | return (string)round(($this->gray / 100), 2); |
130 | } else { |
131 | return (string)$this->gray; |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * Method to print the color object |
137 | * |
138 | * @return string |
139 | */ |
140 | public function __toString() |
141 | { |
142 | return $this->render(self::PERCENT); |
143 | } |
144 | |
145 | /** |
146 | * Magic method to set the color value |
147 | * |
148 | * @param string $name |
149 | * @param mixed $value |
150 | * @throws Exception |
151 | * @return void |
152 | */ |
153 | public function __set(string $name, mixed $value): void |
154 | { |
155 | $this->offsetSet($name, $value); |
156 | } |
157 | |
158 | /** |
159 | * Magic method to return the color value |
160 | * |
161 | * @param string $name |
162 | * @throws Exception |
163 | * @return mixed |
164 | */ |
165 | public function __get(string $name): mixed |
166 | { |
167 | return $this->offsetGet($name); |
168 | } |
169 | |
170 | /** |
171 | * Magic method to return whether the color value exists |
172 | * |
173 | * @param string $name |
174 | * @return bool |
175 | */ |
176 | public function __isset(string $name): bool |
177 | { |
178 | return $this->offsetExists($name); |
179 | } |
180 | |
181 | /** |
182 | * Magic method to unset color value |
183 | * |
184 | * @param string $name |
185 | * @throws Exception |
186 | * @return void |
187 | */ |
188 | public function __unset(string $name): void |
189 | { |
190 | throw new Exception('You cannot unset the properties of this color object.'); |
191 | } |
192 | |
193 | /** |
194 | * ArrayAccess offsetExists |
195 | * |
196 | * @param mixed $offset |
197 | * @return bool |
198 | */ |
199 | public function offsetExists(mixed $offset): bool |
200 | { |
201 | return ($offset == 'gray'); |
202 | } |
203 | |
204 | /** |
205 | * ArrayAccess offsetGet |
206 | * |
207 | * @param mixed $offset |
208 | * @throws Exception |
209 | * @return mixed |
210 | */ |
211 | public function offsetGet(mixed $offset): mixed |
212 | { |
213 | switch ($offset) { |
214 | case 'gray': |
215 | return $this->getGray(); |
216 | break; |
217 | default: |
218 | throw new Exception("Error: You can only use 'gray'."); |
219 | } |
220 | } |
221 | |
222 | /** |
223 | * ArrayAccess offsetSet |
224 | * |
225 | * @param mixed $offset |
226 | * @param mixed $value |
227 | * @throws Exception |
228 | * @return void |
229 | */ |
230 | public function offsetSet(mixed $offset, mixed $value): void |
231 | { |
232 | switch ($offset) { |
233 | case 'gray': |
234 | $this->setGray($value); |
235 | break; |
236 | default: |
237 | throw new Exception("Error: You can only use 'gray'."); |
238 | } |
239 | } |
240 | |
241 | /** |
242 | * ArrayAccess offsetUnset |
243 | * |
244 | * @param mixed $offset |
245 | * @throws Exception |
246 | * @return void |
247 | */ |
248 | public function offsetUnset(mixed $offset): void |
249 | { |
250 | throw new Exception('You cannot unset the properties of this color object.'); |
251 | } |
252 | |
253 | } |