+ /**
+ * Set a color based on a text string. Color text string is of the form:
+ * <code>[ bold ] [ blink ] { foreground on background }</code>
+ *
+ * @param key the color key string
+ * @param text the text string
+ */
+ public void setColorFromString(final String key, final String text) {
+ boolean bold = false;
+ boolean blink = false;
+ String foreColor;
+ String backColor;
+ String token;
+
+ StringTokenizer tokenizer = new StringTokenizer(text);
+ token = tokenizer.nextToken();
+
+ if (token.toLowerCase().equals("rgb:")) {
+ // Foreground
+ int foreColorRGB = -1;
+ try {
+ foreColorRGB = Integer.parseInt(tokenizer.nextToken(), 16);
+ } catch (NumberFormatException e) {
+ // Default to white on black
+ foreColorRGB = 0xFFFFFF;
+ }
+
+ // "on"
+ if (!tokenizer.nextToken().toLowerCase().equals("on")) {
+ // Invalid line.
+ return;
+ }
+
+ // Background
+ int backColorRGB = -1;
+ try {
+ backColorRGB = Integer.parseInt(tokenizer.nextToken(), 16);
+ } catch (NumberFormatException e) {
+ backColorRGB = 0;
+ }
+
+ CellAttributes color = new CellAttributes();
+ color.setForeColorRGB(foreColorRGB);
+ color.setBackColorRGB(backColorRGB);
+ colors.put(key, color);
+ return;
+ }
+
+ while (token.equals("bold") || token.equals("blink")) {
+ if (token.equals("bold")) {
+ bold = true;
+ token = tokenizer.nextToken();
+ }
+ if (token.equals("blink")) {
+ blink = true;
+ token = tokenizer.nextToken();
+ }
+ }
+
+ // What's left is "blah on blah"
+ foreColor = token.toLowerCase();
+
+ if (!tokenizer.nextToken().toLowerCase().equals("on")) {
+ // Invalid line.
+ return;
+ }
+ backColor = tokenizer.nextToken().toLowerCase();
+
+ CellAttributes color = new CellAttributes();
+ if (bold) {
+ color.setBold(true);
+ }
+ if (blink) {
+ color.setBlink(true);
+ }
+ color.setForeColor(Color.getColor(foreColor));
+ color.setBackColor(Color.getColor(backColor));
+ colors.put(key, color);
+ }
+