- private void addEl(Element el, TextColor fore, TextColor back) {
- mapForegroundColor.put(el, fore);
- mapBackgroundColor.put(el, back);
+ /**
+ * Convert the given {@link String} value to a {@link TextColor}.
+ *
+ * @param value
+ * the {@link String} to convert
+ * @param defaultColor
+ * the default {@link TextColor} to return if the conversion
+ * failed
+ *
+ * @return the converted colour
+ */
+ static private TextColor convertToColor(String value, TextColor defaultColor) {
+ try {
+ if (value.startsWith("@")) {
+ int r = Integer.parseInt(value.substring(1, 3), 16);
+ int g = Integer.parseInt(value.substring(3, 5), 16);
+ int b = Integer.parseInt(value.substring(5, 7), 16);
+ return TextColor.Indexed.fromRGB(r, g, b);
+ } else if (value.startsWith("#")) {
+ int r = Integer.parseInt(value.substring(1, 3), 16);
+ int g = Integer.parseInt(value.substring(3, 5), 16);
+ int b = Integer.parseInt(value.substring(5, 7), 16);
+ return new TextColor.RGB(r, g, b);
+ } else if (value.replaceAll("[0-9]*", "").length() == 0) {
+ return new TextColor.Indexed(Integer.parseInt(value));
+ } else {
+ return TextColor.ANSI.valueOf(value);
+ }
+ } catch (Exception e) {
+ new Exception("Cannot convert value to colour: " + value, e)
+ .printStackTrace();
+ }
+
+ return defaultColor;