Color.cpp 11 KB

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