Color.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <AK/Assertions.h>
  2. #include <LibDraw/Color.h>
  3. #include <LibDraw/SystemTheme.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. Color::Color(NamedColor named)
  7. {
  8. struct {
  9. u8 r;
  10. u8 g;
  11. u8 b;
  12. } rgb;
  13. switch (named) {
  14. case Black:
  15. rgb = { 0, 0, 0 };
  16. break;
  17. case White:
  18. rgb = { 255, 255, 255 };
  19. break;
  20. case Red:
  21. rgb = { 255, 0, 0 };
  22. break;
  23. case Green:
  24. rgb = { 0, 255, 0 };
  25. break;
  26. case Cyan:
  27. rgb = { 0, 255, 255 };
  28. break;
  29. case DarkCyan:
  30. rgb = { 0, 127, 127 };
  31. break;
  32. case MidCyan:
  33. rgb = { 0, 192, 192 };
  34. break;
  35. case Blue:
  36. rgb = { 0, 0, 255 };
  37. break;
  38. case Yellow:
  39. rgb = { 255, 255, 0 };
  40. break;
  41. case Magenta:
  42. rgb = { 255, 0, 255 };
  43. break;
  44. case DarkGray:
  45. rgb = { 64, 64, 64 };
  46. break;
  47. case MidGray:
  48. rgb = { 127, 127, 127 };
  49. break;
  50. case LightGray:
  51. rgb = { 192, 192, 192 };
  52. break;
  53. case MidGreen:
  54. rgb = { 0, 192, 0 };
  55. break;
  56. case MidBlue:
  57. rgb = { 0, 0, 192 };
  58. break;
  59. case MidRed:
  60. rgb = { 192, 0, 0 };
  61. break;
  62. case MidMagenta:
  63. rgb = { 192, 0, 192 };
  64. break;
  65. case DarkGreen:
  66. rgb = { 0, 128, 0 };
  67. break;
  68. case DarkBlue:
  69. rgb = { 0, 0, 128 };
  70. break;
  71. case DarkRed:
  72. rgb = { 128, 0, 0 };
  73. break;
  74. case WarmGray:
  75. rgb = { 212, 208, 200 };
  76. break;
  77. default:
  78. ASSERT_NOT_REACHED();
  79. break;
  80. }
  81. m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
  82. }
  83. String Color::to_string() const
  84. {
  85. return String::format("#%b%b%b%b", red(), green(), blue(), alpha());
  86. }
  87. Optional<Color> Color::from_string(const StringView& string)
  88. {
  89. if (string.is_empty())
  90. return {};
  91. struct ColorAndWebName {
  92. RGBA32 color;
  93. const char* name;
  94. };
  95. const ColorAndWebName web_colors[] = {
  96. // CSS Level 1
  97. { 0x000000, "black" },
  98. { 0xc0c0c0, "silver" },
  99. { 0x808080, "gray" },
  100. { 0xffffff, "white" },
  101. { 0x800000, "maroon" },
  102. { 0xff0000, "red" },
  103. { 0x800080, "purple" },
  104. { 0xff00ff, "fuchsia" },
  105. { 0x008000, "green" },
  106. { 0x00ff00, "lime" },
  107. { 0x808000, "olive" },
  108. { 0xffff00, "yellow" },
  109. { 0x000080, "navy" },
  110. { 0x0000ff, "blue" },
  111. { 0x008080, "teal" },
  112. { 0x00ffff, "aqua" },
  113. // CSS Level 2 (Revision 1)
  114. { 0xffa500, "orange" },
  115. // CSS Color Module Level 3
  116. { 0xf0f8ff, "aliceblue" },
  117. { 0xfaebd7, "antiquewhite" },
  118. { 0x7fffd4, "aquamarine" },
  119. { 0xf0ffff, "azure" },
  120. { 0xf5f5dc, "beige" },
  121. { 0xffe4c4, "bisque" },
  122. { 0xffebcd, "blanchedalmond" },
  123. { 0x8a2be2, "blueviolet" },
  124. { 0xa52a2a, "brown" },
  125. { 0xdeb887, "burlywood" },
  126. { 0x5f9ea0, "cadetblue" },
  127. { 0x7fff00, "chartreuse" },
  128. { 0xd2691e, "chocolate" },
  129. { 0xff7f50, "coral" },
  130. { 0x6495ed, "cornflowerblue" },
  131. { 0xfff8dc, "cornsilk" },
  132. { 0xdc143c, "crimson" },
  133. { 0x00ffff, "cyan" },
  134. { 0x00008b, "darkblue" },
  135. { 0x008b8b, "darkcyan" },
  136. { 0xb8860b, "darkgoldenrod" },
  137. { 0xa9a9a9, "darkgray" },
  138. { 0x006400, "darkgreen" },
  139. { 0xa9a9a9, "darkgrey" },
  140. { 0xbdb76b, "darkkhaki" },
  141. { 0x8b008b, "darkmagenta" },
  142. { 0x556b2f, "darkolivegreen" },
  143. { 0xff8c00, "darkorange" },
  144. { 0x9932cc, "darkorchid" },
  145. { 0x8b0000, "darkred" },
  146. { 0xe9967a, "darksalmon" },
  147. { 0x8fbc8f, "darkseagreen" },
  148. { 0x483d8b, "darkslateblue" },
  149. { 0x2f4f4f, "darkslategray" },
  150. { 0x2f4f4f, "darkslategrey" },
  151. { 0x00ced1, "darkturquoise" },
  152. { 0x9400d3, "darkviolet" },
  153. { 0xff1493, "deeppink" },
  154. { 0x00bfff, "deepskyblue" },
  155. { 0x696969, "dimgray" },
  156. { 0x696969, "dimgrey" },
  157. { 0x1e90ff, "dodgerblue" },
  158. { 0xb22222, "firebrick" },
  159. { 0xfffaf0, "floralwhite" },
  160. { 0x228b22, "forestgreen" },
  161. { 0xdcdcdc, "gainsboro" },
  162. { 0xf8f8ff, "ghostwhite" },
  163. { 0xffd700, "gold" },
  164. { 0xdaa520, "goldenrod" },
  165. { 0xadff2f, "greenyellow" },
  166. { 0x808080, "grey" },
  167. { 0xf0fff0, "honeydew" },
  168. { 0xff69b4, "hotpink" },
  169. { 0xcd5c5c, "indianred" },
  170. { 0x4b0082, "indigo" },
  171. { 0xfffff0, "ivory" },
  172. { 0xf0e68c, "khaki" },
  173. { 0xe6e6fa, "lavender" },
  174. { 0xfff0f5, "lavenderblush" },
  175. { 0x7cfc00, "lawngreen" },
  176. { 0xfffacd, "lemonchiffon" },
  177. { 0xadd8e6, "lightblue" },
  178. { 0xf08080, "lightcoral" },
  179. { 0xe0ffff, "lightcyan" },
  180. { 0xfafad2, "lightgoldenrody" },
  181. { 0xd3d3d3, "lightgray" },
  182. { 0x90ee90, "lightgreen" },
  183. { 0xd3d3d3, "lightgrey" },
  184. { 0xffb6c1, "lightpink" },
  185. { 0xffa07a, "lightsalmon" },
  186. { 0x20b2aa, "lightseagreen" },
  187. { 0x87cefa, "lightskyblue" },
  188. { 0x778899, "lightslategray" },
  189. { 0x778899, "lightslategrey" },
  190. { 0xb0c4de, "lightsteelblue" },
  191. { 0xffffe0, "lightyellow" },
  192. { 0x32cd32, "limegreen" },
  193. { 0xfaf0e6, "linen" },
  194. { 0xff00ff, "magenta" },
  195. { 0x66cdaa, "mediumaquamarin" },
  196. { 0x0000cd, "mediumblue" },
  197. { 0xba55d3, "mediumorchid" },
  198. { 0x9370db, "mediumpurple" },
  199. { 0x3cb371, "mediumseagreen" },
  200. { 0x7b68ee, "mediumslateblue" },
  201. { 0x00fa9a, "mediumspringgre" },
  202. { 0x48d1cc, "mediumturquoise" },
  203. { 0xc71585, "mediumvioletred" },
  204. { 0x191970, "midnightblue" },
  205. { 0xf5fffa, "mintcream" },
  206. { 0xffe4e1, "mistyrose" },
  207. { 0xffe4b5, "moccasin" },
  208. { 0xffdead, "navajowhite" },
  209. { 0xfdf5e6, "oldlace" },
  210. { 0x6b8e23, "olivedrab" },
  211. { 0xff4500, "orangered" },
  212. { 0xda70d6, "orchid" },
  213. { 0xeee8aa, "palegoldenrod" },
  214. { 0x98fb98, "palegreen" },
  215. { 0xafeeee, "paleturquoise" },
  216. { 0xdb7093, "palevioletred" },
  217. { 0xffefd5, "papayawhip" },
  218. { 0xffdab9, "peachpuff" },
  219. { 0xcd853f, "peru" },
  220. { 0xffc0cb, "pink" },
  221. { 0xdda0dd, "plum" },
  222. { 0xb0e0e6, "powderblue" },
  223. { 0xbc8f8f, "rosybrown" },
  224. { 0x4169e1, "royalblue" },
  225. { 0x8b4513, "saddlebrown" },
  226. { 0xfa8072, "salmon" },
  227. { 0xf4a460, "sandybrown" },
  228. { 0x2e8b57, "seagreen" },
  229. { 0xfff5ee, "seashell" },
  230. { 0xa0522d, "sienna" },
  231. { 0x87ceeb, "skyblue" },
  232. { 0x6a5acd, "slateblue" },
  233. { 0x708090, "slategray" },
  234. { 0x708090, "slategrey" },
  235. { 0xfffafa, "snow" },
  236. { 0x00ff7f, "springgreen" },
  237. { 0x4682b4, "steelblue" },
  238. { 0xd2b48c, "tan" },
  239. { 0xd8bfd8, "thistle" },
  240. { 0xff6347, "tomato" },
  241. { 0x40e0d0, "turquoise" },
  242. { 0xee82ee, "violet" },
  243. { 0xf5deb3, "wheat" },
  244. { 0xf5f5f5, "whitesmoke" },
  245. { 0x9acd32, "yellowgreen" },
  246. // CSS Color Module Level 4
  247. { 0x663399, "rebeccapurple" },
  248. // (Fallback)
  249. { 0x000000, nullptr }
  250. };
  251. for (size_t i = 0; web_colors[i].name; ++i) {
  252. if (string == web_colors[i].name)
  253. return Color::from_rgb(web_colors[i].color);
  254. }
  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() != 7 && string.length() != 9)
  273. return {};
  274. auto to_hex = [&](char c1, char c2) -> Optional<u8> {
  275. auto nib1 = hex_nibble_to_u8(c1);
  276. auto nib2 = hex_nibble_to_u8(c2);
  277. if (!nib1.has_value() || !nib2.has_value())
  278. return {};
  279. return nib1.value() << 4 | nib2.value();
  280. };
  281. Optional<u8> r = to_hex(string[1], string[2]);
  282. Optional<u8> g = to_hex(string[3], string[4]);
  283. Optional<u8> b = to_hex(string[5], string[6]);
  284. Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
  285. if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
  286. return {};
  287. return Color(r.value(), g.value(), b.value(), a.value());
  288. }