1 package jalview.schemes;
3 import jalview.api.FeatureColourI;
4 import jalview.datamodel.SequenceFeature;
9 * A class that wraps either a simple colour or a graduated colour
11 public class FeatureColour implements FeatureColourI
13 final private Color colour;
15 final private Color minColour;
17 final private Color maxColour;
19 private boolean graduatedColour;
21 private boolean colourByLabel;
23 private float threshold;
29 private boolean belowThreshold;
31 private boolean aboveThreshold;
33 private boolean thresholdIsMinOrMax;
35 private boolean isHighToLow;
37 private boolean autoScaled;
39 final private float minRed;
41 final private float minGreen;
43 final private float minBlue;
45 final private float deltaRed;
47 final private float deltaGreen;
49 final private float deltaBlue;
54 public FeatureColour()
60 * Constructor given a simple colour
64 public FeatureColour(Color c)
78 * Constructor given a colour range and a score range
85 public FeatureColour(Color low, Color high, float min, float max)
87 graduatedColour = true;
91 threshold = Float.NaN;
92 isHighToLow = min >= max;
93 minRed = low.getRed() / 255f;
94 minGreen = low.getGreen() / 255f;
95 minBlue = low.getBlue() / 255f;
96 deltaRed = (high.getRed() / 255f) - minRed;
97 deltaGreen = (high.getGreen() / 255f) - minGreen;
98 deltaBlue = (high.getBlue() / 255f) - minBlue;
116 public FeatureColour(FeatureColour fc)
119 minColour = fc.minColour;
120 maxColour = fc.maxColour;
122 minGreen = fc.minGreen;
123 minBlue = fc.minBlue;
124 deltaRed = fc.deltaRed;
125 deltaGreen = fc.deltaGreen;
126 deltaBlue = fc.deltaBlue;
129 isHighToLow = fc.isHighToLow;
130 setAboveThreshold(fc.isAboveThreshold());
131 setBelowThreshold(fc.isBelowThreshold());
132 setThreshold(fc.getThreshold());
133 setAutoScaled(fc.isAutoScaled());
134 setColourByLabel(fc.isColourByLabel());
138 * Copy constructor with new min/max ranges
143 public FeatureColour(FeatureColour fc, float min, float max)
146 graduatedColour = true;
147 updateBounds(min, max);
151 public boolean isGraduatedColour()
153 return graduatedColour;
157 * Sets the 'graduated colour' flag. If true, also sets 'colour by label' to
161 public void setGraduatedColour(boolean b)
166 setColourByLabel(false);
171 public Color getColour()
177 public Color getMinColour()
183 public Color getMaxColour()
189 public boolean isColourByLabel()
191 return colourByLabel;
195 * Sets the 'colour by label' flag. If true, also sets 'graduated colour' to
199 public void setColourByLabel(boolean b)
204 setGraduatedColour(false);
208 public boolean isBelowThreshold()
210 return belowThreshold;
214 public void setBelowThreshold(boolean b)
219 setAboveThreshold(false);
224 public boolean isAboveThreshold()
226 return aboveThreshold;
230 public void setAboveThreshold(boolean b)
235 setBelowThreshold(false);
240 public boolean isThresholdMinMax()
242 return thresholdIsMinOrMax;
246 public void setThresholdMinMax(boolean b)
248 thresholdIsMinOrMax = b;
252 public float getThreshold()
258 public void setThreshold(float f)
264 public boolean isAutoScaled()
270 public void setAutoScaled(boolean b)
276 * Updates the base and range appropriately for the given minmax range
282 public void updateBounds(float min, float max)
299 * Returns the colour for the given instance of the feature. This may be a
300 * simple colour, a colour generated from the feature description (if
301 * isColourByLabel()), or a colour derived from the feature score (if
302 * isGraduatedColour()).
308 public Color getColor(SequenceFeature feature)
310 if (isColourByLabel())
312 return UserColourScheme
313 .createColourFromName(feature.getDescription());
316 if (!isGraduatedColour())
321 // todo should we check for above/below threshold here?
324 return getMaxColour();
326 float scr = feature.getScore();
327 if (Float.isNaN(scr))
329 return getMinColour();
331 float scl = (scr - base) / range;
344 return new Color(minRed + scl * deltaRed, minGreen + scl * deltaGreen, minBlue + scl * deltaBlue);
348 * Returns the maximum score of the graduated colour range
353 public float getMax()
355 // regenerate the original values passed in to the constructor
356 return (isHighToLow) ? base : (base + range);
360 * Returns the minimum score of the graduated colour range
365 public float getMin()
367 // regenerate the original value passed in to the constructor
368 return (isHighToLow) ? (base + range) : base;
372 * Answers true if the feature has a simple colour, or is coloured by label,
373 * or has a graduated colour and the score of this feature instance is within
374 * the range to render (if any), i.e. does not lie below or above any
381 public boolean isColored(SequenceFeature feature)
383 if (isColourByLabel() || !isGraduatedColour())
388 float val = feature.getScore();
389 if (Float.isNaN(val))
393 if (Float.isNaN(this.threshold))
398 if (isAboveThreshold() && val <= threshold)
402 if (isBelowThreshold() && val >= threshold)
410 public boolean isSimpleColour()
412 return (!isColourByLabel() && !isGraduatedColour());
416 public boolean hasThreshold()
418 return isAboveThreshold() || isBelowThreshold();