2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
18 package jalview.schemes;
20 import jalview.datamodel.SequenceFeature;
22 import java.awt.Color;
25 * Value and/or thresholded colour scale used for colouring by annotation and
31 public class GraduatedColor
33 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or
38 float lr, lg, lb, dr, dg, db;
41 * linear scaling parameters, base, minimum colour threshold, range of linear
42 * scale from lower to upper
44 float base, range, thrsh;
47 * when true, colour from u to u-d rather than u to u+d
49 boolean tolow = false;
52 * when false, min/max range has been manually set so should not be
53 * dynamically adjusted.
55 boolean autoScale = true;
58 * construct a graduatedColor object from simple parameters
64 * color low->high from min->max
66 public GraduatedColor(Color low, Color high, float min, float max)
70 lr = low.getRed() / 255f;
71 lg = low.getGreen() / 255f;
72 lb = low.getBlue() / 255f;
73 dr = (high.getRed() / 255f) - lr;
74 dg = (high.getGreen() / 255f) - lg;
75 db = (high.getBlue() / 255f) - lb;
88 public GraduatedColor(GraduatedColor oldcs)
99 thresholdState = oldcs.thresholdState;
101 autoScale = oldcs.autoScale;
102 colourByLabel = oldcs.colourByLabel;
106 * make a new gradient from an old one with a different scale range
112 public GraduatedColor(GraduatedColor oldcs, float min, float max)
115 updateBounds(min, max);
118 public Color getMinColor()
120 return new Color(lr, lg, lb);
123 public Color getMaxColor()
125 return new Color(lr + dr, lg + dg, lb + db);
130 * @return true if original min/max scale was from high to low
132 public boolean getTolow()
137 public void setTolow(boolean tolower)
142 public boolean isColored(SequenceFeature feature)
144 float val = feature.getScore();
145 if (val == Float.NaN)
149 if (this.thresholdState == AnnotationColourGradient.NO_THRESHOLD)
153 if (this.thrsh == Float.NaN)
157 boolean rtn = thresholdState == AnnotationColourGradient.ABOVE_THRESHOLD;
160 return !rtn; // ? !tolow : tolow;
164 return rtn; // ? tolow : !tolow;
169 * default implementor of a getColourFromString method. TODO: abstract an
170 * interface enabling pluggable colour from string
172 private UserColourScheme ucs = null;
174 private boolean colourByLabel = false;
178 * @return true if colourByLabel style is set
180 public boolean isColourByLabel()
182 return colourByLabel;
186 * @param colourByLabel
187 * the colourByLabel to set
189 public void setColourByLabel(boolean colourByLabel)
191 this.colourByLabel = colourByLabel;
194 public Color findColor(SequenceFeature feature)
198 // TODO: allow user defined feature label colourschemes. Colour space is
199 // {type,regex,%anytype%}x{description string, regex, keyword}
202 ucs = new UserColourScheme();
204 return ucs.createColourFromName(feature.getDescription());
208 return getMaxColor();
210 float scr = feature.getScore();
211 if (scr == Float.NaN)
213 return getMinColor();
215 float scl = (scr - base) / range;
228 return new Color(lr + scl * dr, lg + scl * dg, lb + scl * db);
231 public void setThresh(float value)
236 public float getThresh()
241 public void setThreshType(int aboveThreshold)
243 thresholdState = aboveThreshold;
246 public int getThreshType()
248 return thresholdState;
251 public float getMax()
253 // regenerate the original values passed in to the constructor
254 return (tolow) ? base : (base + range);
257 public float getMin()
259 // regenerate the original value passed in to the constructor
260 return (tolow) ? (base + range) : base;
263 public boolean isAutoScale()
268 public void setAutoScaled(boolean autoscale)
270 autoScale = autoscale;
274 * update the base and range appropriatly for the given minmax range
277 * float[] {min,max} array containing minmax range for the associated
280 public void updateBounds(float min, float max)