Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Color | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
15 | |
100.00% |
1 / 1 |
rgb | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hsl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cmyk | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
grayscale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parse | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
parseColorValues | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 |
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-2024 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\Color; |
15 | |
16 | use ReflectionException; |
17 | |
18 | /** |
19 | * Pop color class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Color |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 1.0.0 |
27 | */ |
28 | class Color |
29 | { |
30 | |
31 | /** |
32 | * Instantiate an RGB color object |
33 | * |
34 | * @param int $r |
35 | * @param int $g |
36 | * @param int $b |
37 | * @param ?float $a |
38 | * @return Color\Rgb |
39 | */ |
40 | public static function rgb(int $r, int $g, int $b, ?float $a = null): Color\Rgb |
41 | { |
42 | return new Color\Rgb($r, $g, $b, $a); |
43 | } |
44 | |
45 | /** |
46 | * Instantiate an HSL color object |
47 | * |
48 | * @param int $h |
49 | * @param int $s |
50 | * @param int $l |
51 | * @param ?float $a |
52 | * @return Color\Hsl |
53 | */ |
54 | public static function hsl(int $h, int $s, int $l, ?float $a = null): Color\Hsl |
55 | { |
56 | return new Color\Hsl($h, $s, $l, $a); |
57 | } |
58 | |
59 | /** |
60 | * Instantiate a Hex color object |
61 | * |
62 | * @param string $hex |
63 | * @return Color\Hex |
64 | */ |
65 | public static function hex(string $hex): Color\Hex |
66 | { |
67 | return new Color\Hex($hex); |
68 | } |
69 | |
70 | /** |
71 | * Instantiate a CMYK color object |
72 | * |
73 | * @param int $c |
74 | * @param int $m |
75 | * @param int $y |
76 | * @param int $k |
77 | * @return Color\Cmyk |
78 | */ |
79 | public static function cmyk(int $c, int $m, int $y, int $k): Color\Cmyk |
80 | { |
81 | return new Color\Cmyk($c, $m, $y, $k); |
82 | } |
83 | |
84 | /** |
85 | * Instantiate a grayscale color object |
86 | * |
87 | * @param int $gray |
88 | * @return Color\Grayscale |
89 | */ |
90 | public static function grayscale(int $gray): Color\Grayscale |
91 | { |
92 | return new Color\Grayscale($gray); |
93 | } |
94 | |
95 | /** |
96 | * Parse color from string |
97 | * |
98 | * @param string $colorString |
99 | * @throws Color\Exception|ReflectionException |
100 | * @return Color\ColorInterface |
101 | */ |
102 | public static function parse(string $colorString): Color\ColorInterface |
103 | { |
104 | $colorString = strtolower($colorString); |
105 | |
106 | if (str_starts_with($colorString, 'rgb')) { |
107 | $params = self::parseColorValues($colorString); |
108 | return (new \ReflectionClass('Pop\Color\Color\Rgb'))->newInstanceArgs($params); |
109 | } else if (str_starts_with($colorString, 'hsl')) { |
110 | $params = self::parseColorValues($colorString); |
111 | return (new \ReflectionClass('Pop\Color\Color\Hsl'))->newInstanceArgs($params); |
112 | } else if (str_starts_with($colorString, '#')) { |
113 | return new Color\Hex($colorString); |
114 | } else if (substr_count($colorString, ' ') == 3) { |
115 | $params = self::parseColorValues($colorString, false); |
116 | return (new \ReflectionClass('Pop\Color\Color\Cmyk'))->newInstanceArgs($params); |
117 | } else if (is_numeric($colorString)) { |
118 | return new Color\Grayscale($colorString); |
119 | } |
120 | else { |
121 | throw new Color\Exception('Error: The string was not in the correct color format.'); |
122 | } |
123 | } |
124 | |
125 | /** |
126 | * Parse color values from string |
127 | * |
128 | * @param string $colorString |
129 | * @param bool $comma |
130 | * @return array |
131 | */ |
132 | public static function parseColorValues(string $colorString, $comma = true): array |
133 | { |
134 | if ((str_contains($colorString, '(')) && (str_contains($colorString, ')'))) { |
135 | $colorString = substr($colorString, (strpos($colorString, '(') + 1)); |
136 | $colorString = substr($colorString, 0, strpos($colorString, ')')); |
137 | } |
138 | |
139 | $values = ($comma) ? explode(',' , $colorString) : explode(' ', $colorString); |
140 | return array_map('trim', $values); |
141 | } |
142 | |
143 | } |