/src/com/ichi2/themes/HtmlColors.java

https://github.com/loonfox/Anki-Android · Java · 233 lines · 219 code · 5 blank · 9 comment · 20 complexity · da7b38dcadbb4804b371e493318184bd MD5 · raw file

  1. package com.ichi2.themes;
  2. import java.util.HashMap;
  3. import java.util.Locale;
  4. import java.util.Map;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. public class HtmlColors {
  8. private static final Pattern fHtmlColorPattern = Pattern.compile(
  9. "((?:color|background)\\s*[=:]\\s*\"?)((?:[a-z]+|#[0-9a-f]+|rgb\\([0-9]+,\\s*[0-9],+\\s*[0-9]+\\)))([\";\\s])", Pattern.CASE_INSENSITIVE);
  10. private static final Pattern fShortHexColorPattern = Pattern.compile("^#([0-9a-f])([0-9a-f])([0-9a-f])$", Pattern.CASE_INSENSITIVE);
  11. private static final Pattern fLongHexColorPattern = Pattern.compile("^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$", Pattern.CASE_INSENSITIVE);
  12. private static final Pattern fRgbColorPattern = Pattern.compile("^rgb\\(([0-9]+)\\s*,\\s*([0-9]+)\\s*,\\s*([0-9]+)\\)$", Pattern.CASE_INSENSITIVE);
  13. private static final Pattern fClozeStylePattern = Pattern.compile("(.cloze\\s*\\{[^}]*color:\\s*#)[0-9a-f]{6}(;[^}]*\\})", Pattern.CASE_INSENSITIVE);
  14. private static final Pattern fDiffStylePattern = Pattern.compile("");
  15. public static String nameToHex(String name) {
  16. if (sColorsMap == null) {
  17. sColorsMap = new HashMap<String, String>();
  18. for (int i = 0; i < fColorsRawList.length; i+=2) {
  19. sColorsMap.put(fColorsRawList[i].toLowerCase(Locale.US), fColorsRawList[i+1].toLowerCase(Locale.US));
  20. }
  21. }
  22. name = name.toLowerCase(Locale.US);
  23. if (sColorsMap.containsKey(name)) {
  24. return sColorsMap.get(name);
  25. }
  26. return name;
  27. }
  28. /**
  29. * Returns a string where all colors have been inverted. It applies to anything that is in a tag and looks like
  30. * #FFFFFF Example: Here only #000000 will be replaced (#777777 is content) <span style="color: #000000;">Code
  31. * #777777 is the grey color</span> This is done with a state machine with 2 states: - 0: within content - 1: within
  32. * a tag
  33. */
  34. public static String invertColors(String text) {
  35. StringBuffer sb = new StringBuffer();
  36. Matcher m1 = fHtmlColorPattern.matcher(text);
  37. while (m1.find()) {
  38. // Convert names to hex
  39. String color = HtmlColors.nameToHex(m1.group(2));
  40. Matcher m2 = null;
  41. try {
  42. if (color.length() == 4 && color.charAt(0) == '#') {
  43. m2 = fShortHexColorPattern.matcher(color);
  44. if (m2.find()) {
  45. color = String.format(Locale.US, "#%x%x%x",
  46. 0xf - Integer.parseInt(m2.group(1), 16),
  47. 0xf - Integer.parseInt(m2.group(2), 16),
  48. 0xf - Integer.parseInt(m2.group(3), 16));
  49. }
  50. } else if (color.length() == 7 && color.charAt(0) == '#') {
  51. m2 = fLongHexColorPattern.matcher(color);
  52. if (m2.find()) {
  53. color = String.format(Locale.US, "#%02x%02x%02x",
  54. 0xff - Integer.parseInt(m2.group(1), 16),
  55. 0xff - Integer.parseInt(m2.group(2), 16),
  56. 0xff - Integer.parseInt(m2.group(3), 16));
  57. }
  58. } else if (color.length() > 9 && color.toLowerCase().startsWith("rgb")) {
  59. m2 = fRgbColorPattern.matcher(color);
  60. if (m2.find()) {
  61. color = String.format(Locale.US, "rgb(%d, %d, %d)",
  62. 0xff - Integer.parseInt(m2.group(1)),
  63. 0xff - Integer.parseInt(m2.group(2)),
  64. 0xff - Integer.parseInt(m2.group(3)));
  65. }
  66. }
  67. } catch (NumberFormatException e) {
  68. // shouldn't happen but ignore anyway
  69. }
  70. m1.appendReplacement(sb, m1.group(1) + color + m1.group(3));
  71. }
  72. m1.appendTail(sb);
  73. String invertedText = sb.toString();
  74. // fix style for cloze to light blue instead of inverted blue which ends up as yellow
  75. Matcher mc = fClozeStylePattern.matcher(invertedText);
  76. invertedText = mc.replaceAll("$10088ff$2");
  77. return invertedText;
  78. }
  79. private static Map<String, String> sColorsMap = null;
  80. private static final String[] fColorsRawList= new String[]{
  81. "AliceBlue", "#F0F8FF",
  82. "AntiqueWhite", "#FAEBD7",
  83. "Aqua", "#00FFFF",
  84. "Aquamarine", "#7FFFD4",
  85. "Azure", "#F0FFFF",
  86. "Beige", "#F5F5DC",
  87. "Bisque", "#FFE4C4",
  88. "Black", "#000000",
  89. "BlanchedAlmond", "#FFEBCD",
  90. "Blue", "#0000FF",
  91. "BlueViolet", "#8A2BE2",
  92. "Brown", "#A52A2A",
  93. "BurlyWood", "#DEB887",
  94. "CadetBlue", "#5F9EA0",
  95. "Chartreuse", "#7FFF00",
  96. "Chocolate", "#D2691E",
  97. "Coral", "#FF7F50",
  98. "CornflowerBlue", "#6495ED",
  99. "Cornsilk", "#FFF8DC",
  100. "Crimson", "#DC143C",
  101. "Cyan", "#00FFFF",
  102. "DarkBlue", "#00008B",
  103. "DarkCyan", "#008B8B",
  104. "DarkGoldenRod", "#B8860B",
  105. "DarkGray", "#A9A9A9",
  106. "DarkGrey", "#A9A9A9",
  107. "DarkGreen", "#006400",
  108. "DarkKhaki", "#BDB76B",
  109. "DarkMagenta", "#8B008B",
  110. "DarkOliveGreen", "#556B2F",
  111. "Darkorange", "#FF8C00",
  112. "DarkOrchid", "#9932CC",
  113. "DarkRed", "#8B0000",
  114. "DarkSalmon", "#E9967A",
  115. "DarkSeaGreen", "#8FBC8F",
  116. "DarkSlateBlue", "#483D8B",
  117. "DarkSlateGray", "#2F4F4F",
  118. "DarkSlateGrey", "#2F4F4F",
  119. "DarkTurquoise", "#00CED1",
  120. "DarkViolet", "#9400D3",
  121. "DeepPink", "#FF1493",
  122. "DeepSkyBlue", "#00BFFF",
  123. "DimGray", "#696969",
  124. "DimGrey", "#696969",
  125. "DodgerBlue", "#1E90FF",
  126. "FireBrick", "#B22222",
  127. "FloralWhite", "#FFFAF0",
  128. "ForestGreen", "#228B22",
  129. "Fuchsia", "#FF00FF",
  130. "Gainsboro", "#DCDCDC",
  131. "GhostWhite", "#F8F8FF",
  132. "Gold", "#FFD700",
  133. "GoldenRod", "#DAA520",
  134. "Gray", "#808080",
  135. "Grey", "#808080",
  136. "Green", "#008000",
  137. "GreenYellow", "#ADFF2F",
  138. "HoneyDew", "#F0FFF0",
  139. "HotPink", "#FF69B4",
  140. "IndianRed", "#CD5C5C",
  141. "Indigo", "#4B0082",
  142. "Ivory", "#FFFFF0",
  143. "Khaki", "#F0E68C",
  144. "Lavender", "#E6E6FA",
  145. "LavenderBlush", "#FFF0F5",
  146. "LawnGreen", "#7CFC00",
  147. "LemonChiffon", "#FFFACD",
  148. "LightBlue", "#ADD8E6",
  149. "LightCoral", "#F08080",
  150. "LightCyan", "#E0FFFF",
  151. "LightGoldenRodYellow", "#FAFAD2",
  152. "LightGray", "#D3D3D3",
  153. "LightGrey", "#D3D3D3",
  154. "LightGreen", "#90EE90",
  155. "LightPink", "#FFB6C1",
  156. "LightSalmon", "#FFA07A",
  157. "LightSeaGreen", "#20B2AA",
  158. "LightSkyBlue", "#87CEFA",
  159. "LightSlateGray", "#778899",
  160. "LightSlateGrey", "#778899",
  161. "LightSteelBlue", "#B0C4DE",
  162. "LightYellow", "#FFFFE0",
  163. "Lime", "#00FF00",
  164. "LimeGreen", "#32CD32",
  165. "Linen", "#FAF0E6",
  166. "Magenta", "#FF00FF",
  167. "Maroon", "#800000",
  168. "MediumAquaMarine", "#66CDAA",
  169. "MediumBlue", "#0000CD",
  170. "MediumOrchid", "#BA55D3",
  171. "MediumPurple", "#9370D8",
  172. "MediumSeaGreen", "#3CB371",
  173. "MediumSlateBlue", "#7B68EE",
  174. "MediumSpringGreen", "#00FA9A",
  175. "MediumTurquoise", "#48D1CC",
  176. "MediumVioletRed", "#C71585",
  177. "MidnightBlue", "#191970",
  178. "MintCream", "#F5FFFA",
  179. "MistyRose", "#FFE4E1",
  180. "Moccasin", "#FFE4B5",
  181. "NavajoWhite", "#FFDEAD",
  182. "Navy", "#000080",
  183. "OldLace", "#FDF5E6",
  184. "Olive", "#808000",
  185. "OliveDrab", "#6B8E23",
  186. "Orange", "#FFA500",
  187. "OrangeRed", "#FF4500",
  188. "Orchid", "#DA70D6",
  189. "PaleGoldenRod", "#EEE8AA",
  190. "PaleGreen", "#98FB98",
  191. "PaleTurquoise", "#AFEEEE",
  192. "PaleVioletRed", "#D87093",
  193. "PapayaWhip", "#FFEFD5",
  194. "PeachPuff", "#FFDAB9",
  195. "Peru", "#CD853F",
  196. "Pink", "#FFC0CB",
  197. "Plum", "#DDA0DD",
  198. "PowderBlue", "#B0E0E6",
  199. "Purple", "#800080",
  200. "Red", "#FF0000",
  201. "RosyBrown", "#BC8F8F",
  202. "RoyalBlue", "#4169E1",
  203. "SaddleBrown", "#8B4513",
  204. "Salmon", "#FA8072",
  205. "SandyBrown", "#F4A460",
  206. "SeaGreen", "#2E8B57",
  207. "SeaShell", "#FFF5EE",
  208. "Sienna", "#A0522D",
  209. "Silver", "#C0C0C0",
  210. "SkyBlue", "#87CEEB",
  211. "SlateBlue", "#6A5ACD",
  212. "SlateGray", "#708090",
  213. "SlateGrey", "#708090",
  214. "Snow", "#FFFAFA",
  215. "SpringGreen", "#00FF7F",
  216. "SteelBlue", "#4682B4",
  217. "Tan", "#D2B48C",
  218. "Teal", "#008080",
  219. "Thistle", "#D8BFD8",
  220. "Tomato", "#FF6347",
  221. "Turquoise", "#40E0D0",
  222. "Violet", "#EE82EE",
  223. "Wheat", "#F5DEB3",
  224. "White", "#FFFFFF",
  225. "WhiteSmoke", "#F5F5F5",
  226. "Yellow", "#FFFF00",
  227. "YellowGreen", "#9ACD32"};
  228. }