JAL-2360 refactoring for JalviewColourScheme enum,
[jalview.git] / src / jalview / schemes / UserColourScheme.java
index 4bb54f9..1865518 100755 (executable)
@@ -23,6 +23,7 @@ package jalview.schemes;
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
+import jalview.util.ColorUtils;
 
 import java.awt.Color;
 import java.util.Map;
@@ -63,12 +64,12 @@ public class UserColourScheme extends ResidueColourScheme
   public UserColourScheme(String colour)
   {
     super(ResidueProperties.aaIndex);
-    Color col = getColourFromString(colour);
+    Color col = ColorUtils.parseColourString(colour);
 
     if (col == null)
     {
       System.out.println("Making colour from name: " + colour);
-      col = createColourFromName(colour);
+      col = ColorUtils.createColourFromName(colour);
     }
 
     colors = new Color[24];
@@ -100,86 +101,6 @@ public class UserColourScheme extends ResidueColourScheme
   }
 
   /**
-   * 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>
-   * </ul>
-   * 
-   * @param colour
-   * @return the parsed colour, or null if parsing fails
-   */
-  public static Color getColourFromString(String colour)
-  {
-    if (colour == null)
-    {
-      return null;
-    }
-    colour = colour.trim();
-
-    Color col = null;
-    try
-    {
-      int value = Integer.parseInt(colour, 16);
-      col = new Color(value);
-    } catch (NumberFormatException ex)
-    {
-    }
-
-    if (col == null)
-    {
-      col = ColourSchemeProperty.getAWTColorFromName(colour);
-    }
-
-    if (col == null)
-    {
-      try
-      {
-        String[] tokens = colour.split(",");
-        if (tokens.length == 3)
-        {
-          int r = Integer.parseInt(tokens[0].trim());
-          int g = Integer.parseInt(tokens[1].trim());
-          int b = Integer.parseInt(tokens[2].trim());
-          col = new Color(r, g, b);
-        }
-      } catch (Exception ex)
-      {
-        // non-numeric token or out of 0-255 range
-      }
-    }
-
-    return col;
-  }
-
-  public static Color createColourFromName(String name)
-  {
-    int r, g, b;
-
-    int lsize = name.length();
-    int start = 0, end = lsize / 3;
-
-    int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
-
-    r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
-    start = end;
-    end += lsize / 3;
-    if (end > lsize)
-    {
-      end = lsize;
-    }
-
-    g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
-
-    b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
-
-    Color color = new Color(r, g, b);
-
-    return color;
-  }
-
-  /**
    * Parse and save residue colours specified as (for example)
    * 
    * <pre>
@@ -230,7 +151,7 @@ public class UserColourScheme extends ResidueColourScheme
             {
               if (lowerCaseColours[i] == null)
               {
-                lowerCaseColours[i] = getColourFromString(colour);
+                lowerCaseColours[i] = ColorUtils.parseColourString(colour);
               }
             }
 
@@ -243,11 +164,11 @@ public class UserColourScheme extends ResidueColourScheme
             {
               lowerCaseColours = new Color[23];
             }
-            lowerCaseColours[colIndex] = getColourFromString(colour);
+            lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour);
           }
           else
           {
-            colors[colIndex] = getColourFromString(colour);
+            colors[colIndex] = ColorUtils.parseColourString(colour);
           }
         }
       }
@@ -313,4 +234,18 @@ public class UserColourScheme extends ResidueColourScheme
     return super.findColour(c);
   }
 
+  /**
+   * Answers the customised name of the colour scheme, if it has one, else
+   * "User Defined"
+   */
+  @Override
+  public String getSchemeName()
+  {
+    if (schemeName != null && schemeName.length() > 0)
+    {
+      return schemeName;
+    }
+    return JalviewColourScheme.UserDefined.toString();
+  }
+
 }