JAL-3215 refactored ScoreColourScheme construction for scriptability
[jalview.git] / src / jalview / schemes / ScoreColourScheme.java
index 442306d..66bb5b4 100755 (executable)
@@ -38,52 +38,76 @@ import java.awt.Color;
  */
 public class ScoreColourScheme extends ResidueColourScheme
 {
-  public double min;
+  String schemeName;
 
-  public double max;
+  double min;
 
-  public double[] scores;
+  double max;
+
+  Color minColour;
+
+  Color maxColour;
+
+  double[] scores;
 
   /**
    * Constructor
    * 
    * @param symbolIndex
-   *          a lookup where the index is a character e.g. 'R' or 'r', and the
-   *          value is its position in the colour table lookup
+   *          a lookup where the index is a char e.g. 'R' or 'r', and the value
+   *          is its position in the colour table lookup
    * @param scores
-   *          per residue, indices matching colors lookup
+   *          per residue, with indices corresponding to those for colour lookup
    */
-  public ScoreColourScheme(int symbolIndex[], double[] scores)
+  public ScoreColourScheme(String name, int[] symbolIndex, double[] scores,
+          Color minColour, Color maxColour)
   {
     super(symbolIndex);
+    this.schemeName = name;
+    this.minColour = minColour;
+    this.maxColour = maxColour;
+
     setMinMax(scores);
     this.scores = scores;
 
-    // Make colours in constructor
-    // Why wasn't this done earlier?
     int iSize = scores.length;
     colors = new Color[scores.length];
     for (int i = 0; i < iSize; i++)
     {
-      /*
-       * scale score between min and max to the range 0.0 - 1.0
-       */
-      float score = (float) (scores[i] - (float) min) / (float) (max - min);
-
-      if (score > 1.0f)
-      {
-        score = 1.0f;
-      }
-
-      if (score < 0.0f)
-      {
-        score = 0.0f;
-      }
-      colors[i] = makeColour(score);
+      colors[i] = getScoreColour(scores[i]);
     }
   }
 
   /**
+   * Computes a colour for a score by
+   * <ul>
+   * <li>first scaling the score linearly from 0 (minScore) to 1 (maxScore)</li>
+   * <li>then interpolating rgb values from minColour to maxColour</li>
+   * </ul>
+   * This method is used to make colours for residue scores, but may also be
+   * called to generate a colour for an intermediate score value (for example, a
+   * column average).
+   */
+  public Color getScoreColour(double rawScore)
+  {
+    float score = (float) (rawScore - (float) min) / (float) (max - min);
+    score = Math.max(score, 0f);
+    score = Math.min(score, 1f);
+
+    int r = minColour.getRed()
+            + Math.round((maxColour.getRed() - minColour.getRed()) * score);
+    int g = minColour.getGreen()
+            + Math.round(
+                    (maxColour.getGreen() - minColour.getGreen()) * score);
+    int b = minColour.getBlue()
+            + Math.round(
+                    (maxColour.getBlue() - minColour.getBlue()) * score);
+
+    Color c = new Color(r, g, b);
+    return c;
+  }
+
+  /**
    * Inspects score values and saves the minimum and maximum
    * 
    * @param vals
@@ -113,23 +137,10 @@ public class ScoreColourScheme extends ResidueColourScheme
     return super.findColour(c);
   }
 
-  /**
-   * DOCUMENT ME!
-   * 
-   * @param c
-   *          DOCUMENT ME!
-   * 
-   * @return DOCUMENT ME!
-   */
-  public Color makeColour(float c)
-  {
-    return new Color(c, (float) 0.0, (float) 1.0 - c);
-  }
-
   @Override
   public String getSchemeName()
   {
-    return "Score";
+    return schemeName;
   }
 
   /**
@@ -140,6 +151,7 @@ public class ScoreColourScheme extends ResidueColourScheme
   public ColourSchemeI getInstance(AlignViewportI view,
           AnnotatedCollectionI coll)
   {
-    return new ScoreColourScheme(symbolIndex, scores);
+    return new ScoreColourScheme(schemeName, symbolIndex, scores, minColour,
+            maxColour);
   }
 }