Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
40 / 40 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
Rgb | |
100.00% |
40 / 40 |
|
100.00% |
11 / 11 |
21 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setR | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
setG | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
setB | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
getR | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getG | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getB | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toCmyk | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
5 | |||
toGray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toHex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Image\Color; |
15 | |
16 | /** |
17 | * Image RGB color class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Image |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 3.4.0 |
25 | */ |
26 | class Rgb extends AbstractColor |
27 | { |
28 | |
29 | /** |
30 | * Red |
31 | * @var float |
32 | */ |
33 | protected $r = 0; |
34 | |
35 | /** |
36 | * Green |
37 | * @var float |
38 | */ |
39 | protected $g = 0; |
40 | |
41 | /** |
42 | * Blue |
43 | * @var float |
44 | */ |
45 | protected $b = 0; |
46 | |
47 | /** |
48 | * Constructor |
49 | * |
50 | * Instantiate a PDF RGB Color object |
51 | * |
52 | * @param mixed $r 0 - 255 |
53 | * @param mixed $g 0 - 255 |
54 | * @param mixed $b 0 - 255 |
55 | */ |
56 | public function __construct($r, $g, $b) |
57 | { |
58 | $this->setR($r); |
59 | $this->setG($g); |
60 | $this->setB($b); |
61 | } |
62 | |
63 | /** |
64 | * Set the red value |
65 | * |
66 | * @param mixed $r |
67 | * @throws \OutOfRangeException |
68 | * @return Rgb |
69 | */ |
70 | public function setR($r) |
71 | { |
72 | if (((int)$r < 0) || ((int)$r > 255)) { |
73 | throw new \OutOfRangeException('Error: The value must be between 0 and 255'); |
74 | } |
75 | $this->r = (int)$r; |
76 | return $this; |
77 | } |
78 | |
79 | /** |
80 | * Set the green value |
81 | * |
82 | * @param mixed $g |
83 | * @throws \OutOfRangeException |
84 | * @return Rgb |
85 | */ |
86 | public function setG($g) |
87 | { |
88 | if (((int)$g < 0) || ((int)$g > 255)) { |
89 | throw new \OutOfRangeException('Error: The value must be between 0 and 255'); |
90 | } |
91 | $this->g = (int)$g; |
92 | return $this; |
93 | } |
94 | |
95 | /** |
96 | * Set the blue value |
97 | * |
98 | * @param mixed $b |
99 | * @throws \OutOfRangeException |
100 | * @return Rgb |
101 | */ |
102 | public function setB($b) |
103 | { |
104 | if (((int)$b < 0) || ((int)$b > 255)) { |
105 | throw new \OutOfRangeException('Error: The value must be between 0 and 255'); |
106 | } |
107 | $this->b = (int)$b; |
108 | return $this; |
109 | } |
110 | |
111 | /** |
112 | * Get the red value |
113 | * |
114 | * @return float |
115 | */ |
116 | public function getR() |
117 | { |
118 | return $this->r; |
119 | } |
120 | |
121 | /** |
122 | * Get the green value |
123 | * |
124 | * @return float |
125 | */ |
126 | public function getG() |
127 | { |
128 | return $this->g; |
129 | } |
130 | |
131 | /** |
132 | * Get the blue value |
133 | * |
134 | * @return float |
135 | */ |
136 | public function getB() |
137 | { |
138 | return $this->b; |
139 | } |
140 | |
141 | /** |
142 | * Convert to CMYK |
143 | * |
144 | * @return Cmyk |
145 | */ |
146 | public function toCmyk() |
147 | { |
148 | $K = 1; |
149 | |
150 | // Calculate CMY. |
151 | $cyan = 1 - ($this->r / 255); |
152 | $magenta = 1 - ($this->g / 255); |
153 | $yellow = 1 - ($this->b / 255); |
154 | |
155 | // Calculate K. |
156 | if ($cyan < $K) { |
157 | $K = $cyan; |
158 | } |
159 | if ($magenta < $K) { |
160 | $K = $magenta; |
161 | } |
162 | if ($yellow < $K) { |
163 | $K = $yellow; |
164 | } |
165 | |
166 | if ($K == 1) { |
167 | $cyan = 0; |
168 | $magenta = 0; |
169 | $yellow = 0; |
170 | } else { |
171 | $cyan = round((($cyan - $K) / (1 - $K)) * 100); |
172 | $magenta = round((($magenta - $K) / (1 - $K)) * 100); |
173 | $yellow = round((($yellow - $K) / (1 - $K)) * 100); |
174 | } |
175 | |
176 | $black = round($K * 100); |
177 | |
178 | return new Cmyk($cyan, $magenta, $yellow, $black); |
179 | } |
180 | |
181 | /** |
182 | * Convert to Gray |
183 | * |
184 | * @return Gray |
185 | */ |
186 | public function toGray() |
187 | { |
188 | return new Gray(floor(($this->r + $this->g + $this->b) / 3)); |
189 | } |
190 | |
191 | /** |
192 | * Convert to hex string |
193 | * |
194 | * @return string |
195 | */ |
196 | public function toHex() |
197 | { |
198 | return sprintf('%02x', $this->r) . sprintf('%02x', $this->g) . sprintf('%02x', $this->b); |
199 | } |
200 | |
201 | /** |
202 | * Method to print the color object |
203 | * |
204 | * @return string |
205 | */ |
206 | public function __toString() |
207 | { |
208 | return $this->r . ', ' . $this->g . ', ' . $this->b; |
209 | } |
210 | |
211 | } |