/**
* Constructor
*
+ * @param name
* @param symbolIndex
* 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
+ * @param theScores
* per residue, with indices corresponding to those for colour lookup
+ * @param minColour
+ * graduated colour at minimum score value
+ * @param maxColour
+ * graduated colour at maximum score value
*/
- public ScoreColourScheme(String name, int[] symbolIndex, double[] scores,
+ public ScoreColourScheme(String name, int[] symbolIndex, double[] theScores,
Color minColour, Color maxColour)
{
super(symbolIndex);
this.minColour = minColour;
this.maxColour = maxColour;
- setMinMax(scores);
- this.scores = scores;
+ setMinMax(theScores);
+ computeColours(theScores);
+ }
+
+ /**
+ * Calculates and saves a graduated score based colour for each residue
+ *
+ * @param theScores
+ */
+ protected void computeColours(double[] theScores)
+ {
+ this.scores = theScores;
- int iSize = scores.length;
- colors = new Color[scores.length];
+ int iSize = theScores.length;
+ colors = new Color[theScores.length];
for (int i = 0; i < iSize; i++)
{
- colors[i] = getScoreColour(scores[i]);
+ colors[i] = getScoreColour(theScores[i]);
}
}
double dMin = Double.MAX_VALUE;
double dMax = -Double.MAX_VALUE;
- for (int i = 0; i < vals.length - 1; i++)
+ for (int i = 0; i < vals.length; i++)
{
dMin = Math.min(dMin, vals[i]);
dMax = Math.max(dMax, vals[i]);
--- /dev/null
+package jalview.schemes;
+
+import static org.testng.Assert.assertEquals;
+
+import java.awt.Color;
+
+import org.testng.annotations.Test;
+
+public class ScoreColourSchemeTest
+{
+ @Test
+ public void testGetScoreColour()
+ {
+ Color minColour = new Color(100, 150, 50);
+ Color maxColour = new Color(20, 220, 90);
+
+ double[] scores = new double[] { 6.2d /* A */, -2.4d /* R */,
+ 7.9d /* N */ };
+ ScoreColourScheme cs = new ScoreColourScheme("test",
+ ResidueProperties.aaIndex, scores, minColour, maxColour);
+
+ /*
+ * basic sanity checks
+ */
+ assertEquals(cs.min, -2.4d);
+ assertEquals(cs.max, 7.9d);
+ assertEquals(cs.minColour, minColour);
+ assertEquals(cs.maxColour, maxColour);
+ assertEquals(cs.findColour('R'), minColour);
+ assertEquals(cs.findColour('n'), maxColour);
+
+ /*
+ * a score of 3 gets a graduated colour between (100, 150, 50) (-2.4) and
+ * (20, 220, 90) (7.9)
+ */
+ double score = 3d;
+ float f = (float) ((score - cs.min)/ (cs.max - cs.min)); // 5.4 / 10.3
+ assertEquals(f, 0.52427, 0.0001d);
+ int expectR = Math.round(100 + f * (20 - 100)); // 100 - 41.94 = 58.06 = 58
+ assertEquals(expectR, 58);
+ int expectG = Math.round(150 + f * (220 - 150)); // 150 + 36.699 = 187
+ assertEquals(expectG, 187);
+ int expectB = Math.round(50 + f * (90 - 50)); // 50 + 20.97 = 71
+ assertEquals(expectB, 71);
+
+ assertEquals(cs.getScoreColour(score),
+ new Color(expectR, expectG, expectB));
+ }
+}