JAL-3215 remove redundant min/max constants, refactor ctr, add tests
[jalview.git] / src / jalview / schemes / ScoreColourScheme.java
index eae76e1..442306d 100755 (executable)
@@ -28,10 +28,13 @@ import jalview.util.Comparison;
 import java.awt.Color;
 
 /**
- * DOCUMENT ME!
- * 
- * @author $author$
- * @version $Revision$
+ * A base class for colour schemes which define a graduated colour range based
+ * on
+ * <ul>
+ * <li>a minimum colour</li>
+ * <li>a maximum colour</li>
+ * <li>a score assigned to each residue</li>
+ * </ul>
  */
 public class ScoreColourScheme extends ResidueColourScheme
 {
@@ -42,23 +45,19 @@ public class ScoreColourScheme extends ResidueColourScheme
   public double[] scores;
 
   /**
-   * Creates a new ScoreColourScheme object.
+   * 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
    * @param scores
-   *          DOCUMENT ME!
-   * @param min
-   *          DOCUMENT ME!
-   * @param max
-   *          DOCUMENT ME!
+   *          per residue, indices matching colors lookup
    */
-  public ScoreColourScheme(int symbolIndex[], double[] scores, double min,
-          double max)
+  public ScoreColourScheme(int symbolIndex[], double[] scores)
   {
     super(symbolIndex);
-
+    setMinMax(scores);
     this.scores = scores;
-    this.min = min;
-    this.max = max;
 
     // Make colours in constructor
     // Why wasn't this done earlier?
@@ -84,6 +83,26 @@ public class ScoreColourScheme extends ResidueColourScheme
     }
   }
 
+  /**
+   * Inspects score values and saves the minimum and maximum
+   * 
+   * @param vals
+   */
+  void setMinMax(double[] vals)
+  {
+    double dMin = Double.MAX_VALUE;
+    double dMax = -Double.MAX_VALUE;
+
+    for (int i = 0; i < vals.length - 1; i++)
+    {
+      dMin = Math.min(dMin, vals[i]);
+      dMax = Math.max(dMax, vals[i]);
+    }
+
+    this.min = vals.length == 0 ? 0d : dMin;
+    this.max = vals.length == 0 ? 0d : dMax;
+  }
+
   @Override
   public Color findColour(char c, int j, SequenceI seq)
   {
@@ -121,6 +140,6 @@ public class ScoreColourScheme extends ResidueColourScheme
   public ColourSchemeI getInstance(AlignViewportI view,
           AnnotatedCollectionI coll)
   {
-    return new ScoreColourScheme(symbolIndex, scores, min, max);
+    return new ScoreColourScheme(symbolIndex, scores);
   }
 }