1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:57:45 +00:00

LibGfx: Use StringView literals for CSS color list

And also remove the null terminating entry in favour of for-each loop.
This commit is contained in:
MacDue 2023-03-12 01:42:44 +00:00 committed by Linus Groh
parent b10ec6743f
commit b698d64ee9

View file

@ -82,179 +82,172 @@ Optional<Color> Color::from_string(StringView string)
if (string.is_empty()) if (string.is_empty())
return {}; return {};
struct ColorAndWebName { struct WebColor {
constexpr ColorAndWebName(ARGB32 c, char const* n)
: color(c)
, name(n != nullptr ? StringView { n, __builtin_strlen(n) } : StringView {})
{
}
ARGB32 color; ARGB32 color;
StringView name; StringView name;
}; };
constexpr ColorAndWebName web_colors[] = { constexpr Array web_colors {
// CSS Level 1 // CSS Level 1
{ 0x000000, "black" }, WebColor { 0x000000, "black"sv },
{ 0xc0c0c0, "silver" }, WebColor { 0xc0c0c0, "silver"sv },
{ 0x808080, "gray" }, WebColor { 0x808080, "gray"sv },
{ 0xffffff, "white" }, WebColor { 0xffffff, "white"sv },
{ 0x800000, "maroon" }, WebColor { 0x800000, "maroon"sv },
{ 0xff0000, "red" }, WebColor { 0xff0000, "red"sv },
{ 0x800080, "purple" }, WebColor { 0x800080, "purple"sv },
{ 0xff00ff, "fuchsia" }, WebColor { 0xff00ff, "fuchsia"sv },
{ 0x008000, "green" }, WebColor { 0x008000, "green"sv },
{ 0x00ff00, "lime" }, WebColor { 0x00ff00, "lime"sv },
{ 0x808000, "olive" }, WebColor { 0x808000, "olive"sv },
{ 0xffff00, "yellow" }, WebColor { 0xffff00, "yellow"sv },
{ 0x000080, "navy" }, WebColor { 0x000080, "navy"sv },
{ 0x0000ff, "blue" }, WebColor { 0x0000ff, "blue"sv },
{ 0x008080, "teal" }, WebColor { 0x008080, "teal"sv },
{ 0x00ffff, "aqua" }, WebColor { 0x00ffff, "aqua"sv },
// CSS Level 2 (Revision 1) // CSS Level 2 (Revision 1)
{ 0xffa500, "orange" }, WebColor { 0xffa500, "orange"sv },
// CSS Color Module Level 3 // CSS Color Module Level 3
{ 0xf0f8ff, "aliceblue" }, WebColor { 0xf0f8ff, "aliceblue"sv },
{ 0xfaebd7, "antiquewhite" }, WebColor { 0xfaebd7, "antiquewhite"sv },
{ 0x7fffd4, "aquamarine" }, WebColor { 0x7fffd4, "aquamarine"sv },
{ 0xf0ffff, "azure" }, WebColor { 0xf0ffff, "azure"sv },
{ 0xf5f5dc, "beige" }, WebColor { 0xf5f5dc, "beige"sv },
{ 0xffe4c4, "bisque" }, WebColor { 0xffe4c4, "bisque"sv },
{ 0xffebcd, "blanchedalmond" }, WebColor { 0xffebcd, "blanchedalmond"sv },
{ 0x8a2be2, "blueviolet" }, WebColor { 0x8a2be2, "blueviolet"sv },
{ 0xa52a2a, "brown" }, WebColor { 0xa52a2a, "brown"sv },
{ 0xdeb887, "burlywood" }, WebColor { 0xdeb887, "burlywood"sv },
{ 0x5f9ea0, "cadetblue" }, WebColor { 0x5f9ea0, "cadetblue"sv },
{ 0x7fff00, "chartreuse" }, WebColor { 0x7fff00, "chartreuse"sv },
{ 0xd2691e, "chocolate" }, WebColor { 0xd2691e, "chocolate"sv },
{ 0xff7f50, "coral" }, WebColor { 0xff7f50, "coral"sv },
{ 0x6495ed, "cornflowerblue" }, WebColor { 0x6495ed, "cornflowerblue"sv },
{ 0xfff8dc, "cornsilk" }, WebColor { 0xfff8dc, "cornsilk"sv },
{ 0xdc143c, "crimson" }, WebColor { 0xdc143c, "crimson"sv },
{ 0x00ffff, "cyan" }, WebColor { 0x00ffff, "cyan"sv },
{ 0x00008b, "darkblue" }, WebColor { 0x00008b, "darkblue"sv },
{ 0x008b8b, "darkcyan" }, WebColor { 0x008b8b, "darkcyan"sv },
{ 0xb8860b, "darkgoldenrod" }, WebColor { 0xb8860b, "darkgoldenrod"sv },
{ 0xa9a9a9, "darkgray" }, WebColor { 0xa9a9a9, "darkgray"sv },
{ 0x006400, "darkgreen" }, WebColor { 0x006400, "darkgreen"sv },
{ 0xa9a9a9, "darkgrey" }, WebColor { 0xa9a9a9, "darkgrey"sv },
{ 0xbdb76b, "darkkhaki" }, WebColor { 0xbdb76b, "darkkhaki"sv },
{ 0x8b008b, "darkmagenta" }, WebColor { 0x8b008b, "darkmagenta"sv },
{ 0x556b2f, "darkolivegreen" }, WebColor { 0x556b2f, "darkolivegreen"sv },
{ 0xff8c00, "darkorange" }, WebColor { 0xff8c00, "darkorange"sv },
{ 0x9932cc, "darkorchid" }, WebColor { 0x9932cc, "darkorchid"sv },
{ 0x8b0000, "darkred" }, WebColor { 0x8b0000, "darkred"sv },
{ 0xe9967a, "darksalmon" }, WebColor { 0xe9967a, "darksalmon"sv },
{ 0x8fbc8f, "darkseagreen" }, WebColor { 0x8fbc8f, "darkseagreen"sv },
{ 0x483d8b, "darkslateblue" }, WebColor { 0x483d8b, "darkslateblue"sv },
{ 0x2f4f4f, "darkslategray" }, WebColor { 0x2f4f4f, "darkslategray"sv },
{ 0x2f4f4f, "darkslategrey" }, WebColor { 0x2f4f4f, "darkslategrey"sv },
{ 0x00ced1, "darkturquoise" }, WebColor { 0x00ced1, "darkturquoise"sv },
{ 0x9400d3, "darkviolet" }, WebColor { 0x9400d3, "darkviolet"sv },
{ 0xff1493, "deeppink" }, WebColor { 0xff1493, "deeppink"sv },
{ 0x00bfff, "deepskyblue" }, WebColor { 0x00bfff, "deepskyblue"sv },
{ 0x696969, "dimgray" }, WebColor { 0x696969, "dimgray"sv },
{ 0x696969, "dimgrey" }, WebColor { 0x696969, "dimgrey"sv },
{ 0x1e90ff, "dodgerblue" }, WebColor { 0x1e90ff, "dodgerblue"sv },
{ 0xb22222, "firebrick" }, WebColor { 0xb22222, "firebrick"sv },
{ 0xfffaf0, "floralwhite" }, WebColor { 0xfffaf0, "floralwhite"sv },
{ 0x228b22, "forestgreen" }, WebColor { 0x228b22, "forestgreen"sv },
{ 0xdcdcdc, "gainsboro" }, WebColor { 0xdcdcdc, "gainsboro"sv },
{ 0xf8f8ff, "ghostwhite" }, WebColor { 0xf8f8ff, "ghostwhite"sv },
{ 0xffd700, "gold" }, WebColor { 0xffd700, "gold"sv },
{ 0xdaa520, "goldenrod" }, WebColor { 0xdaa520, "goldenrod"sv },
{ 0xadff2f, "greenyellow" }, WebColor { 0xadff2f, "greenyellow"sv },
{ 0x808080, "grey" }, WebColor { 0x808080, "grey"sv },
{ 0xf0fff0, "honeydew" }, WebColor { 0xf0fff0, "honeydew"sv },
{ 0xff69b4, "hotpink" }, WebColor { 0xff69b4, "hotpink"sv },
{ 0xcd5c5c, "indianred" }, WebColor { 0xcd5c5c, "indianred"sv },
{ 0x4b0082, "indigo" }, WebColor { 0x4b0082, "indigo"sv },
{ 0xfffff0, "ivory" }, WebColor { 0xfffff0, "ivory"sv },
{ 0xf0e68c, "khaki" }, WebColor { 0xf0e68c, "khaki"sv },
{ 0xe6e6fa, "lavender" }, WebColor { 0xe6e6fa, "lavender"sv },
{ 0xfff0f5, "lavenderblush" }, WebColor { 0xfff0f5, "lavenderblush"sv },
{ 0x7cfc00, "lawngreen" }, WebColor { 0x7cfc00, "lawngreen"sv },
{ 0xfffacd, "lemonchiffon" }, WebColor { 0xfffacd, "lemonchiffon"sv },
{ 0xadd8e6, "lightblue" }, WebColor { 0xadd8e6, "lightblue"sv },
{ 0xf08080, "lightcoral" }, WebColor { 0xf08080, "lightcoral"sv },
{ 0xe0ffff, "lightcyan" }, WebColor { 0xe0ffff, "lightcyan"sv },
{ 0xfafad2, "lightgoldenrodyellow" }, WebColor { 0xfafad2, "lightgoldenrodyellow"sv },
{ 0xd3d3d3, "lightgray" }, WebColor { 0xd3d3d3, "lightgray"sv },
{ 0x90ee90, "lightgreen" }, WebColor { 0x90ee90, "lightgreen"sv },
{ 0xd3d3d3, "lightgrey" }, WebColor { 0xd3d3d3, "lightgrey"sv },
{ 0xffb6c1, "lightpink" }, WebColor { 0xffb6c1, "lightpink"sv },
{ 0xffa07a, "lightsalmon" }, WebColor { 0xffa07a, "lightsalmon"sv },
{ 0x20b2aa, "lightseagreen" }, WebColor { 0x20b2aa, "lightseagreen"sv },
{ 0x87cefa, "lightskyblue" }, WebColor { 0x87cefa, "lightskyblue"sv },
{ 0x778899, "lightslategray" }, WebColor { 0x778899, "lightslategray"sv },
{ 0x778899, "lightslategrey" }, WebColor { 0x778899, "lightslategrey"sv },
{ 0xb0c4de, "lightsteelblue" }, WebColor { 0xb0c4de, "lightsteelblue"sv },
{ 0xffffe0, "lightyellow" }, WebColor { 0xffffe0, "lightyellow"sv },
{ 0x32cd32, "limegreen" }, WebColor { 0x32cd32, "limegreen"sv },
{ 0xfaf0e6, "linen" }, WebColor { 0xfaf0e6, "linen"sv },
{ 0xff00ff, "magenta" }, WebColor { 0xff00ff, "magenta"sv },
{ 0x66cdaa, "mediumaquamarine" }, WebColor { 0x66cdaa, "mediumaquamarine"sv },
{ 0x0000cd, "mediumblue" }, WebColor { 0x0000cd, "mediumblue"sv },
{ 0xba55d3, "mediumorchid" }, WebColor { 0xba55d3, "mediumorchid"sv },
{ 0x9370db, "mediumpurple" }, WebColor { 0x9370db, "mediumpurple"sv },
{ 0x3cb371, "mediumseagreen" }, WebColor { 0x3cb371, "mediumseagreen"sv },
{ 0x7b68ee, "mediumslateblue" }, WebColor { 0x7b68ee, "mediumslateblue"sv },
{ 0x00fa9a, "mediumspringgreen" }, WebColor { 0x00fa9a, "mediumspringgreen"sv },
{ 0x48d1cc, "mediumturquoise" }, WebColor { 0x48d1cc, "mediumturquoise"sv },
{ 0xc71585, "mediumvioletred" }, WebColor { 0xc71585, "mediumvioletred"sv },
{ 0x191970, "midnightblue" }, WebColor { 0x191970, "midnightblue"sv },
{ 0xf5fffa, "mintcream" }, WebColor { 0xf5fffa, "mintcream"sv },
{ 0xffe4e1, "mistyrose" }, WebColor { 0xffe4e1, "mistyrose"sv },
{ 0xffe4b5, "moccasin" }, WebColor { 0xffe4b5, "moccasin"sv },
{ 0xffdead, "navajowhite" }, WebColor { 0xffdead, "navajowhite"sv },
{ 0xfdf5e6, "oldlace" }, WebColor { 0xfdf5e6, "oldlace"sv },
{ 0x6b8e23, "olivedrab" }, WebColor { 0x6b8e23, "olivedrab"sv },
{ 0xff4500, "orangered" }, WebColor { 0xff4500, "orangered"sv },
{ 0xda70d6, "orchid" }, WebColor { 0xda70d6, "orchid"sv },
{ 0xeee8aa, "palegoldenrod" }, WebColor { 0xeee8aa, "palegoldenrod"sv },
{ 0x98fb98, "palegreen" }, WebColor { 0x98fb98, "palegreen"sv },
{ 0xafeeee, "paleturquoise" }, WebColor { 0xafeeee, "paleturquoise"sv },
{ 0xdb7093, "palevioletred" }, WebColor { 0xdb7093, "palevioletred"sv },
{ 0xffefd5, "papayawhip" }, WebColor { 0xffefd5, "papayawhip"sv },
{ 0xffdab9, "peachpuff" }, WebColor { 0xffdab9, "peachpuff"sv },
{ 0xcd853f, "peru" }, WebColor { 0xcd853f, "peru"sv },
{ 0xffc0cb, "pink" }, WebColor { 0xffc0cb, "pink"sv },
{ 0xdda0dd, "plum" }, WebColor { 0xdda0dd, "plum"sv },
{ 0xb0e0e6, "powderblue" }, WebColor { 0xb0e0e6, "powderblue"sv },
{ 0xbc8f8f, "rosybrown" }, WebColor { 0xbc8f8f, "rosybrown"sv },
{ 0x4169e1, "royalblue" }, WebColor { 0x4169e1, "royalblue"sv },
{ 0x8b4513, "saddlebrown" }, WebColor { 0x8b4513, "saddlebrown"sv },
{ 0xfa8072, "salmon" }, WebColor { 0xfa8072, "salmon"sv },
{ 0xf4a460, "sandybrown" }, WebColor { 0xf4a460, "sandybrown"sv },
{ 0x2e8b57, "seagreen" }, WebColor { 0x2e8b57, "seagreen"sv },
{ 0xfff5ee, "seashell" }, WebColor { 0xfff5ee, "seashell"sv },
{ 0xa0522d, "sienna" }, WebColor { 0xa0522d, "sienna"sv },
{ 0x87ceeb, "skyblue" }, WebColor { 0x87ceeb, "skyblue"sv },
{ 0x6a5acd, "slateblue" }, WebColor { 0x6a5acd, "slateblue"sv },
{ 0x708090, "slategray" }, WebColor { 0x708090, "slategray"sv },
{ 0x708090, "slategrey" }, WebColor { 0x708090, "slategrey"sv },
{ 0xfffafa, "snow" }, WebColor { 0xfffafa, "snow"sv },
{ 0x00ff7f, "springgreen" }, WebColor { 0x00ff7f, "springgreen"sv },
{ 0x4682b4, "steelblue" }, WebColor { 0x4682b4, "steelblue"sv },
{ 0xd2b48c, "tan" }, WebColor { 0xd2b48c, "tan"sv },
{ 0xd8bfd8, "thistle" }, WebColor { 0xd8bfd8, "thistle"sv },
{ 0xff6347, "tomato" }, WebColor { 0xff6347, "tomato"sv },
{ 0x40e0d0, "turquoise" }, WebColor { 0x40e0d0, "turquoise"sv },
{ 0xee82ee, "violet" }, WebColor { 0xee82ee, "violet"sv },
{ 0xf5deb3, "wheat" }, WebColor { 0xf5deb3, "wheat"sv },
{ 0xf5f5f5, "whitesmoke" }, WebColor { 0xf5f5f5, "whitesmoke"sv },
{ 0x9acd32, "yellowgreen" }, WebColor { 0x9acd32, "yellowgreen"sv },
// CSS Color Module Level 4 // CSS Color Module Level 4
{ 0x663399, "rebeccapurple" }, WebColor { 0x663399, "rebeccapurple"sv },
// (Fallback)
{ 0x000000, nullptr }
}; };
if (string.equals_ignoring_ascii_case("transparent"sv)) if (string.equals_ignoring_ascii_case("transparent"sv))
return Color::from_argb(0x00000000); return Color::from_argb(0x00000000);
for (size_t i = 0; !web_colors[i].name.is_null(); ++i) { for (auto const& web_color : web_colors) {
if (string.equals_ignoring_ascii_case(web_colors[i].name)) if (string.equals_ignoring_ascii_case(web_color.name))
return Color::from_rgb(web_colors[i].color); return Color::from_rgb(web_color.color);
} }
if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')')) if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))