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.FeatureColourI;
24 import jalview.datamodel.SequenceFeature;
26 import java.awt.Color;
29 * Value and/or thresholded colour scale used for colouring by annotation and
35 public class GraduatedColor
37 private static final Color[] colours = new Color[] { Color.blue,
40 private static int colourModulus = 0;
42 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or
47 float lr, lg, lb, dr, dg, db;
50 * linear scaling parameters, base, minimum colour threshold, range of linear
51 * scale from lower to upper
53 float base, range, thrsh;
56 * when true, colour from u to u-d rather than u to u+d
58 boolean tolow = false;
61 * when false, min/max range has been manually set so should not be
62 * dynamically adjusted.
64 boolean autoScale = true;
67 * construct a graduatedColor object from simple parameters
73 * color low->high from min->max
75 public GraduatedColor(Color low, Color high, float min, float max)
79 lr = low.getRed() / 255f;
80 lg = low.getGreen() / 255f;
81 lb = low.getBlue() / 255f;
82 dr = (high.getRed() / 255f) - lr;
83 dg = (high.getGreen() / 255f) - lg;
84 db = (high.getBlue() / 255f) - lb;
97 public GraduatedColor(GraduatedColor oldcs)
108 thresholdState = oldcs.thresholdState;
110 autoScale = oldcs.autoScale;
111 colourByLabel = oldcs.colourByLabel;
112 colourAlternately = oldcs.colourAlternately;
116 * make a new gradient from an old one with a different scale range
122 public GraduatedColor(GraduatedColor oldcs, float min, float max)
125 updateBounds(min, max);
128 public GraduatedColor(FeatureColourI col)
130 setColourByLabel(col.isColourByLabel());
133 public Color getMinColor()
135 return new Color(lr, lg, lb);
138 public Color getMaxColor()
140 return new Color(lr + dr, lg + dg, lb + db);
145 * @return true if original min/max scale was from high to low
147 public boolean getTolow()
152 public void setTolow(boolean tolower)
157 public boolean isColored(SequenceFeature feature)
159 float val = feature.getScore();
160 if (Float.isNaN(val))
164 if (this.thresholdState == AnnotationColourGradient.NO_THRESHOLD)
168 if (Float.isNaN(this.thrsh))
172 boolean rtn = thresholdState == AnnotationColourGradient.ABOVE_THRESHOLD;
175 return !rtn; // ? !tolow : tolow;
179 return rtn; // ? tolow : !tolow;
184 * default implementor of a getColourFromString method. TODO: abstract an
185 * interface enabling pluggable colour from string
187 private UserColourScheme ucs = null;
189 private boolean colourByLabel = false;
191 private boolean colourAlternately = false;
195 * @return true if colourByLabel style is set
197 public boolean isColourByLabel()
199 return colourByLabel;
203 * @param colourByLabel
204 * the colourByLabel to set
206 public void setColourByLabel(boolean colourByLabel)
208 this.colourByLabel = colourByLabel;
211 public Color findColor(SequenceFeature feature)
215 // TODO: allow user defined feature label colourschemes. Colour space is
216 // {type,regex,%anytype%}x{description string, regex, keyword}
219 ucs = new UserColourScheme();
221 return ucs.createColourFromName(feature.getDescription());
223 if (colourAlternately)
225 int minOrMax = feature.getFeatureNumber() % 2;
226 return minOrMax == 0 ? new Color(lr, lg, lb) : new Color(lr + dr, lg
231 return getMaxColor();
233 float scr = feature.getScore();
234 if (Float.isNaN(scr))
236 return getMinColor();
238 float scl = (scr - base) / range;
251 return new Color(lr + scl * dr, lg + scl * dg, lb + scl * db);
254 public void setThresh(float value)
259 public float getThresh()
264 public void setThreshType(int aboveThreshold)
266 thresholdState = aboveThreshold;
269 public int getThreshType()
271 return thresholdState;
274 public float getMax()
276 // regenerate the original values passed in to the constructor
277 return (tolow) ? base : (base + range);
280 public float getMin()
282 // regenerate the original value passed in to the constructor
283 return (tolow) ? (base + range) : base;
286 public boolean isAutoScale()
291 public void setAutoScaled(boolean autoscale)
293 autoScale = autoscale;
297 * update the base and range appropriatly for the given minmax range
300 * float[] {min,max} array containing minmax range for the associated
303 public void updateBounds(float min, float max)
319 public boolean isColourAlternately()
321 return colourAlternately;
324 public void setColourAlternately(boolean colourAlternately)
326 this.colourAlternately = colourAlternately;