2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
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 feature score
30 public class GraduatedColor
32 int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or ABOVE_THRESHOLD or BELOW_THRESHOLD
33 float lr,lg,lb,dr,dg,db;
35 * linear scaling parameters, base, minimum colour threshold, range of linear scale from lower to upper
37 float base,range,thrsh;
39 * when true, colour from u to u-d rather than u to u+d
43 * when false, min/max range has been manually set so should not be dynamically adjusted.
45 boolean autoScale = true;
47 * construct a graduatedColor object from simple parameters
52 * color low->high from min->max
54 public GraduatedColor(Color low,Color high, float min,float max)
58 lr = low.getRed()/255f;
59 lg = low.getGreen()/255f;
60 lb = low.getBlue()/255f;
61 dr = (high.getRed()/255f)-lr;
62 dg = (high.getGreen()/255f)-lg;
63 db = (high.getBlue()/255f)-lb;
73 public GraduatedColor(GraduatedColor oldcs)
84 thresholdState = oldcs.thresholdState;
86 autoScale = oldcs.autoScale;
87 colourByLabel = oldcs.colourByLabel;
90 * make a new gradient from an old one with a different scale range
95 public GraduatedColor(GraduatedColor oldcs, float min, float max)
98 updateBounds(min, max);
100 public Color getMinColor()
102 return new Color(lr,lg,lb);
104 public Color getMaxColor()
106 return new Color(lr+dr,lg+dg,lb+db);
110 * @return true if original min/max scale was from high to low
112 public boolean getTolow()
116 public void setTolow(boolean tolower)
120 public boolean isColored(SequenceFeature feature)
122 float val = feature.getScore();
127 if (this.thresholdState==AnnotationColourGradient.NO_THRESHOLD)
131 if (this.thrsh==Float.NaN)
135 boolean rtn = thresholdState==AnnotationColourGradient.ABOVE_THRESHOLD;
138 return !rtn; // ? !tolow : tolow;
140 return rtn; // ? tolow : !tolow;
144 * default implementor of a getColourFromString method.
145 * TODO: abstract an interface enabling pluggable colour from string
147 private UserColourScheme ucs = null;
148 private boolean colourByLabel=true;
151 * @return true if colourByLabel style is set
153 public boolean isColourByLabel()
155 return colourByLabel;
158 * @param colourByLabel the colourByLabel to set
160 public void setColourByLabel(boolean colourByLabel)
162 this.colourByLabel = colourByLabel;
164 public Color findColor(SequenceFeature feature)
168 //TODO: allow user defined feature label colourschemes. Colour space is {type,regex,%anytype%}x{description string, regex, keyword}
171 ucs = new UserColourScheme();
173 return ucs.createColourFromName(feature.getDescription());
177 return getMaxColor();
179 float scr = feature.getScore();
182 return getMinColor();
184 float scl = (scr-base)/range;
185 if (tolow) { scl = -scl; }
186 if (scl<0f) { scl = 0f; }
187 if (scl>1f) { scl = 1f; }
188 return new Color(lr+scl*dr,lg+scl*dg,lb+scl*db);
190 public void setThresh(float value)
194 public float getThresh() {
197 public void setThreshType(int aboveThreshold)
199 thresholdState = aboveThreshold;
201 public int getThreshType()
203 return thresholdState;
205 public float getMax()
207 // regenerate the original values passed in to the constructor
208 return (tolow) ? base : (base + range);
210 public float getMin()
212 // regenerate the original value passed in to the constructor
213 return (tolow) ? (base+range) : base;
215 public boolean isAutoScale()
219 public void setAutoScaled(boolean autoscale) {
220 autoScale = autoscale;
223 * update the base and range appropriatly for the given minmax range
224 * @param a float[] {min,max} array containing minmax range for the associated score values
226 public void updateBounds(float min,float max)