JAL-3368 removed parsing web colours (now on a separate branch)
[jalview.git] / src / jalview / util / ColorUtils.java
index 3eb080b..d465632 100644 (file)
@@ -29,9 +29,13 @@ 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
    */
@@ -203,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
@@ -219,18 +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))
     {
-      col = Platform.getColorFromName(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)
@@ -245,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
       }
@@ -313,15 +319,49 @@ public class ColorUtils
 
   /**
    * 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)
   {
-    return Platform.getColorFromName(name); // BH 2019 -- allows for wide range
-                                            // of JavaScript colors (for
-                                            // JavaScript only)
+    if (name == null)
+    {
+      return null;
+    }
+    // or make a static map; or use reflection on the field name
+    switch (name.toLowerCase())
+    {
+    case "black":
+      return Color.black;
+    case "blue":
+      return Color.blue;
+    case "cyan":
+      return Color.cyan;
+    case "darkgray":
+      return Color.darkGray;
+    case "gray":
+      return Color.gray;
+    case "green":
+      return Color.green;
+    case "lightgray":
+      return Color.lightGray;
+    case "magenta":
+      return Color.magenta;
+    case "orange":
+      return Color.orange;
+    case "pink":
+      return Color.pink;
+    case "red":
+      return Color.red;
+    case "white":
+      return Color.white;
+    case "yellow":
+      return Color.yellow;
+    default:
+      return null;
+    }
   }
 }