Color.cpp 13 KB

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