Color.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2019-2023, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/ByteString.h>
  9. #include <AK/FloatingPointStringConversions.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/Color.h>
  13. #include <LibGfx/SystemTheme.h>
  14. #include <LibIPC/Decoder.h>
  15. #include <LibIPC/Encoder.h>
  16. #include <ctype.h>
  17. namespace Gfx {
  18. String Color::to_string() const
  19. {
  20. return MUST(String::formatted("#{:02x}{:02x}{:02x}{:02x}", red(), green(), blue(), alpha()));
  21. }
  22. String Color::to_string_without_alpha() const
  23. {
  24. return MUST(String::formatted("#{:02x}{:02x}{:02x}", red(), green(), blue()));
  25. }
  26. ByteString Color::to_byte_string() const
  27. {
  28. return to_string().to_byte_string();
  29. }
  30. ByteString Color::to_byte_string_without_alpha() const
  31. {
  32. return to_string_without_alpha().to_byte_string();
  33. }
  34. static Optional<Color> parse_rgb_color(StringView string)
  35. {
  36. VERIFY(string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive));
  37. VERIFY(string.ends_with(')'));
  38. auto substring = string.substring_view(4, string.length() - 5);
  39. auto parts = substring.split_view(',');
  40. if (parts.size() != 3)
  41. return {};
  42. auto r = parts[0].to_number<double>().map(AK::clamp_to<u8, double>);
  43. auto g = parts[1].to_number<double>().map(AK::clamp_to<u8, double>);
  44. auto b = parts[2].to_number<double>().map(AK::clamp_to<u8, double>);
  45. if (!r.has_value() || !g.has_value() || !b.has_value())
  46. return {};
  47. return Color(*r, *g, *b);
  48. }
  49. static Optional<Color> parse_rgba_color(StringView string)
  50. {
  51. VERIFY(string.starts_with("rgba("sv, CaseSensitivity::CaseInsensitive));
  52. VERIFY(string.ends_with(')'));
  53. auto substring = string.substring_view(5, string.length() - 6);
  54. auto parts = substring.split_view(',');
  55. if (parts.size() != 4)
  56. return {};
  57. auto r = parts[0].to_number<double>().map(AK::clamp_to<u8, double>);
  58. auto g = parts[1].to_number<double>().map(AK::clamp_to<u8, double>);
  59. auto b = parts[2].to_number<double>().map(AK::clamp_to<u8, double>);
  60. double alpha = 0;
  61. auto alpha_str = parts[3].trim_whitespace();
  62. char const* start = alpha_str.characters_without_null_termination();
  63. auto alpha_result = parse_first_floating_point(start, start + alpha_str.length());
  64. if (alpha_result.parsed_value())
  65. alpha = alpha_result.value;
  66. unsigned a = alpha * 255;
  67. if (!r.has_value() || !g.has_value() || !b.has_value() || a > 255)
  68. return {};
  69. return Color(*r, *g, *b, a);
  70. }
  71. Optional<Color> Color::from_named_css_color_string(StringView string)
  72. {
  73. if (string.is_empty())
  74. return {};
  75. struct WebColor {
  76. ARGB32 color;
  77. StringView name;
  78. };
  79. constexpr Array web_colors {
  80. // CSS Level 1
  81. WebColor { 0x000000, "black"sv },
  82. WebColor { 0xc0c0c0, "silver"sv },
  83. WebColor { 0x808080, "gray"sv },
  84. WebColor { 0xffffff, "white"sv },
  85. WebColor { 0x800000, "maroon"sv },
  86. WebColor { 0xff0000, "red"sv },
  87. WebColor { 0x800080, "purple"sv },
  88. WebColor { 0xff00ff, "fuchsia"sv },
  89. WebColor { 0x008000, "green"sv },
  90. WebColor { 0x00ff00, "lime"sv },
  91. WebColor { 0x808000, "olive"sv },
  92. WebColor { 0xffff00, "yellow"sv },
  93. WebColor { 0x000080, "navy"sv },
  94. WebColor { 0x0000ff, "blue"sv },
  95. WebColor { 0x008080, "teal"sv },
  96. WebColor { 0x00ffff, "aqua"sv },
  97. // CSS Level 2 (Revision 1)
  98. WebColor { 0xffa500, "orange"sv },
  99. // CSS Color Module Level 3
  100. WebColor { 0xf0f8ff, "aliceblue"sv },
  101. WebColor { 0xfaebd7, "antiquewhite"sv },
  102. WebColor { 0x7fffd4, "aquamarine"sv },
  103. WebColor { 0xf0ffff, "azure"sv },
  104. WebColor { 0xf5f5dc, "beige"sv },
  105. WebColor { 0xffe4c4, "bisque"sv },
  106. WebColor { 0xffebcd, "blanchedalmond"sv },
  107. WebColor { 0x8a2be2, "blueviolet"sv },
  108. WebColor { 0xa52a2a, "brown"sv },
  109. WebColor { 0xdeb887, "burlywood"sv },
  110. WebColor { 0x5f9ea0, "cadetblue"sv },
  111. WebColor { 0x7fff00, "chartreuse"sv },
  112. WebColor { 0xd2691e, "chocolate"sv },
  113. WebColor { 0xff7f50, "coral"sv },
  114. WebColor { 0x6495ed, "cornflowerblue"sv },
  115. WebColor { 0xfff8dc, "cornsilk"sv },
  116. WebColor { 0xdc143c, "crimson"sv },
  117. WebColor { 0x00ffff, "cyan"sv },
  118. WebColor { 0x00008b, "darkblue"sv },
  119. WebColor { 0x008b8b, "darkcyan"sv },
  120. WebColor { 0xb8860b, "darkgoldenrod"sv },
  121. WebColor { 0xa9a9a9, "darkgray"sv },
  122. WebColor { 0x006400, "darkgreen"sv },
  123. WebColor { 0xa9a9a9, "darkgrey"sv },
  124. WebColor { 0xbdb76b, "darkkhaki"sv },
  125. WebColor { 0x8b008b, "darkmagenta"sv },
  126. WebColor { 0x556b2f, "darkolivegreen"sv },
  127. WebColor { 0xff8c00, "darkorange"sv },
  128. WebColor { 0x9932cc, "darkorchid"sv },
  129. WebColor { 0x8b0000, "darkred"sv },
  130. WebColor { 0xe9967a, "darksalmon"sv },
  131. WebColor { 0x8fbc8f, "darkseagreen"sv },
  132. WebColor { 0x483d8b, "darkslateblue"sv },
  133. WebColor { 0x2f4f4f, "darkslategray"sv },
  134. WebColor { 0x2f4f4f, "darkslategrey"sv },
  135. WebColor { 0x00ced1, "darkturquoise"sv },
  136. WebColor { 0x9400d3, "darkviolet"sv },
  137. WebColor { 0xff1493, "deeppink"sv },
  138. WebColor { 0x00bfff, "deepskyblue"sv },
  139. WebColor { 0x696969, "dimgray"sv },
  140. WebColor { 0x696969, "dimgrey"sv },
  141. WebColor { 0x1e90ff, "dodgerblue"sv },
  142. WebColor { 0xb22222, "firebrick"sv },
  143. WebColor { 0xfffaf0, "floralwhite"sv },
  144. WebColor { 0x228b22, "forestgreen"sv },
  145. WebColor { 0xdcdcdc, "gainsboro"sv },
  146. WebColor { 0xf8f8ff, "ghostwhite"sv },
  147. WebColor { 0xffd700, "gold"sv },
  148. WebColor { 0xdaa520, "goldenrod"sv },
  149. WebColor { 0xadff2f, "greenyellow"sv },
  150. WebColor { 0x808080, "grey"sv },
  151. WebColor { 0xf0fff0, "honeydew"sv },
  152. WebColor { 0xff69b4, "hotpink"sv },
  153. WebColor { 0xcd5c5c, "indianred"sv },
  154. WebColor { 0x4b0082, "indigo"sv },
  155. WebColor { 0xfffff0, "ivory"sv },
  156. WebColor { 0xf0e68c, "khaki"sv },
  157. WebColor { 0xe6e6fa, "lavender"sv },
  158. WebColor { 0xfff0f5, "lavenderblush"sv },
  159. WebColor { 0x7cfc00, "lawngreen"sv },
  160. WebColor { 0xfffacd, "lemonchiffon"sv },
  161. WebColor { 0xadd8e6, "lightblue"sv },
  162. WebColor { 0xf08080, "lightcoral"sv },
  163. WebColor { 0xe0ffff, "lightcyan"sv },
  164. WebColor { 0xfafad2, "lightgoldenrodyellow"sv },
  165. WebColor { 0xd3d3d3, "lightgray"sv },
  166. WebColor { 0x90ee90, "lightgreen"sv },
  167. WebColor { 0xd3d3d3, "lightgrey"sv },
  168. WebColor { 0xffb6c1, "lightpink"sv },
  169. WebColor { 0xffa07a, "lightsalmon"sv },
  170. WebColor { 0x20b2aa, "lightseagreen"sv },
  171. WebColor { 0x87cefa, "lightskyblue"sv },
  172. WebColor { 0x778899, "lightslategray"sv },
  173. WebColor { 0x778899, "lightslategrey"sv },
  174. WebColor { 0xb0c4de, "lightsteelblue"sv },
  175. WebColor { 0xffffe0, "lightyellow"sv },
  176. WebColor { 0x32cd32, "limegreen"sv },
  177. WebColor { 0xfaf0e6, "linen"sv },
  178. WebColor { 0xff00ff, "magenta"sv },
  179. WebColor { 0x66cdaa, "mediumaquamarine"sv },
  180. WebColor { 0x0000cd, "mediumblue"sv },
  181. WebColor { 0xba55d3, "mediumorchid"sv },
  182. WebColor { 0x9370db, "mediumpurple"sv },
  183. WebColor { 0x3cb371, "mediumseagreen"sv },
  184. WebColor { 0x7b68ee, "mediumslateblue"sv },
  185. WebColor { 0x00fa9a, "mediumspringgreen"sv },
  186. WebColor { 0x48d1cc, "mediumturquoise"sv },
  187. WebColor { 0xc71585, "mediumvioletred"sv },
  188. WebColor { 0x191970, "midnightblue"sv },
  189. WebColor { 0xf5fffa, "mintcream"sv },
  190. WebColor { 0xffe4e1, "mistyrose"sv },
  191. WebColor { 0xffe4b5, "moccasin"sv },
  192. WebColor { 0xffdead, "navajowhite"sv },
  193. WebColor { 0xfdf5e6, "oldlace"sv },
  194. WebColor { 0x6b8e23, "olivedrab"sv },
  195. WebColor { 0xff4500, "orangered"sv },
  196. WebColor { 0xda70d6, "orchid"sv },
  197. WebColor { 0xeee8aa, "palegoldenrod"sv },
  198. WebColor { 0x98fb98, "palegreen"sv },
  199. WebColor { 0xafeeee, "paleturquoise"sv },
  200. WebColor { 0xdb7093, "palevioletred"sv },
  201. WebColor { 0xffefd5, "papayawhip"sv },
  202. WebColor { 0xffdab9, "peachpuff"sv },
  203. WebColor { 0xcd853f, "peru"sv },
  204. WebColor { 0xffc0cb, "pink"sv },
  205. WebColor { 0xdda0dd, "plum"sv },
  206. WebColor { 0xb0e0e6, "powderblue"sv },
  207. WebColor { 0xbc8f8f, "rosybrown"sv },
  208. WebColor { 0x4169e1, "royalblue"sv },
  209. WebColor { 0x8b4513, "saddlebrown"sv },
  210. WebColor { 0xfa8072, "salmon"sv },
  211. WebColor { 0xf4a460, "sandybrown"sv },
  212. WebColor { 0x2e8b57, "seagreen"sv },
  213. WebColor { 0xfff5ee, "seashell"sv },
  214. WebColor { 0xa0522d, "sienna"sv },
  215. WebColor { 0x87ceeb, "skyblue"sv },
  216. WebColor { 0x6a5acd, "slateblue"sv },
  217. WebColor { 0x708090, "slategray"sv },
  218. WebColor { 0x708090, "slategrey"sv },
  219. WebColor { 0xfffafa, "snow"sv },
  220. WebColor { 0x00ff7f, "springgreen"sv },
  221. WebColor { 0x4682b4, "steelblue"sv },
  222. WebColor { 0xd2b48c, "tan"sv },
  223. WebColor { 0xd8bfd8, "thistle"sv },
  224. WebColor { 0xff6347, "tomato"sv },
  225. WebColor { 0x40e0d0, "turquoise"sv },
  226. WebColor { 0xee82ee, "violet"sv },
  227. WebColor { 0xf5deb3, "wheat"sv },
  228. WebColor { 0xf5f5f5, "whitesmoke"sv },
  229. WebColor { 0x9acd32, "yellowgreen"sv },
  230. // CSS Color Module Level 4
  231. WebColor { 0x663399, "rebeccapurple"sv },
  232. };
  233. for (auto const& web_color : web_colors) {
  234. if (string.equals_ignoring_ascii_case(web_color.name))
  235. return Color::from_rgb(web_color.color);
  236. }
  237. return {};
  238. }
  239. Optional<Color> Color::from_string(StringView string)
  240. {
  241. if (string.is_empty())
  242. return {};
  243. if (string[0] == '#') {
  244. auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
  245. if (!isxdigit(nibble))
  246. return {};
  247. if (nibble >= '0' && nibble <= '9')
  248. return nibble - '0';
  249. return 10 + (tolower(nibble) - 'a');
  250. };
  251. if (string.length() == 4) {
  252. Optional<u8> r = hex_nibble_to_u8(string[1]);
  253. Optional<u8> g = hex_nibble_to_u8(string[2]);
  254. Optional<u8> b = hex_nibble_to_u8(string[3]);
  255. if (!r.has_value() || !g.has_value() || !b.has_value())
  256. return {};
  257. return Color(r.value() * 17, g.value() * 17, b.value() * 17);
  258. }
  259. if (string.length() == 5) {
  260. Optional<u8> r = hex_nibble_to_u8(string[1]);
  261. Optional<u8> g = hex_nibble_to_u8(string[2]);
  262. Optional<u8> b = hex_nibble_to_u8(string[3]);
  263. Optional<u8> a = hex_nibble_to_u8(string[4]);
  264. if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
  265. return {};
  266. return Color(r.value() * 17, g.value() * 17, b.value() * 17, a.value() * 17);
  267. }
  268. if (string.length() != 7 && string.length() != 9)
  269. return {};
  270. auto to_hex = [&](char c1, char c2) -> Optional<u8> {
  271. auto nib1 = hex_nibble_to_u8(c1);
  272. auto nib2 = hex_nibble_to_u8(c2);
  273. if (!nib1.has_value() || !nib2.has_value())
  274. return {};
  275. return nib1.value() << 4 | nib2.value();
  276. };
  277. Optional<u8> r = to_hex(string[1], string[2]);
  278. Optional<u8> g = to_hex(string[3], string[4]);
  279. Optional<u8> b = to_hex(string[5], string[6]);
  280. Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
  281. if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
  282. return {};
  283. return Color(r.value(), g.value(), b.value(), a.value());
  284. }
  285. if (string.starts_with("rgb("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
  286. return parse_rgb_color(string);
  287. if (string.starts_with("rgba("sv, CaseSensitivity::CaseInsensitive) && string.ends_with(')'))
  288. return parse_rgba_color(string);
  289. if (string.equals_ignoring_ascii_case("transparent"sv))
  290. return Color::from_argb(0x00000000);
  291. if (auto const color = from_named_css_color_string(string); color.has_value())
  292. return color;
  293. return {};
  294. }
  295. Vector<Color> Color::shades(u32 steps, float max) const
  296. {
  297. float shade = 1.f;
  298. float step = max / steps;
  299. Vector<Color> shades;
  300. for (u32 i = 0; i < steps; i++) {
  301. shade -= step;
  302. shades.append(this->darkened(shade));
  303. }
  304. return shades;
  305. }
  306. Vector<Color> Color::tints(u32 steps, float max) const
  307. {
  308. float shade = 1.f;
  309. float step = max / steps;
  310. Vector<Color> tints;
  311. for (u32 i = 0; i < steps; i++) {
  312. shade += step;
  313. tints.append(this->lightened(shade));
  314. }
  315. return tints;
  316. }
  317. }
  318. template<>
  319. ErrorOr<void> IPC::encode(Encoder& encoder, Color const& color)
  320. {
  321. return encoder.encode(color.value());
  322. }
  323. template<>
  324. ErrorOr<Gfx::Color> IPC::decode(Decoder& decoder)
  325. {
  326. auto rgba = TRY(decoder.decode<u32>());
  327. return Gfx::Color::from_argb(rgba);
  328. }
  329. ErrorOr<void> AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, Gfx::Color value)
  330. {
  331. return Formatter<StringView>::format(builder, value.to_byte_string());
  332. }