2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.schemes;
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.AnnotatedCollectionI;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.Comparison;
28 import java.awt.Color;
31 * A base class for colour schemes which define a graduated colour range based
34 * <li>a minimum colour</li>
35 * <li>a maximum colour</li>
36 * <li>a score assigned to each residue</li>
39 public class ScoreColourScheme extends ResidueColourScheme
57 * a lookup where the index is a char e.g. 'R' or 'r', and the value
58 * is its position in the colour table lookup
60 * per residue, with indices corresponding to those for colour lookup
62 public ScoreColourScheme(String name, int[] symbolIndex, double[] scores,
63 Color minColour, Color maxColour)
66 this.schemeName = name;
67 this.minColour = minColour;
68 this.maxColour = maxColour;
73 int iSize = scores.length;
74 colors = new Color[scores.length];
75 for (int i = 0; i < iSize; i++)
77 colors[i] = getScoreColour(scores[i]);
82 * Computes a colour for a score by
84 * <li>first scaling the score linearly from 0 (minScore) to 1 (maxScore)</li>
85 * <li>then interpolating rgb values from minColour to maxColour</li>
87 * This method is used to make colours for residue scores, but may also be
88 * called to generate a colour for an intermediate score value (for example, a
91 public Color getScoreColour(double rawScore)
93 float score = (float) (rawScore - (float) min) / (float) (max - min);
94 score = Math.max(score, 0f);
95 score = Math.min(score, 1f);
97 int r = minColour.getRed()
98 + Math.round((maxColour.getRed() - minColour.getRed()) * score);
99 int g = minColour.getGreen()
101 (maxColour.getGreen() - minColour.getGreen()) * score);
102 int b = minColour.getBlue()
104 (maxColour.getBlue() - minColour.getBlue()) * score);
106 Color c = new Color(r, g, b);
111 * Inspects score values and saves the minimum and maximum
115 void setMinMax(double[] vals)
117 double dMin = Double.MAX_VALUE;
118 double dMax = -Double.MAX_VALUE;
120 for (int i = 0; i < vals.length - 1; i++)
122 dMin = Math.min(dMin, vals[i]);
123 dMax = Math.max(dMax, vals[i]);
126 this.min = vals.length == 0 ? 0d : dMin;
127 this.max = vals.length == 0 ? 0d : dMax;
131 public Color findColour(char c, int j, SequenceI seq)
133 if (Comparison.isGap(c))
137 return super.findColour(c);
141 public String getSchemeName()
147 * Returns a new instance of this colour scheme with which the given data may
151 public ColourSchemeI getInstance(AlignViewportI view,
152 AnnotatedCollectionI coll)
154 return new ScoreColourScheme(schemeName, symbolIndex, scores, minColour,