Colour above or below threshold
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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 extends ResidueColourScheme\r
26 {\r
27     public static int NO_THRESHOLD = -1;\r
28     public static int BELOW_THRESHOLD = 0;\r
29     public static int ABOVE_THRESHOLD = 1;\r
30 \r
31     AlignmentAnnotation annotation;\r
32     int aboveAnnotationThreshold = -1;\r
33 \r
34     GraphLine annotationThreshold;\r
35 \r
36     float r1, g1, b1, rr, gg, bb, dr, dg, db;\r
37     float range;\r
38 \r
39     /**\r
40      * Creates a new AnnotationColourGradient object.\r
41      */\r
42     public AnnotationColourGradient(AlignmentAnnotation annotation,\r
43         Color minColour, Color maxColour, int aboveThreshold)\r
44     {\r
45       this.annotation = annotation;\r
46 \r
47       aboveAnnotationThreshold = aboveThreshold;\r
48 \r
49       if(aboveThreshold!=NO_THRESHOLD && annotation.graphLines!=null)\r
50         annotationThreshold = annotation.getGraphLine(0);\r
51 \r
52       r1 = minColour.getRed();\r
53       g1 = minColour.getGreen();\r
54       b1 = minColour.getBlue();\r
55 \r
56       rr = maxColour.getRed() - r1;\r
57       gg = maxColour.getGreen() - g1;\r
58       bb = maxColour.getBlue() - b1;\r
59 \r
60       range = annotation.graphMax - annotation.graphMin;\r
61 \r
62     }\r
63 \r
64     public Color getMinColour()\r
65     {\r
66       return new Color( (int) r1, (int) g1, (int) b1);\r
67     }\r
68 \r
69     public Color getMaxColour()\r
70     {\r
71       return new Color( (int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));\r
72     }\r
73 \r
74     /**\r
75      * DOCUMENT ME!\r
76      *\r
77      * @param n DOCUMENT ME!\r
78      *\r
79      * @return DOCUMENT ME!\r
80      */\r
81     public Color findColour(String n)\r
82     {\r
83       System.out.println("AnnotationColourGradient findColour(string)");\r
84         return Color.red;\r
85     }\r
86 \r
87     /**\r
88      * DOCUMENT ME!\r
89      *\r
90      * @param n DOCUMENT ME!\r
91      * @param j DOCUMENT ME!\r
92      *\r
93      * @return DOCUMENT ME!\r
94      */\r
95     public Color findColour(String n, int j)\r
96     {\r
97         if ((threshold == 0) || aboveThreshold(n, j))\r
98         {\r
99           if( j+1>annotation.annotations.length || annotation.annotations[j]==null)\r
100             currentColour = Color.white;\r
101           else\r
102           {\r
103             if(  aboveAnnotationThreshold==NO_THRESHOLD\r
104                || (annotationThreshold!=null && aboveAnnotationThreshold==ABOVE_THRESHOLD && annotation.annotations[j].value>=annotationThreshold.value)\r
105                || (annotationThreshold!=null && aboveAnnotationThreshold==BELOW_THRESHOLD && annotation.annotations[j].value<=annotationThreshold.value))\r
106             {\r
107                 dr = rr *\r
108                      ((annotation.annotations[j].value-annotation.graphMin) / range )\r
109                      +r1;\r
110                 dg = gg *\r
111                      ((annotation.annotations[j].value-annotation.graphMin)  / range )\r
112                      +g1;\r
113                 db = bb *\r
114                      ((annotation.annotations[j].value-annotation.graphMin) / range )\r
115                      +b1;\r
116 \r
117                  currentColour = new Color( (int) dr, (int) dg, (int) db);\r
118             }\r
119             else\r
120               currentColour = Color.white;\r
121           }\r
122         }\r
123         else\r
124         {\r
125             return Color.white;\r
126         }\r
127 \r
128         if(conservationColouring)\r
129          applyConservation(j);\r
130 \r
131        return currentColour;\r
132     }\r
133 }\r