JAL-2362 return correct lower-case colour; Javadoc; unit test
[jalview.git] / src / jalview / schemes / UserColourScheme.java
index 9ae14ca..4bb54f9 100755 (executable)
@@ -99,6 +99,17 @@ public class UserColourScheme extends ResidueColourScheme
     return schemeName;
   }
 
+  /**
+   * 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)
@@ -125,19 +136,21 @@ public class UserColourScheme extends ResidueColourScheme
     {
       try
       {
-        java.util.StringTokenizer st = new java.util.StringTokenizer(
-                colour, ",");
-        int r = Integer.parseInt(st.nextToken());
-        int g = Integer.parseInt(st.nextToken());
-        int b = Integer.parseInt(st.nextToken());
-        col = new Color(r, g, b);
+        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)
@@ -166,6 +179,21 @@ public class UserColourScheme extends ResidueColourScheme
     return color;
   }
 
+  /**
+   * Parse and save residue colours specified as (for example)
+   * 
+   * <pre>
+   *     D,E=red; K,R,H=0022FF; c=100,50,75
+   * </pre>
+   * 
+   * This should be a semi-colon separated list of colours, which may be defined
+   * by colour name, hex value or comma-separated RGB triple. Each colour is
+   * defined for a comma-separated list of amino acid single letter codes. (Note
+   * that this also allows a colour scheme to be defined for ACGT, but not for
+   * U.)
+   * 
+   * @param paramValue
+   */
   public void parseAppletParameter(String paramValue)
   {
     // TODO: need a function to generate appletParameter colour string from a
@@ -184,16 +212,15 @@ public class UserColourScheme extends ResidueColourScheme
         st2 = new StringTokenizer(residues, " ,");
         while (st2.hasMoreTokens())
         {
-          token = st2.nextToken();
+          String residue = st2.nextToken();
 
-          if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
+          int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
+          if (colIndex == -1)
           {
             continue;
           }
 
-          int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
-
-          if (token.equalsIgnoreCase("lowerCase"))
+          if (residue.equalsIgnoreCase("lowerCase"))
           {
             if (lowerCaseColours == null)
             {
@@ -210,7 +237,7 @@ public class UserColourScheme extends ResidueColourScheme
             continue;
           }
 
-          if (token.equals(token.toLowerCase()))
+          if (residue.equals(residue.toLowerCase()))
           {
             if (lowerCaseColours == null)
             {
@@ -267,4 +294,23 @@ public class UserColourScheme extends ResidueColourScheme
     lowerCaseColours = lcolours;
   }
 
+  /**
+   * Returns the colour for the given residue character. If the residue is
+   * lower-case, and there is a specific colour defined for lower case, that
+   * colour is returned, else the colour for the upper case residue.
+   */
+  @Override
+  public Color findColour(char c)
+  {
+    if ('a' <= c && c <= 'z' && lowerCaseColours != null)
+    {
+      Color colour = lowerCaseColours[symbolIndex[c]];
+      if (colour != null)
+      {
+        return colour;
+      }
+    }
+    return super.findColour(c);
+  }
+
 }