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 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or
42 float lr, lg, lb, dr, dg, db;
45 * linear scaling parameters, base, minimum colour threshold, range of linear
46 * scale from lower to upper
48 float base, range, thrsh;
51 * when true, colour from u to u-d rather than u to u+d
53 boolean tolow = false;
56 * when false, min/max range has been manually set so should not be
57 * dynamically adjusted.
59 boolean autoScale = true;
62 * construct a graduatedColor object from simple parameters
68 * color low->high from min->max
70 public GraduatedColor(Color low, Color high, float min, float max)
74 lr = low.getRed() / 255f;
75 lg = low.getGreen() / 255f;
76 lb = low.getBlue() / 255f;
77 dr = (high.getRed() / 255f) - lr;
78 dg = (high.getGreen() / 255f) - lg;
79 db = (high.getBlue() / 255f) - lb;
92 public GraduatedColor(GraduatedColor oldcs)
103 thresholdState = oldcs.thresholdState;
105 autoScale = oldcs.autoScale;
106 colourByLabel = oldcs.colourByLabel;
107 colourAlternately = oldcs.colourAlternately;
111 * make a new gradient from an old one with a different scale range
117 public GraduatedColor(GraduatedColor oldcs, float min, float max)
120 updateBounds(min, max);
123 public GraduatedColor(FeatureColourI col)
125 setColourByLabel(col.isColourByLabel());
128 public Color getMinColor()
130 return new Color(lr, lg, lb);
133 public Color getMaxColor()
135 return new Color(lr + dr, lg + dg, lb + db);
140 * @return true if original min/max scale was from high to low
142 public boolean getTolow()
147 public void setTolow(boolean tolower)
152 public boolean isColored(SequenceFeature feature)
154 float val = feature.getScore();
155 if (Float.isNaN(val))
159 if (this.thresholdState == AnnotationColourGradient.NO_THRESHOLD)
163 if (Float.isNaN(this.thrsh))
167 boolean rtn = thresholdState == AnnotationColourGradient.ABOVE_THRESHOLD;
170 return !rtn; // ? !tolow : tolow;
174 return rtn; // ? tolow : !tolow;
179 * default implementor of a getColourFromString method. TODO: abstract an
180 * interface enabling pluggable colour from string
182 private UserColourScheme ucs = null;
184 private boolean colourByLabel = false;
186 private boolean colourAlternately = false;
190 * @return true if colourByLabel style is set
192 public boolean isColourByLabel()
194 return colourByLabel;
198 * @param colourByLabel
199 * the colourByLabel to set
201 public void setColourByLabel(boolean colourByLabel)
203 this.colourByLabel = colourByLabel;
206 public Color findColor(SequenceFeature feature)
210 // TODO: allow user defined feature label colourschemes. Colour space is
211 // {type,regex,%anytype%}x{description string, regex, keyword}
214 ucs = new UserColourScheme();
216 return ucs.createColourFromName(feature.getDescription());
218 if (colourAlternately)
220 int minOrMax = feature.getFeatureNumber() % 2;
221 return minOrMax == 0 ? new Color(lr, lg, lb) : new Color(lr + dr, lg
226 return getMaxColor();
228 float scr = feature.getScore();
229 if (Float.isNaN(scr))
231 return getMinColor();
233 float scl = (scr - base) / range;
246 return new Color(lr + scl * dr, lg + scl * dg, lb + scl * db);
249 public void setThresh(float value)
254 public float getThresh()
259 public void setThreshType(int aboveThreshold)
261 thresholdState = aboveThreshold;
264 public int getThreshType()
266 return thresholdState;
269 public float getMax()
271 // regenerate the original values passed in to the constructor
272 return (tolow) ? base : (base + range);
275 public float getMin()
277 // regenerate the original value passed in to the constructor
278 return (tolow) ? (base + range) : base;
281 public boolean isAutoScale()
286 public void setAutoScaled(boolean autoscale)
288 autoScale = autoscale;
292 * update the base and range appropriatly for the given minmax range
295 * float[] {min,max} array containing minmax range for the associated
298 public void updateBounds(float min, float max)
314 public boolean isColourAlternately()
316 return colourAlternately;
319 public void setColourAlternately(boolean colourAlternately)
321 this.colourAlternately = colourAlternately;