Color.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. Color::Color(NamedColor named)
  39. {
  40. if (named == Transparent) {
  41. m_value = 0;
  42. return;
  43. }
  44. struct {
  45. u8 r;
  46. u8 g;
  47. u8 b;
  48. } rgb;
  49. switch (named) {
  50. case Black:
  51. rgb = { 0, 0, 0 };
  52. break;
  53. case White:
  54. rgb = { 255, 255, 255 };
  55. break;
  56. case Red:
  57. rgb = { 255, 0, 0 };
  58. break;
  59. case Green:
  60. rgb = { 0, 255, 0 };
  61. break;
  62. case Cyan:
  63. rgb = { 0, 255, 255 };
  64. break;
  65. case DarkCyan:
  66. rgb = { 0, 127, 127 };
  67. break;
  68. case MidCyan:
  69. rgb = { 0, 192, 192 };
  70. break;
  71. case Blue:
  72. rgb = { 0, 0, 255 };
  73. break;
  74. case Yellow:
  75. rgb = { 255, 255, 0 };
  76. break;
  77. case Magenta:
  78. rgb = { 255, 0, 255 };
  79. break;
  80. case DarkGray:
  81. rgb = { 64, 64, 64 };
  82. break;
  83. case MidGray:
  84. rgb = { 127, 127, 127 };
  85. break;
  86. case LightGray:
  87. rgb = { 192, 192, 192 };
  88. break;
  89. case MidGreen:
  90. rgb = { 0, 192, 0 };
  91. break;
  92. case MidBlue:
  93. rgb = { 0, 0, 192 };
  94. break;
  95. case MidRed:
  96. rgb = { 192, 0, 0 };
  97. break;
  98. case MidMagenta:
  99. rgb = { 192, 0, 192 };
  100. break;
  101. case DarkGreen:
  102. rgb = { 0, 128, 0 };
  103. break;
  104. case DarkBlue:
  105. rgb = { 0, 0, 128 };
  106. break;
  107. case DarkRed:
  108. rgb = { 128, 0, 0 };
  109. break;
  110. case WarmGray:
  111. rgb = { 212, 208, 200 };
  112. break;
  113. default:
  114. ASSERT_NOT_REACHED();
  115. break;
  116. }
  117. m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
  118. }
  119. String Color::to_string() const
  120. {
  121. return String::format("#%02x%02x%02x%02x", red(), green(), blue(), alpha());
  122. }
  123. String Color::to_string_without_alpha() const
  124. {
  125. return String::format("#%02x%02x%02x", red(), green(), blue());
  126. }
  127. static Optional<Color> parse_rgb_color(const StringView& string)
  128. {
  129. ASSERT(string.starts_with("rgb("));
  130. ASSERT(string.ends_with(")"));
  131. auto substring = string.substring_view(4, string.length() - 5);
  132. auto parts = substring.split_view(',');
  133. if (parts.size() != 3)
  134. return {};
  135. auto r = parts[0].to_uint().value_or(256);
  136. auto g = parts[1].to_uint().value_or(256);
  137. auto b = parts[2].to_uint().value_or(256);
  138. if (r > 255 || g > 255 || b > 255)
  139. return {};
  140. return Color(r, g, b);
  141. }
  142. static Optional<Color> parse_rgba_color(const StringView& string)
  143. {
  144. ASSERT(string.starts_with("rgba("));
  145. ASSERT(string.ends_with(")"));
  146. auto substring = string.substring_view(5, string.length() - 6);
  147. auto parts = substring.split_view(',');
  148. if (parts.size() != 4)
  149. return {};
  150. auto r = parts[0].to_int().value_or(256);
  151. auto g = parts[1].to_int().value_or(256);
  152. auto b = parts[2].to_int().value_or(256);
  153. double alpha = strtod(parts[3].to_string().characters(), nullptr);
  154. unsigned a = alpha * 255;
  155. if (r > 255 || g > 255 || b > 255 || a > 255)
  156. return {};
  157. return Color(r, g, b, a);
  158. }
  159. Optional<Color> Color::from_string(const StringView& string)
  160. {
  161. if (string.is_empty())
  162. return {};
  163. struct ColorAndWebName {
  164. constexpr ColorAndWebName(RGBA32 c, const char* n)
  165. : color(c)
  166. , name(n)
  167. {
  168. }
  169. RGBA32 color;
  170. StringView name;
  171. };
  172. constexpr ColorAndWebName web_colors[] = {
  173. // CSS Level 1
  174. { 0x000000, "black" },
  175. { 0xc0c0c0, "silver" },
  176. { 0x808080, "gray" },
  177. { 0xffffff, "white" },
  178. { 0x800000, "maroon" },
  179. { 0xff0000, "red" },
  180. { 0x800080, "purple" },
  181. { 0xff00ff, "fuchsia" },
  182. { 0x008000, "green" },
  183. { 0x00ff00, "lime" },
  184. { 0x808000, "olive" },
  185. { 0xffff00, "yellow" },
  186. { 0x000080, "navy" },
  187. { 0x0000ff, "blue" },
  188. { 0x008080, "teal" },
  189. { 0x00ffff, "aqua" },
  190. // CSS Level 2 (Revision 1)
  191. { 0xffa500, "orange" },
  192. // CSS Color Module Level 3
  193. { 0xf0f8ff, "aliceblue" },
  194. { 0xfaebd7, "antiquewhite" },
  195. { 0x7fffd4, "aquamarine" },
  196. { 0xf0ffff, "azure" },
  197. { 0xf5f5dc, "beige" },
  198. { 0xffe4c4, "bisque" },
  199. { 0xffebcd, "blanchedalmond" },
  200. { 0x8a2be2, "blueviolet" },
  201. { 0xa52a2a, "brown" },
  202. { 0xdeb887, "burlywood" },
  203. { 0x5f9ea0, "cadetblue" },
  204. { 0x7fff00, "chartreuse" },
  205. { 0xd2691e, "chocolate" },
  206. { 0xff7f50, "coral" },
  207. { 0x6495ed, "cornflowerblue" },
  208. { 0xfff8dc, "cornsilk" },
  209. { 0xdc143c, "crimson" },
  210. { 0x00ffff, "cyan" },
  211. { 0x00008b, "darkblue" },
  212. { 0x008b8b, "darkcyan" },
  213. { 0xb8860b, "darkgoldenrod" },
  214. { 0xa9a9a9, "darkgray" },
  215. { 0x006400, "darkgreen" },
  216. { 0xa9a9a9, "darkgrey" },
  217. { 0xbdb76b, "darkkhaki" },
  218. { 0x8b008b, "darkmagenta" },
  219. { 0x556b2f, "darkolivegreen" },
  220. { 0xff8c00, "darkorange" },
  221. { 0x9932cc, "darkorchid" },
  222. { 0x8b0000, "darkred" },
  223. { 0xe9967a, "darksalmon" },
  224. { 0x8fbc8f, "darkseagreen" },
  225. { 0x483d8b, "darkslateblue" },
  226. { 0x2f4f4f, "darkslategray" },
  227. { 0x2f4f4f, "darkslategrey" },
  228. { 0x00ced1, "darkturquoise" },
  229. { 0x9400d3, "darkviolet" },
  230. { 0xff1493, "deeppink" },
  231. { 0x00bfff, "deepskyblue" },
  232. { 0x696969, "dimgray" },
  233. { 0x696969, "dimgrey" },
  234. { 0x1e90ff, "dodgerblue" },
  235. { 0xb22222, "firebrick" },
  236. { 0xfffaf0, "floralwhite" },
  237. { 0x228b22, "forestgreen" },
  238. { 0xdcdcdc, "gainsboro" },
  239. { 0xf8f8ff, "ghostwhite" },
  240. { 0xffd700, "gold" },
  241. { 0xdaa520, "goldenrod" },
  242. { 0xadff2f, "greenyellow" },
  243. { 0x808080, "grey" },
  244. { 0xf0fff0, "honeydew" },
  245. { 0xff69b4, "hotpink" },
  246. { 0xcd5c5c, "indianred" },
  247. { 0x4b0082, "indigo" },
  248. { 0xfffff0, "ivory" },
  249. { 0xf0e68c, "khaki" },
  250. { 0xe6e6fa, "lavender" },
  251. { 0xfff0f5, "lavenderblush" },
  252. { 0x7cfc00, "lawngreen" },
  253. { 0xfffacd, "lemonchiffon" },
  254. { 0xadd8e6, "lightblue" },
  255. { 0xf08080, "lightcoral" },
  256. { 0xe0ffff, "lightcyan" },
  257. { 0xfafad2, "lightgoldenrodyellow" },
  258. { 0xd3d3d3, "lightgray" },
  259. { 0x90ee90, "lightgreen" },
  260. { 0xd3d3d3, "lightgrey" },
  261. { 0xffb6c1, "lightpink" },
  262. { 0xffa07a, "lightsalmon" },
  263. { 0x20b2aa, "lightseagreen" },
  264. { 0x87cefa, "lightskyblue" },
  265. { 0x778899, "lightslategray" },
  266. { 0x778899, "lightslategrey" },
  267. { 0xb0c4de, "lightsteelblue" },
  268. { 0xffffe0, "lightyellow" },
  269. { 0x32cd32, "limegreen" },
  270. { 0xfaf0e6, "linen" },
  271. { 0xff00ff, "magenta" },
  272. { 0x66cdaa, "mediumaquamarine" },
  273. { 0x0000cd, "mediumblue" },
  274. { 0xba55d3, "mediumorchid" },
  275. { 0x9370db, "mediumpurple" },
  276. { 0x3cb371, "mediumseagreen" },
  277. { 0x7b68ee, "mediumslateblue" },
  278. { 0x00fa9a, "mediumspringgreen" },
  279. { 0x48d1cc, "mediumturquoise" },
  280. { 0xc71585, "mediumvioletred" },
  281. { 0x191970, "midnightblue" },
  282. { 0xf5fffa, "mintcream" },
  283. { 0xffe4e1, "mistyrose" },
  284. { 0xffe4b5, "moccasin" },
  285. { 0xffdead, "navajowhite" },
  286. { 0xfdf5e6, "oldlace" },
  287. { 0x6b8e23, "olivedrab" },
  288. { 0xff4500, "orangered" },
  289. { 0xda70d6, "orchid" },
  290. { 0xeee8aa, "palegoldenrod" },
  291. { 0x98fb98, "palegreen" },
  292. { 0xafeeee, "paleturquoise" },
  293. { 0xdb7093, "palevioletred" },
  294. { 0xffefd5, "papayawhip" },
  295. { 0xffdab9, "peachpuff" },
  296. { 0xcd853f, "peru" },
  297. { 0xffc0cb, "pink" },
  298. { 0xdda0dd, "plum" },
  299. { 0xb0e0e6, "powderblue" },
  300. { 0xbc8f8f, "rosybrown" },
  301. { 0x4169e1, "royalblue" },
  302. { 0x8b4513, "saddlebrown" },
  303. { 0xfa8072, "salmon" },
  304. { 0xf4a460, "sandybrown" },
  305. { 0x2e8b57, "seagreen" },
  306. { 0xfff5ee, "seashell" },
  307. { 0xa0522d, "sienna" },
  308. { 0x87ceeb, "skyblue" },
  309. { 0x6a5acd, "slateblue" },
  310. { 0x708090, "slategray" },
  311. { 0x708090, "slategrey" },
  312. { 0xfffafa, "snow" },
  313. { 0x00ff7f, "springgreen" },
  314. { 0x4682b4, "steelblue" },
  315. { 0xd2b48c, "tan" },
  316. { 0xd8bfd8, "thistle" },
  317. { 0xff6347, "tomato" },
  318. { 0x40e0d0, "turquoise" },
  319. { 0xee82ee, "violet" },
  320. { 0xf5deb3, "wheat" },
  321. { 0xf5f5f5, "whitesmoke" },
  322. { 0x9acd32, "yellowgreen" },
  323. // CSS Color Module Level 4
  324. { 0x663399, "rebeccapurple" },
  325. // (Fallback)
  326. { 0x000000, nullptr }
  327. };
  328. for (size_t i = 0; !web_colors[i].name.is_null(); ++i) {
  329. if (string == web_colors[i].name)
  330. return Color::from_rgb(web_colors[i].color);
  331. }
  332. if (string.starts_with("rgb(") && string.ends_with(")"))
  333. return parse_rgb_color(string);
  334. if (string.starts_with("rgba(") && string.ends_with(")"))
  335. return parse_rgba_color(string);
  336. if (string[0] != '#')
  337. return {};
  338. auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
  339. if (!isxdigit(nibble))
  340. return {};
  341. if (nibble >= '0' && nibble <= '9')
  342. return nibble - '0';
  343. return 10 + (tolower(nibble) - 'a');
  344. };
  345. if (string.length() == 4) {
  346. Optional<u8> r = hex_nibble_to_u8(string[1]);
  347. Optional<u8> g = hex_nibble_to_u8(string[2]);
  348. Optional<u8> b = hex_nibble_to_u8(string[3]);
  349. if (!r.has_value() || !g.has_value() || !b.has_value())
  350. return {};
  351. return Color(r.value() * 17, g.value() * 17, b.value() * 17);
  352. }
  353. if (string.length() == 5) {
  354. Optional<u8> r = hex_nibble_to_u8(string[1]);
  355. Optional<u8> g = hex_nibble_to_u8(string[2]);
  356. Optional<u8> b = hex_nibble_to_u8(string[3]);
  357. Optional<u8> a = hex_nibble_to_u8(string[4]);
  358. if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
  359. return {};
  360. return Color(r.value() * 17, g.value() * 17, b.value() * 17, a.value() * 17);
  361. }
  362. if (string.length() != 7 && string.length() != 9)
  363. return {};
  364. auto to_hex = [&](char c1, char c2) -> Optional<u8> {
  365. auto nib1 = hex_nibble_to_u8(c1);
  366. auto nib2 = hex_nibble_to_u8(c2);
  367. if (!nib1.has_value() || !nib2.has_value())
  368. return {};
  369. return nib1.value() << 4 | nib2.value();
  370. };
  371. Optional<u8> r = to_hex(string[1], string[2]);
  372. Optional<u8> g = to_hex(string[3], string[4]);
  373. Optional<u8> b = to_hex(string[5], string[6]);
  374. Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
  375. if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
  376. return {};
  377. return Color(r.value(), g.value(), b.value(), a.value());
  378. }
  379. const LogStream& operator<<(const LogStream& stream, Color value)
  380. {
  381. return stream << value.to_string();
  382. }
  383. }
  384. bool IPC::encode(IPC::Encoder& encoder, const Color& color)
  385. {
  386. encoder << color.value();
  387. return true;
  388. }
  389. bool IPC::decode(IPC::Decoder& decoder, Color& color)
  390. {
  391. u32 rgba = 0;
  392. if (!decoder.decode(rgba))
  393. return false;
  394. color = Color::from_rgba(rgba);
  395. return true;
  396. }