| 1 | <?php |
||
| 10 | class Rgb implements Color |
||
| 11 | { |
||
| 12 | private int $red = 0; |
||
|
|
|||
| 13 | private int $green = 0; |
||
| 14 | private int $blue = 0; |
||
| 15 | |||
| 16 | 13 | public function __construct(int $red = null, int $green = null, int $blue = null) |
|
| 17 | { |
||
| 18 | 13 | if ($red) { |
|
| 19 | 2 | $this->setRed($red); |
|
| 20 | } |
||
| 21 | |||
| 22 | 13 | if ($green) { |
|
| 23 | 2 | $this->setGreen($green); |
|
| 24 | } |
||
| 25 | |||
| 26 | 13 | if ($blue) { |
|
| 27 | 2 | $this->setBlue($blue); |
|
| 28 | } |
||
| 29 | 13 | } |
|
| 30 | |||
| 31 | 6 | public function setRed(int $red): void |
|
| 32 | { |
||
| 33 | 6 | $this->isValid($red); |
|
| 34 | 4 | $this->red = $red; |
|
| 35 | 4 | } |
|
| 36 | |||
| 37 | 6 | public function setGreen(int $green): void |
|
| 38 | { |
||
| 39 | 6 | $this->isValid($green); |
|
| 40 | 4 | $this->green = $green; |
|
| 41 | 4 | } |
|
| 42 | |||
| 43 | 6 | public function setBlue(int $blue): void |
|
| 44 | { |
||
| 45 | 6 | $this->isValid($blue); |
|
| 46 | 4 | $this->blue = $blue; |
|
| 47 | 4 | } |
|
| 48 | |||
| 49 | 7 | public function toString(): string |
|
| 50 | { |
||
| 51 | 7 | return sprintf('rgb(%s, %s, %s)', $this->red, $this->green, $this->blue); |
|
| 52 | } |
||
| 53 | |||
| 54 | 12 | private function isValid(int $value): void |
|
| 55 | { |
||
| 56 | 12 | if ($value < 0 || $value > 255 ) { |
|
| 57 | 6 | throw new OutOfRangeException('Value out of range (0-255).'); |
|
| 58 | } |
||
| 59 | 6 | } |
|
| 60 | } |
||
| 61 |