JAL-3368 removed parsing web colours (now on a separate branch)
[jalview.git] / src / jalview / util / ColorUtils.java
index d4be322..d465632 100644 (file)
 package jalview.util;
 
 import java.awt.Color;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Random;
 
+/**
+ * A class with utility methods for manipulating AWT colours/colors
+ */
 public class ColorUtils
 {
+  private static final int MAX_CACHE_SIZE = 1729;
+
+  /*
+   * a cache for colours generated from text strings
+   */
+  static Map<String, Color> myColours = new HashMap<>();
 
   /**
    * Generates a random color, will mix with input color. Code taken from
@@ -196,8 +207,8 @@ public class ColorUtils
    * Parses a string into a Color, where the accepted formats are
    * <ul>
    * <li>an AWT colour name e.g. white</li>
-   * <li>a hex colour value (without prefix) e.g. ff0000</li>
-   * <li>an rgb triple e.g. 100,50,150</li>
+   * <li>a six digit rgb hex colour value (without prefix) e.g. ff0000</li>
+   * <li>a comma-separated rgb triple e.g. 100,50,150</li>
    * </ul>
    * 
    * @param colour
@@ -212,17 +223,20 @@ public class ColorUtils
     colour = colour.trim();
 
     Color col = null;
-    try
-    {
-      int value = Integer.parseInt(colour, 16);
-      col = new Color(value);
-    } catch (NumberFormatException ex)
+    if (colour.length() == 6 && StringUtils.isHexString(colour))
     {
+      try
+      {
+        int value = Integer.parseInt(colour, 16);
+        col = new Color(value);
+      } catch (NumberFormatException ex)
+      {
+      }
     }
 
     if (col == null)
     {
-      col = ColorUtils.getAWTColorFromName(colour);
+      col = ColorUtils.getColorFromName(colour);
     }
 
     if (col == null)
@@ -237,7 +251,7 @@ public class ColorUtils
           int b = Integer.parseInt(tokens[2].trim());
           col = new Color(r, g, b);
         }
-      } catch (Exception ex)
+      } catch (IllegalArgumentException ex)
       {
         // non-numeric token or out of 0-255 range
       }
@@ -260,6 +274,10 @@ public class ColorUtils
     {
       return Color.white;
     }
+    if (myColours.containsKey(name))
+    {
+      return myColours.get(name);
+    }
     int lsize = name.length();
     int start = 0;
     int end = lsize / 3;
@@ -291,69 +309,59 @@ public class ColorUtils
 
     Color color = new Color(r, g, b);
 
+    if (myColours.size() < MAX_CACHE_SIZE)
+    {
+      myColours.put(name, color);
+    }
+
     return color;
   }
 
   /**
    * Returns the Color constant for a given colour name e.g. "pink", or null if
-   * the name is not recognised
+   * the name is not recognised. Currently recognises only AWT colour names, but
+   * could be extended to support others e.g. standard html colour names.
    * 
    * @param name
    * @return
    */
-  public static Color getAWTColorFromName(String name)
+  public static Color getColorFromName(String name)
   {
     if (name == null)
     {
       return null;
     }
-    Color col = null;
-    name = name.toLowerCase();
-
     // or make a static map; or use reflection on the field name
-    switch (name)
+    switch (name.toLowerCase())
     {
     case "black":
-      col = Color.black;
-      break;
+      return Color.black;
     case "blue":
-      col = Color.blue;
-      break;
+      return Color.blue;
     case "cyan":
-      col = Color.cyan;
-      break;
+      return Color.cyan;
     case "darkgray":
-      col = Color.darkGray;
-      break;
+      return Color.darkGray;
     case "gray":
-      col = Color.gray;
-      break;
+      return Color.gray;
     case "green":
-      col = Color.green;
-      break;
+      return Color.green;
     case "lightgray":
-      col = Color.lightGray;
-      break;
+      return Color.lightGray;
     case "magenta":
-      col = Color.magenta;
-      break;
+      return Color.magenta;
     case "orange":
-      col = Color.orange;
-      break;
+      return Color.orange;
     case "pink":
-      col = Color.pink;
-      break;
+      return Color.pink;
     case "red":
-      col = Color.red;
-      break;
+      return Color.red;
     case "white":
-      col = Color.white;
-      break;
+      return Color.white;
     case "yellow":
-      col = Color.yellow;
-      break;
+      return Color.yellow;
+    default:
+      return null;
     }
-
-    return col;
   }
 }