Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| Grayscale | |
100.00% |
22 / 22 |
|
100.00% |
9 / 9 |
16 | |
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% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| channels | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Color\Color; |
| 16 | |
| 17 | use OutOfRangeException; |
| 18 | |
| 19 | /** |
| 20 | * Image gray color class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Image |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 2.0.0 |
| 28 | */ |
| 29 | class Grayscale extends AbstractColor |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Gray |
| 34 | * @var float |
| 35 | */ |
| 36 | protected float $gray = 0; |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * Instantiate a PERCENT Gray Color object |
| 42 | * |
| 43 | * @param mixed $gray 0 - 100 |
| 44 | */ |
| 45 | public function __construct(mixed $gray) |
| 46 | { |
| 47 | $this->setGray($gray); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Set the gray value |
| 52 | * |
| 53 | * @param mixed $gray |
| 54 | * @throws OutOfRangeException |
| 55 | * @return Grayscale |
| 56 | */ |
| 57 | public function setGray(mixed $gray): Grayscale |
| 58 | { |
| 59 | $gray = (float)$gray; |
| 60 | if (($gray < 0) || ($gray > 100)) { |
| 61 | throw new OutOfRangeException('Error: The value must be between 0 and 100'); |
| 62 | } else if ($gray <= 1) { |
| 63 | $gray = (float)($gray * 100); |
| 64 | } |
| 65 | $this->gray = $gray; |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the gray value |
| 71 | * |
| 72 | * @return float |
| 73 | */ |
| 74 | public function getGray(): float |
| 75 | { |
| 76 | return $this->gray; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert to CMYK |
| 81 | * |
| 82 | * @return Cmyk |
| 83 | */ |
| 84 | public function toCmyk(): Cmyk |
| 85 | { |
| 86 | return new Cmyk(0, 0, 0, $this->gray); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Convert to RGB |
| 91 | * |
| 92 | * @return Rgb |
| 93 | */ |
| 94 | public function toRgb(): Rgb |
| 95 | { |
| 96 | $rgb = (int)round(($this->gray / 100) * 255); |
| 97 | return new Rgb($rgb, $rgb, $rgb); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Convert to array |
| 102 | * |
| 103 | * @param bool $assoc |
| 104 | * @return array |
| 105 | */ |
| 106 | public function toArray(bool $assoc = true): array |
| 107 | { |
| 108 | $gray = []; |
| 109 | |
| 110 | if ($assoc) { |
| 111 | $gray['gray'] = $this->gray; |
| 112 | } else { |
| 113 | $gray[] = $this->gray; |
| 114 | } |
| 115 | |
| 116 | return $gray; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Convert to readable string |
| 121 | * |
| 122 | * @param ?string $format |
| 123 | * @return string |
| 124 | */ |
| 125 | public function render(?string $format = null): string |
| 126 | { |
| 127 | return match ($format) { |
| 128 | self::CSS, self::COMMA => $this->toRgb()->render($format), |
| 129 | self::PERCENT => (string)round(($this->gray / 100), 2), |
| 130 | default => (string)$this->gray, |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Method to print the color object |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | public function __toString() |
| 140 | { |
| 141 | return $this->render(self::PERCENT); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the valid channel names |
| 146 | * |
| 147 | * @return array |
| 148 | */ |
| 149 | protected function channels(): array |
| 150 | { |
| 151 | return ['gray']; |
| 152 | } |
| 153 | |
| 154 | } |