Formatting
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.schemes;\r
20 \r
21 import java.awt.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 public class AnnotationColourGradient\r
26     extends ResidueColourScheme\r
27 {\r
28   public static int NO_THRESHOLD = -1;\r
29   public static int BELOW_THRESHOLD = 0;\r
30   public static int ABOVE_THRESHOLD = 1;\r
31 \r
32   AlignmentAnnotation annotation;\r
33   int aboveAnnotationThreshold = -1;\r
34 \r
35   GraphLine annotationThreshold;\r
36 \r
37   float r1, g1, b1, rr, gg, bb, dr, dg, db;\r
38   float range = 0;\r
39 \r
40   ColourSchemeI colourScheme;\r
41 \r
42   /**\r
43    * Creates a new AnnotationColourGradient object.\r
44    */\r
45   public AnnotationColourGradient(AlignmentAnnotation annotation,\r
46                                   ColourSchemeI originalColour,\r
47                                   int aboveThreshold)\r
48   {\r
49     if (originalColour instanceof AnnotationColourGradient)\r
50     {\r
51       colourScheme = ( (AnnotationColourGradient) originalColour).colourScheme;\r
52     }\r
53     else\r
54     {\r
55       colourScheme = originalColour;\r
56     }\r
57 \r
58     this.annotation = annotation;\r
59 \r
60     aboveAnnotationThreshold = aboveThreshold;\r
61 \r
62     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)\r
63     {\r
64       annotationThreshold = annotation.threshold;\r
65     }\r
66   }\r
67 \r
68   /**\r
69    * Creates a new AnnotationColourGradient object.\r
70    */\r
71   public AnnotationColourGradient(AlignmentAnnotation annotation,\r
72                                   Color minColour, Color maxColour,\r
73                                   int aboveThreshold)\r
74   {\r
75     this.annotation = annotation;\r
76 \r
77     aboveAnnotationThreshold = aboveThreshold;\r
78 \r
79     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)\r
80     {\r
81       annotationThreshold = annotation.threshold;\r
82     }\r
83 \r
84     r1 = minColour.getRed();\r
85     g1 = minColour.getGreen();\r
86     b1 = minColour.getBlue();\r
87 \r
88     rr = maxColour.getRed() - r1;\r
89     gg = maxColour.getGreen() - g1;\r
90     bb = maxColour.getBlue() - b1;\r
91 \r
92     range = annotation.graphMax - annotation.graphMin;\r
93 \r
94   }\r
95 \r
96   public String getAnnotation()\r
97   {\r
98     return annotation.label;\r
99   }\r
100 \r
101   public int getAboveThreshold()\r
102   {\r
103     return aboveAnnotationThreshold;\r
104   }\r
105 \r
106   public float getAnnotationThreshold()\r
107   {\r
108     if (annotationThreshold == null)\r
109     {\r
110       return 0;\r
111     }\r
112     else\r
113     {\r
114       return annotationThreshold.value;\r
115     }\r
116   }\r
117 \r
118   public ColourSchemeI getBaseColour()\r
119   {\r
120     return colourScheme;\r
121   }\r
122 \r
123   public Color getMinColour()\r
124   {\r
125     return new Color( (int) r1, (int) g1, (int) b1);\r
126   }\r
127 \r
128   public Color getMaxColour()\r
129   {\r
130     return new Color( (int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));\r
131   }\r
132 \r
133   /**\r
134    * DOCUMENT ME!\r
135    *\r
136    * @param n DOCUMENT ME!\r
137    *\r
138    * @return DOCUMENT ME!\r
139    */\r
140   public Color findColour(char c)\r
141   {\r
142     return Color.red;\r
143   }\r
144 \r
145   /**\r
146    * DOCUMENT ME!\r
147    *\r
148    * @param n DOCUMENT ME!\r
149    * @param j DOCUMENT ME!\r
150    *\r
151    * @return DOCUMENT ME!\r
152    */\r
153   public Color findColour(char c, int j)\r
154   {\r
155     Color currentColour = Color.white;\r
156 \r
157     if ( (threshold == 0) || aboveThreshold(c, j))\r
158     {\r
159       if (j < annotation.annotations.length\r
160           && annotation.annotations[j] != null\r
161           && !jalview.util.Comparison.isGap(c))\r
162       {\r
163         if (aboveAnnotationThreshold == NO_THRESHOLD\r
164             ||\r
165             (annotationThreshold != null && aboveAnnotationThreshold == ABOVE_THRESHOLD &&\r
166              annotation.annotations[j].value >= annotationThreshold.value)\r
167             ||\r
168             (annotationThreshold != null && aboveAnnotationThreshold == BELOW_THRESHOLD &&\r
169              annotation.annotations[j].value <= annotationThreshold.value))\r
170         {\r
171           if (colourScheme != null)\r
172           {\r
173             currentColour = colourScheme.findColour(c, j);\r
174           }\r
175           else if (range != 0)\r
176           {\r
177             dr = rr *\r
178                 ( (annotation.annotations[j].value - annotation.graphMin) /\r
179                  range)\r
180                 + r1;\r
181             dg = gg *\r
182                 ( (annotation.annotations[j].value - annotation.graphMin) /\r
183                  range)\r
184                 + g1;\r
185             db = bb *\r
186                 ( (annotation.annotations[j].value - annotation.graphMin) /\r
187                  range)\r
188                 + b1;\r
189 \r
190             currentColour = new Color( (int) dr, (int) dg, (int) db);\r
191           }\r
192         }\r
193       }\r
194     }\r
195 \r
196     if (conservationColouring)\r
197     {\r
198       currentColour = applyConservation(currentColour, j);\r
199     }\r
200 \r
201     return currentColour;\r
202   }\r
203 }\r