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.datamodel.SequenceFeature;
25 import java.awt.Color;
28 * Value and/or thresholded colour scale used for colouring by annotation and
34 public class GraduatedColor
36 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or
41 float lr, lg, lb, dr, dg, db;
44 * linear scaling parameters, base, minimum colour threshold, range of linear
45 * scale from lower to upper
47 float base, range, thrsh;
50 * when true, colour from u to u-d rather than u to u+d
52 boolean tolow = false;
55 * when false, min/max range has been manually set so should not be
56 * dynamically adjusted.
58 boolean autoScale = true;
61 * construct a graduatedColor object from simple parameters
67 * color low->high from min->max
69 public GraduatedColor(Color low, Color high, float min, float max)
73 lr = low.getRed() / 255f;
74 lg = low.getGreen() / 255f;
75 lb = low.getBlue() / 255f;
76 dr = (high.getRed() / 255f) - lr;
77 dg = (high.getGreen() / 255f) - lg;
78 db = (high.getBlue() / 255f) - lb;
91 public GraduatedColor(GraduatedColor oldcs)
102 thresholdState = oldcs.thresholdState;
104 autoScale = oldcs.autoScale;
105 colourByLabel = oldcs.colourByLabel;
109 * make a new gradient from an old one with a different scale range
115 public GraduatedColor(GraduatedColor oldcs, float min, float max)
118 updateBounds(min, max);
121 public Color getMinColor()
123 return new Color(lr, lg, lb);
126 public Color getMaxColor()
128 return new Color(lr + dr, lg + dg, lb + db);
133 * @return true if original min/max scale was from high to low
135 public boolean getTolow()
140 public void setTolow(boolean tolower)
145 public boolean isColored(SequenceFeature feature)
147 float val = feature.getScore();
148 if (Float.isNaN(val))
152 if (this.thresholdState == AnnotationColourGradient.NO_THRESHOLD)
156 if (Float.isNaN(this.thrsh))
160 boolean rtn = thresholdState == AnnotationColourGradient.ABOVE_THRESHOLD;
163 return !rtn; // ? !tolow : tolow;
167 return rtn; // ? tolow : !tolow;
172 * default implementor of a getColourFromString method. TODO: abstract an
173 * interface enabling pluggable colour from string
175 private UserColourScheme ucs = null;
177 private boolean colourByLabel = false;
181 * @return true if colourByLabel style is set
183 public boolean isColourByLabel()
185 return colourByLabel;
189 * @param colourByLabel
190 * the colourByLabel to set
192 public void setColourByLabel(boolean colourByLabel)
194 this.colourByLabel = colourByLabel;
197 public Color findColor(SequenceFeature feature)
201 // TODO: allow user defined feature label colourschemes. Colour space is
202 // {type,regex,%anytype%}x{description string, regex, keyword}
205 ucs = new UserColourScheme();
207 return ucs.createColourFromName(feature.getDescription());
211 return getMaxColor();
213 float scr = feature.getScore();
214 if (Float.isNaN(scr))
216 return getMinColor();
218 float scl = (scr - base) / range;
231 return new Color(lr + scl * dr, lg + scl * dg, lb + scl * db);
234 public void setThresh(float value)
239 public float getThresh()
244 public void setThreshType(int aboveThreshold)
246 thresholdState = aboveThreshold;
249 public int getThreshType()
251 return thresholdState;
254 public float getMax()
256 // regenerate the original values passed in to the constructor
257 return (tolow) ? base : (base + range);
260 public float getMin()
262 // regenerate the original value passed in to the constructor
263 return (tolow) ? (base + range) : base;
266 public boolean isAutoScale()
271 public void setAutoScaled(boolean autoscale)
273 autoScale = autoscale;
277 * update the base and range appropriatly for the given minmax range
280 * float[] {min,max} array containing minmax range for the associated
283 public void updateBounds(float min, float max)