Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.65% |
88 / 92 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Color | |
95.65% |
88 / 92 |
|
50.00% |
2 / 4 |
20 | |
0.00% |
0 / 1 |
| parse | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| parseHex | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
5.27 | |||
| parseRgb | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
8.10 | |||
| namedColors | |
100.00% |
55 / 55 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Svg\Value; |
| 6 | |
| 7 | use Phpdftk\Color\ColorInterface; |
| 8 | use Phpdftk\Color\RgbColor; |
| 9 | |
| 10 | /** |
| 11 | * Parse an SVG colour string into a `Phpdftk\Color\ColorInterface`. |
| 12 | * |
| 13 | * Recognised forms (SVG 2 / CSS Color 3 subset): |
| 14 | * |
| 15 | * - Hex: `#rgb` (each digit doubled to a byte) and `#rrggbb`. |
| 16 | * - `rgb(R, G, B)` with numeric (0–255) or percentage (0–100%) |
| 17 | * components; comma-or-whitespace separated. |
| 18 | * - CSS named colours — the CSS Color 3 §4.3 table (148 entries |
| 19 | * including the spec-only `transparent`). Lookups are |
| 20 | * case-insensitive per spec. |
| 21 | * |
| 22 | * Returns null on absent / empty / malformed input — SVG 2's "invalid → |
| 23 | * ignored" semantics. Callers that want a hard error (`Paint::parse` does |
| 24 | * not) should check the return. |
| 25 | * |
| 26 | * Out of scope at 3E: `rgba()`, `hsl()`, `hwb()`, `lab()`, `lch()`, |
| 27 | * `oklch()`, modern slash-syntax alpha, calc() inside components. These |
| 28 | * land alongside the cascade work in 3J. |
| 29 | */ |
| 30 | final class Color |
| 31 | { |
| 32 | public static function parse(string $raw): ?ColorInterface |
| 33 | { |
| 34 | $value = trim($raw); |
| 35 | if ($value === '') { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | if ($value[0] === '#') { |
| 40 | return self::parseHex(substr($value, 1)); |
| 41 | } |
| 42 | if (preg_match('/^rgb\s*\(\s*(.*)\s*\)\s*$/i', $value, $m) === 1) { |
| 43 | return self::parseRgb($m[1]); |
| 44 | } |
| 45 | |
| 46 | $named = self::namedColors()[strtolower($value)] ?? null; |
| 47 | if ($named !== null) { |
| 48 | return RgbColor::fromHex($named); |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | private static function parseHex(string $body): ?ColorInterface |
| 54 | { |
| 55 | if (preg_match('/^[0-9a-fA-F]+$/', $body) !== 1) { |
| 56 | return null; |
| 57 | } |
| 58 | if (strlen($body) === 3) { |
| 59 | $body = $body[0] . $body[0] . $body[1] . $body[1] . $body[2] . $body[2]; |
| 60 | } |
| 61 | if (strlen($body) !== 6) { |
| 62 | return null; |
| 63 | } |
| 64 | try { |
| 65 | return RgbColor::fromHex($body); |
| 66 | } catch (\InvalidArgumentException) { |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private static function parseRgb(string $body): ?ColorInterface |
| 72 | { |
| 73 | $parts = preg_split('/\s*,\s*|\s+/', trim($body)) ?: []; |
| 74 | if (count($parts) !== 3) { |
| 75 | return null; |
| 76 | } |
| 77 | $components = []; |
| 78 | foreach ($parts as $part) { |
| 79 | if ($part === '') { |
| 80 | return null; |
| 81 | } |
| 82 | if (str_ends_with($part, '%')) { |
| 83 | $n = substr($part, 0, -1); |
| 84 | if (!is_numeric($n)) { |
| 85 | return null; |
| 86 | } |
| 87 | $components[] = max(0.0, min(1.0, ((float) $n) / 100.0)); |
| 88 | continue; |
| 89 | } |
| 90 | if (!is_numeric($part)) { |
| 91 | return null; |
| 92 | } |
| 93 | $components[] = max(0.0, min(1.0, ((float) $part) / 255.0)); |
| 94 | } |
| 95 | return new RgbColor($components[0], $components[1], $components[2]); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * CSS Color 3 §4.3 named-colour table. Lazily materialised at first |
| 100 | * lookup — keeps the parse() fast-path free of the ~150-entry map. |
| 101 | * |
| 102 | * @return array<string, string> |
| 103 | */ |
| 104 | private static function namedColors(): array |
| 105 | { |
| 106 | static $map = null; |
| 107 | if ($map === null) { |
| 108 | $map = [ |
| 109 | 'aliceblue' => 'F0F8FF', 'antiquewhite' => 'FAEBD7', 'aqua' => '00FFFF', |
| 110 | 'aquamarine' => '7FFFD4', 'azure' => 'F0FFFF', 'beige' => 'F5F5DC', |
| 111 | 'bisque' => 'FFE4C4', 'black' => '000000', 'blanchedalmond' => 'FFEBCD', |
| 112 | 'blue' => '0000FF', 'blueviolet' => '8A2BE2', 'brown' => 'A52A2A', |
| 113 | 'burlywood' => 'DEB887', 'cadetblue' => '5F9EA0', 'chartreuse' => '7FFF00', |
| 114 | 'chocolate' => 'D2691E', 'coral' => 'FF7F50', 'cornflowerblue' => '6495ED', |
| 115 | 'cornsilk' => 'FFF8DC', 'crimson' => 'DC143C', 'cyan' => '00FFFF', |
| 116 | 'darkblue' => '00008B', 'darkcyan' => '008B8B', 'darkgoldenrod' => 'B8860B', |
| 117 | 'darkgray' => 'A9A9A9', 'darkgreen' => '006400', 'darkgrey' => 'A9A9A9', |
| 118 | 'darkkhaki' => 'BDB76B', 'darkmagenta' => '8B008B', 'darkolivegreen' => '556B2F', |
| 119 | 'darkorange' => 'FF8C00', 'darkorchid' => '9932CC', 'darkred' => '8B0000', |
| 120 | 'darksalmon' => 'E9967A', 'darkseagreen' => '8FBC8F', 'darkslateblue' => '483D8B', |
| 121 | 'darkslategray' => '2F4F4F', 'darkslategrey' => '2F4F4F', 'darkturquoise' => '00CED1', |
| 122 | 'darkviolet' => '9400D3', 'deeppink' => 'FF1493', 'deepskyblue' => '00BFFF', |
| 123 | 'dimgray' => '696969', 'dimgrey' => '696969', 'dodgerblue' => '1E90FF', |
| 124 | 'firebrick' => 'B22222', 'floralwhite' => 'FFFAF0', 'forestgreen' => '228B22', |
| 125 | 'fuchsia' => 'FF00FF', 'gainsboro' => 'DCDCDC', 'ghostwhite' => 'F8F8FF', |
| 126 | 'gold' => 'FFD700', 'goldenrod' => 'DAA520', 'gray' => '808080', |
| 127 | 'green' => '008000', 'greenyellow' => 'ADFF2F', 'grey' => '808080', |
| 128 | 'honeydew' => 'F0FFF0', 'hotpink' => 'FF69B4', 'indianred' => 'CD5C5C', |
| 129 | 'indigo' => '4B0082', 'ivory' => 'FFFFF0', 'khaki' => 'F0E68C', |
| 130 | 'lavender' => 'E6E6FA', 'lavenderblush' => 'FFF0F5', 'lawngreen' => '7CFC00', |
| 131 | 'lemonchiffon' => 'FFFACD', 'lightblue' => 'ADD8E6', 'lightcoral' => 'F08080', |
| 132 | 'lightcyan' => 'E0FFFF', 'lightgoldenrodyellow' => 'FAFAD2', 'lightgray' => 'D3D3D3', |
| 133 | 'lightgreen' => '90EE90', 'lightgrey' => 'D3D3D3', 'lightpink' => 'FFB6C1', |
| 134 | 'lightsalmon' => 'FFA07A', 'lightseagreen' => '20B2AA', 'lightskyblue' => '87CEFA', |
| 135 | 'lightslategray' => '778899', 'lightslategrey' => '778899', 'lightsteelblue' => 'B0C4DE', |
| 136 | 'lightyellow' => 'FFFFE0', 'lime' => '00FF00', 'limegreen' => '32CD32', |
| 137 | 'linen' => 'FAF0E6', 'magenta' => 'FF00FF', 'maroon' => '800000', |
| 138 | 'mediumaquamarine' => '66CDAA', 'mediumblue' => '0000CD', 'mediumorchid' => 'BA55D3', |
| 139 | 'mediumpurple' => '9370DB', 'mediumseagreen' => '3CB371', 'mediumslateblue' => '7B68EE', |
| 140 | 'mediumspringgreen' => '00FA9A', 'mediumturquoise' => '48D1CC', 'mediumvioletred' => 'C71585', |
| 141 | 'midnightblue' => '191970', 'mintcream' => 'F5FFFA', 'mistyrose' => 'FFE4E1', |
| 142 | 'moccasin' => 'FFE4B5', 'navajowhite' => 'FFDEAD', 'navy' => '000080', |
| 143 | 'oldlace' => 'FDF5E6', 'olive' => '808000', 'olivedrab' => '6B8E23', |
| 144 | 'orange' => 'FFA500', 'orangered' => 'FF4500', 'orchid' => 'DA70D6', |
| 145 | 'palegoldenrod' => 'EEE8AA', 'palegreen' => '98FB98', 'paleturquoise' => 'AFEEEE', |
| 146 | 'palevioletred' => 'DB7093', 'papayawhip' => 'FFEFD5', 'peachpuff' => 'FFDAB9', |
| 147 | 'peru' => 'CD853F', 'pink' => 'FFC0CB', 'plum' => 'DDA0DD', |
| 148 | 'powderblue' => 'B0E0E6', 'purple' => '800080', 'rebeccapurple' => '663399', |
| 149 | 'red' => 'FF0000', 'rosybrown' => 'BC8F8F', 'royalblue' => '4169E1', |
| 150 | 'saddlebrown' => '8B4513', 'salmon' => 'FA8072', 'sandybrown' => 'F4A460', |
| 151 | 'seagreen' => '2E8B57', 'seashell' => 'FFF5EE', 'sienna' => 'A0522D', |
| 152 | 'silver' => 'C0C0C0', 'skyblue' => '87CEEB', 'slateblue' => '6A5ACD', |
| 153 | 'slategray' => '708090', 'slategrey' => '708090', 'snow' => 'FFFAFA', |
| 154 | 'springgreen' => '00FF7F', 'steelblue' => '4682B4', 'tan' => 'D2B48C', |
| 155 | 'teal' => '008080', 'thistle' => 'D8BFD8', 'tomato' => 'FF6347', |
| 156 | 'turquoise' => '40E0D0', 'violet' => 'EE82EE', 'wheat' => 'F5DEB3', |
| 157 | 'white' => 'FFFFFF', 'whitesmoke' => 'F5F5F5', 'yellow' => 'FFFF00', |
| 158 | 'yellowgreen' => '9ACD32', |
| 159 | ]; |
| 160 | } |
| 161 | return $map; |
| 162 | } |
| 163 | } |