| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package dompdf |
| 5 |
* @link https://github.com/dompdf/dompdf |
| 6 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 7 |
*/ |
| 8 |
namespace Dompdf\Css; |
| 9 |
|
| 10 |
use Dompdf\Helpers; |
| 11 |
|
| 12 |
class Color |
| 13 |
{ |
| 14 |
static $cssColorNames = [ |
| 15 |
"aliceblue" => "F0F8FF", |
| 16 |
"antiquewhite" => "FAEBD7", |
| 17 |
"aqua" => "00FFFF", |
| 18 |
"aquamarine" => "7FFFD4", |
| 19 |
"azure" => "F0FFFF", |
| 20 |
"beige" => "F5F5DC", |
| 21 |
"bisque" => "FFE4C4", |
| 22 |
"black" => "000000", |
| 23 |
"blanchedalmond" => "FFEBCD", |
| 24 |
"blue" => "0000FF", |
| 25 |
"blueviolet" => "8A2BE2", |
| 26 |
"brown" => "A52A2A", |
| 27 |
"burlywood" => "DEB887", |
| 28 |
"cadetblue" => "5F9EA0", |
| 29 |
"chartreuse" => "7FFF00", |
| 30 |
"chocolate" => "D2691E", |
| 31 |
"coral" => "FF7F50", |
| 32 |
"cornflowerblue" => "6495ED", |
| 33 |
"cornsilk" => "FFF8DC", |
| 34 |
"crimson" => "DC143C", |
| 35 |
"cyan" => "00FFFF", |
| 36 |
"darkblue" => "00008B", |
| 37 |
"darkcyan" => "008B8B", |
| 38 |
"darkgoldenrod" => "B8860B", |
| 39 |
"darkgray" => "A9A9A9", |
| 40 |
"darkgreen" => "006400", |
| 41 |
"darkgrey" => "A9A9A9", |
| 42 |
"darkkhaki" => "BDB76B", |
| 43 |
"darkmagenta" => "8B008B", |
| 44 |
"darkolivegreen" => "556B2F", |
| 45 |
"darkorange" => "FF8C00", |
| 46 |
"darkorchid" => "9932CC", |
| 47 |
"darkred" => "8B0000", |
| 48 |
"darksalmon" => "E9967A", |
| 49 |
"darkseagreen" => "8FBC8F", |
| 50 |
"darkslateblue" => "483D8B", |
| 51 |
"darkslategray" => "2F4F4F", |
| 52 |
"darkslategrey" => "2F4F4F", |
| 53 |
"darkturquoise" => "00CED1", |
| 54 |
"darkviolet" => "9400D3", |
| 55 |
"deeppink" => "FF1493", |
| 56 |
"deepskyblue" => "00BFFF", |
| 57 |
"dimgray" => "696969", |
| 58 |
"dimgrey" => "696969", |
| 59 |
"dodgerblue" => "1E90FF", |
| 60 |
"firebrick" => "B22222", |
| 61 |
"floralwhite" => "FFFAF0", |
| 62 |
"forestgreen" => "228B22", |
| 63 |
"fuchsia" => "FF00FF", |
| 64 |
"gainsboro" => "DCDCDC", |
| 65 |
"ghostwhite" => "F8F8FF", |
| 66 |
"gold" => "FFD700", |
| 67 |
"goldenrod" => "DAA520", |
| 68 |
"gray" => "808080", |
| 69 |
"green" => "008000", |
| 70 |
"greenyellow" => "ADFF2F", |
| 71 |
"grey" => "808080", |
| 72 |
"honeydew" => "F0FFF0", |
| 73 |
"hotpink" => "FF69B4", |
| 74 |
"indianred" => "CD5C5C", |
| 75 |
"indigo" => "4B0082", |
| 76 |
"ivory" => "FFFFF0", |
| 77 |
"khaki" => "F0E68C", |
| 78 |
"lavender" => "E6E6FA", |
| 79 |
"lavenderblush" => "FFF0F5", |
| 80 |
"lawngreen" => "7CFC00", |
| 81 |
"lemonchiffon" => "FFFACD", |
| 82 |
"lightblue" => "ADD8E6", |
| 83 |
"lightcoral" => "F08080", |
| 84 |
"lightcyan" => "E0FFFF", |
| 85 |
"lightgoldenrodyellow" => "FAFAD2", |
| 86 |
"lightgray" => "D3D3D3", |
| 87 |
"lightgreen" => "90EE90", |
| 88 |
"lightgrey" => "D3D3D3", |
| 89 |
"lightpink" => "FFB6C1", |
| 90 |
"lightsalmon" => "FFA07A", |
| 91 |
"lightseagreen" => "20B2AA", |
| 92 |
"lightskyblue" => "87CEFA", |
| 93 |
"lightslategray" => "778899", |
| 94 |
"lightslategrey" => "778899", |
| 95 |
"lightsteelblue" => "B0C4DE", |
| 96 |
"lightyellow" => "FFFFE0", |
| 97 |
"lime" => "00FF00", |
| 98 |
"limegreen" => "32CD32", |
| 99 |
"linen" => "FAF0E6", |
| 100 |
"magenta" => "FF00FF", |
| 101 |
"maroon" => "800000", |
| 102 |
"mediumaquamarine" => "66CDAA", |
| 103 |
"mediumblue" => "0000CD", |
| 104 |
"mediumorchid" => "BA55D3", |
| 105 |
"mediumpurple" => "9370DB", |
| 106 |
"mediumseagreen" => "3CB371", |
| 107 |
"mediumslateblue" => "7B68EE", |
| 108 |
"mediumspringgreen" => "00FA9A", |
| 109 |
"mediumturquoise" => "48D1CC", |
| 110 |
"mediumvioletred" => "C71585", |
| 111 |
"midnightblue" => "191970", |
| 112 |
"mintcream" => "F5FFFA", |
| 113 |
"mistyrose" => "FFE4E1", |
| 114 |
"moccasin" => "FFE4B5", |
| 115 |
"navajowhite" => "FFDEAD", |
| 116 |
"navy" => "000080", |
| 117 |
"oldlace" => "FDF5E6", |
| 118 |
"olive" => "808000", |
| 119 |
"olivedrab" => "6B8E23", |
| 120 |
"orange" => "FFA500", |
| 121 |
"orangered" => "FF4500", |
| 122 |
"orchid" => "DA70D6", |
| 123 |
"palegoldenrod" => "EEE8AA", |
| 124 |
"palegreen" => "98FB98", |
| 125 |
"paleturquoise" => "AFEEEE", |
| 126 |
"palevioletred" => "DB7093", |
| 127 |
"papayawhip" => "FFEFD5", |
| 128 |
"peachpuff" => "FFDAB9", |
| 129 |
"peru" => "CD853F", |
| 130 |
"pink" => "FFC0CB", |
| 131 |
"plum" => "DDA0DD", |
| 132 |
"powderblue" => "B0E0E6", |
| 133 |
"purple" => "800080", |
| 134 |
"red" => "FF0000", |
| 135 |
"rosybrown" => "BC8F8F", |
| 136 |
"royalblue" => "4169E1", |
| 137 |
"saddlebrown" => "8B4513", |
| 138 |
"salmon" => "FA8072", |
| 139 |
"sandybrown" => "F4A460", |
| 140 |
"seagreen" => "2E8B57", |
| 141 |
"seashell" => "FFF5EE", |
| 142 |
"sienna" => "A0522D", |
| 143 |
"silver" => "C0C0C0", |
| 144 |
"skyblue" => "87CEEB", |
| 145 |
"slateblue" => "6A5ACD", |
| 146 |
"slategray" => "708090", |
| 147 |
"slategrey" => "708090", |
| 148 |
"snow" => "FFFAFA", |
| 149 |
"springgreen" => "00FF7F", |
| 150 |
"steelblue" => "4682B4", |
| 151 |
"tan" => "D2B48C", |
| 152 |
"teal" => "008080", |
| 153 |
"thistle" => "D8BFD8", |
| 154 |
"tomato" => "FF6347", |
| 155 |
"turquoise" => "40E0D0", |
| 156 |
"violet" => "EE82EE", |
| 157 |
"wheat" => "F5DEB3", |
| 158 |
"white" => "FFFFFF", |
| 159 |
"whitesmoke" => "F5F5F5", |
| 160 |
"yellow" => "FFFF00", |
| 161 |
"yellowgreen" => "9ACD32", |
| 162 |
]; |
| 163 |
|
| 164 |
/** |
| 165 |
* @param array|string|null $color |
| 166 |
* @return array|string|null |
| 167 |
*/ |
| 168 |
static function parse($color) |
| 169 |
{ |
| 170 |
if ($color === null) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
if (is_array($color)) { |
| 175 |
// Assume the array has the right format... |
| 176 |
// FIXME: should/could verify this. |
| 177 |
return $color; |
| 178 |
} |
| 179 |
|
| 180 |
static $cache = []; |
| 181 |
|
| 182 |
$color = strtolower($color); |
| 183 |
|
| 184 |
if (isset($cache[$color])) { |
| 185 |
return $cache[$color]; |
| 186 |
} |
| 187 |
|
| 188 |
if ($color === "transparent") { |
| 189 |
return $cache[$color] = $color; |
| 190 |
} |
| 191 |
|
| 192 |
if (isset(self::$cssColorNames[$color])) { |
| 193 |
return $cache[$color] = self::getArray(self::$cssColorNames[$color]); |
| 194 |
} |
| 195 |
|
| 196 |
// https://www.w3.org/TR/css-color-4/#hex-notation |
| 197 |
if (mb_substr($color, 0, 1) === "#") { |
| 198 |
$length = mb_strlen($color); |
| 199 |
$alpha = 1.0; |
| 200 |
|
| 201 |
// #rgb format |
| 202 |
if ($length === 4) { |
| 203 |
return $cache[$color] = self::getArray($color[1] . $color[1] . $color[2] . $color[2] . $color[3] . $color[3]); |
| 204 |
} |
| 205 |
|
| 206 |
// #rgba format |
| 207 |
if ($length === 5) { |
| 208 |
if (ctype_xdigit($color[4])) { |
| 209 |
$alpha = round(hexdec($color[4] . $color[4])/255, 2); |
| 210 |
} |
| 211 |
return $cache[$color] = self::getArray($color[1] . $color[1] . $color[2] . $color[2] . $color[3] . $color[3], $alpha); |
| 212 |
} |
| 213 |
|
| 214 |
// #rrggbb format |
| 215 |
if ($length === 7) { |
| 216 |
return $cache[$color] = self::getArray(mb_substr($color, 1, 6)); |
| 217 |
} |
| 218 |
|
| 219 |
// #rrggbbaa format |
| 220 |
if ($length === 9) { |
| 221 |
if (ctype_xdigit(mb_substr($color, 7, 2))) { |
| 222 |
$alpha = round(hexdec(mb_substr($color, 7, 2))/255, 2); |
| 223 |
} |
| 224 |
return $cache[$color] = self::getArray(mb_substr($color, 1, 6), $alpha); |
| 225 |
} |
| 226 |
|
| 227 |
return null; |
| 228 |
} |
| 229 |
|
| 230 |
// rgb( r g b [/α] ) / rgb( r,g,b[,α] ) format and alias rgba() |
| 231 |
// https://www.w3.org/TR/css-color-4/#rgb-functions |
| 232 |
if (mb_substr($color, 0, 4) === "rgb(" || mb_substr($color, 0, 5) === "rgba(") { |
| 233 |
$i = mb_strpos($color, "("); |
| 234 |
$j = mb_strpos($color, ")"); |
| 235 |
|
| 236 |
// Bad color value |
| 237 |
if ($i === false || $j === false) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
|
| 241 |
$value_decl = trim(mb_substr($color, $i + 1, $j - $i - 1)); |
| 242 |
|
| 243 |
if (mb_strpos($value_decl, ",") === false) { |
| 244 |
// Space-separated values syntax `r g b` or `r g b / α` |
| 245 |
$parts = preg_split("/\s*\/\s*/", $value_decl); |
| 246 |
$triplet = preg_split("/\s+/", $parts[0]); |
| 247 |
$alpha = $parts[1] ?? 1.0; |
| 248 |
} else { |
| 249 |
// Comma-separated values syntax `r, g, b` or `r, g, b, α` |
| 250 |
$parts = preg_split("/\s*,\s*/", $value_decl); |
| 251 |
$triplet = array_slice($parts, 0, 3); |
| 252 |
$alpha = $parts[3] ?? 1.0; |
| 253 |
} |
| 254 |
|
| 255 |
if (count($triplet) !== 3) { |
| 256 |
return null; |
| 257 |
} |
| 258 |
|
| 259 |
// Parse alpha value |
| 260 |
if (Helpers::is_percent($alpha)) { |
| 261 |
$alpha = (float) $alpha / 100; |
| 262 |
} else { |
| 263 |
$alpha = (float) $alpha; |
| 264 |
} |
| 265 |
|
| 266 |
$alpha = max(0.0, min($alpha, 1.0)); |
| 267 |
|
| 268 |
foreach ($triplet as &$c) { |
| 269 |
if (Helpers::is_percent($c)) { |
| 270 |
$c = round((float) $c * 2.55); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return $cache[$color] = self::getArray(vsprintf("%02X%02X%02X", $triplet), $alpha); |
| 275 |
} |
| 276 |
|
| 277 |
// cmyk( c,m,y,k ) format |
| 278 |
// http://www.w3.org/TR/css3-gcpm/#cmyk-colors |
| 279 |
if (mb_substr($color, 0, 5) === "cmyk(") { |
| 280 |
$i = mb_strpos($color, "("); |
| 281 |
$j = mb_strpos($color, ")"); |
| 282 |
|
| 283 |
// Bad color value |
| 284 |
if ($i === false || $j === false) { |
| 285 |
return null; |
| 286 |
} |
| 287 |
|
| 288 |
$values = explode(",", mb_substr($color, $i + 1, $j - $i - 1)); |
| 289 |
|
| 290 |
if (count($values) != 4) { |
| 291 |
return null; |
| 292 |
} |
| 293 |
|
| 294 |
$values = array_map(function ($c) { |
| 295 |
return min(1.0, max(0.0, floatval(trim($c)))); |
| 296 |
}, $values); |
| 297 |
|
| 298 |
return $cache[$color] = self::getArray($values); |
| 299 |
} |
| 300 |
|
| 301 |
// Invalid or unsupported color format |
| 302 |
return null; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* @param array|string $color |
| 307 |
* @param float $alpha |
| 308 |
* @return array |
| 309 |
*/ |
| 310 |
static function getArray($color, $alpha = 1.0) |
| 311 |
{ |
| 312 |
$c = [null, null, null, null, "alpha" => $alpha, "hex" => null]; |
| 313 |
|
| 314 |
if (is_array($color)) { |
| 315 |
$c = $color; |
| 316 |
$c["c"] = $c[0]; |
| 317 |
$c["m"] = $c[1]; |
| 318 |
$c["y"] = $c[2]; |
| 319 |
$c["k"] = $c[3]; |
| 320 |
$c["alpha"] = $alpha; |
| 321 |
$c["hex"] = "cmyk($c[0],$c[1],$c[2],$c[3])"; |
| 322 |
} else { |
| 323 |
if (ctype_xdigit($color) === false || mb_strlen($color) !== 6) { |
| 324 |
// invalid color value ... expected 6-character hex |
| 325 |
return $c; |
| 326 |
} |
| 327 |
$c[0] = hexdec(mb_substr($color, 0, 2)) / 0xff; |
| 328 |
$c[1] = hexdec(mb_substr($color, 2, 2)) / 0xff; |
| 329 |
$c[2] = hexdec(mb_substr($color, 4, 2)) / 0xff; |
| 330 |
$c["r"] = $c[0]; |
| 331 |
$c["g"] = $c[1]; |
| 332 |
$c["b"] = $c[2]; |
| 333 |
$c["alpha"] = $alpha; |
| 334 |
$c["hex"] = sprintf("#%s%02X", $color, round($alpha * 255)); |
| 335 |
} |
| 336 |
|
| 337 |
return $c; |
| 338 |
} |
| 339 |
} |
| 340 |
|