2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.schemes;
21 import jalview.datamodel.SequenceFeature;
23 import java.awt.Color;
26 * Value and/or thresholded colour scale used for colouring by annotation and
32 public class GraduatedColor
34 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or
39 float lr, lg, lb, dr, dg, db;
42 * linear scaling parameters, base, minimum colour threshold, range of linear
43 * scale from lower to upper
45 float base, range, thrsh;
48 * when true, colour from u to u-d rather than u to u+d
50 boolean tolow = false;
53 * when false, min/max range has been manually set so should not be
54 * dynamically adjusted.
56 boolean autoScale = true;
59 * construct a graduatedColor object from simple parameters
65 * color low->high from min->max
67 public GraduatedColor(Color low, Color high, float min, float max)
71 lr = low.getRed() / 255f;
72 lg = low.getGreen() / 255f;
73 lb = low.getBlue() / 255f;
74 dr = (high.getRed() / 255f) - lr;
75 dg = (high.getGreen() / 255f) - lg;
76 db = (high.getBlue() / 255f) - lb;
89 public GraduatedColor(GraduatedColor oldcs)
100 thresholdState = oldcs.thresholdState;
102 autoScale = oldcs.autoScale;
103 colourByLabel = oldcs.colourByLabel;
107 * make a new gradient from an old one with a different scale range
113 public GraduatedColor(GraduatedColor oldcs, float min, float max)
116 updateBounds(min, max);
119 public Color getMinColor()
121 return new Color(lr, lg, lb);
124 public Color getMaxColor()
126 return new Color(lr + dr, lg + dg, lb + db);
131 * @return true if original min/max scale was from high to low
133 public boolean getTolow()
138 public void setTolow(boolean tolower)
143 public boolean isColored(SequenceFeature feature)
145 float val = feature.getScore();
146 if (val == Float.NaN)
150 if (this.thresholdState == AnnotationColourGradient.NO_THRESHOLD)
154 if (this.thrsh == Float.NaN)
158 boolean rtn = thresholdState == AnnotationColourGradient.ABOVE_THRESHOLD;
161 return !rtn; // ? !tolow : tolow;
165 return rtn; // ? tolow : !tolow;
170 * default implementor of a getColourFromString method. TODO: abstract an
171 * interface enabling pluggable colour from string
173 private UserColourScheme ucs = null;
175 private boolean colourByLabel = false;
179 * @return true if colourByLabel style is set
181 public boolean isColourByLabel()
183 return colourByLabel;
187 * @param colourByLabel
188 * the colourByLabel to set
190 public void setColourByLabel(boolean colourByLabel)
192 this.colourByLabel = colourByLabel;
195 public Color findColor(SequenceFeature feature)
199 // TODO: allow user defined feature label colourschemes. Colour space is
200 // {type,regex,%anytype%}x{description string, regex, keyword}
203 ucs = new UserColourScheme();
205 return ucs.createColourFromName(feature.getDescription());
209 return getMaxColor();
211 float scr = feature.getScore();
212 if (scr == Float.NaN)
214 return getMinColor();
216 float scl = (scr - base) / range;
229 return new Color(lr + scl * dr, lg + scl * dg, lb + scl * db);
232 public void setThresh(float value)
237 public float getThresh()
242 public void setThreshType(int aboveThreshold)
244 thresholdState = aboveThreshold;
247 public int getThreshType()
249 return thresholdState;
252 public float getMax()
254 // regenerate the original values passed in to the constructor
255 return (tolow) ? base : (base + range);
258 public float getMin()
260 // regenerate the original value passed in to the constructor
261 return (tolow) ? (base + range) : base;
264 public boolean isAutoScale()
269 public void setAutoScaled(boolean autoscale)
271 autoScale = autoscale;
275 * update the base and range appropriatly for the given minmax range
278 * float[] {min,max} array containing minmax range for the associated
281 public void updateBounds(float min, float max)