holds shading and range parameters rendering a feature colour
[jalview.git] / src / jalview / schemes / GraduatedColor.java
1 /**
2  * 
3  */
4 package jalview.schemes;
5
6 import jalview.datamodel.SequenceFeature;
7
8 import java.awt.Color;
9
10 /**
11  * Value and/or thresholded colour scale used for colouring by annotation and feature score
12  * @author JimP
13  *
14  */
15 public class GraduatedColor
16 {
17   int thresholdState = AnnotationColourGradient.NO_THRESHOLD; // or ABOVE_THRESHOLD or BELOW_THRESHOLD
18   float lr,lg,lb,dr,dg,db;
19   /**
20    * linear scaling parameters, base, minimum colour threshold, range of linear scale from lower to upper
21    */
22   float base,range,thrsh;
23   /**
24    * when true, colour from u to u-d rather than u to u+d
25    */
26   boolean tolow=false;
27   /**
28    * construct a graduatedColor object from simple parameters
29    * @param low
30    * @param high
31    * @param min
32    * @param max
33    * color low->high from min->max
34    */
35   public GraduatedColor(Color low,Color high, float min,float max)
36   {
37     thrsh = Float.NaN;
38     tolow = min>=max;
39     lr = low.getRed()/255f;
40     lg = low.getGreen()/255f;
41     lb = low.getBlue()/255f;
42     dr = (high.getRed()/255f)-lr;
43     dg = (high.getGreen()/255f)-lg;
44     db = (high.getBlue()/255f)-lb;
45     if (tolow)
46     {
47       base = max;
48       range = min-max;
49     } else {
50       base = min;
51       range = max-min;
52     }
53   }
54   public GraduatedColor(GraduatedColor oldcs)
55   {
56     lr = oldcs.lr;
57     lg = oldcs.lg;
58     lb = oldcs.lb;
59     dr = oldcs.dr;
60     dg = oldcs.dg;
61     db = oldcs.db;
62     base = oldcs.base;
63     range = oldcs.range;
64     tolow = oldcs.tolow;
65     thresholdState = oldcs.thresholdState;
66     thrsh = oldcs.thrsh;
67   }
68   /**
69    * make a new gradient from an old one with a different scale range
70    * @param oldcs
71    * @param min
72    * @param max
73    */
74   public GraduatedColor(GraduatedColor oldcs, float min, float max)
75   {
76     this(oldcs);
77     if (max<min)
78     {
79       base = max;
80       range = min-max;
81       tolow=true;
82     } else {
83       base = min;
84       range = max-min;
85       tolow=false;
86     }
87   }
88   public Color getMinColor()
89   {
90     return new Color(lr,lg,lb);
91   }
92   public Color getMaxColor()
93   {
94     return new Color(lr+dr,lg+dg,lb+db);
95   }
96   public boolean getTolow()
97   {
98     return tolow;
99   }
100   public void setTolow(boolean tolower)
101   {
102     tolow = tolower;
103   }
104   public boolean isColored(SequenceFeature feature)
105   {
106     float val = feature.getScore();
107     if (val==Float.NaN)
108     {
109       return true;
110     }
111     if (this.thresholdState==AnnotationColourGradient.NO_THRESHOLD)
112     {
113       return true;
114     }
115     if (this.thrsh==Float.NaN)
116     {
117       return true;
118     }
119     boolean rtn = thresholdState==AnnotationColourGradient.ABOVE_THRESHOLD;
120     if (val<=thrsh)
121     {
122       return !rtn; //  ? !tolow : tolow;
123     } else {
124       return rtn; //  ? tolow : !tolow;
125     }
126   }
127   public Color findColor(SequenceFeature feature)
128   {     
129     if (range==0.0)
130     {
131       return getMaxColor();
132     }
133     float scr = feature.getScore();
134     if (scr==Float.NaN)
135     {
136       return getMinColor();
137     }
138     float scl = (scr-base)/range;
139     if (tolow) { scl = -scl; }
140     if (scl<0f) { scl = 0f; }
141     if (scl>1f) { scl = 1f; }
142     return new Color(lr+scl*dr,lg+scl*dg,lb+scl*db);
143   }
144   public void setThresh(float value)
145   {
146     thrsh = value;
147   }
148   public void setThreshType(int aboveThreshold)
149   {
150     thresholdState = aboveThreshold;
151   }
152   public int getThreshType()
153   {
154     return thresholdState;
155   }
156 }