Color.cpp 11 KB

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