Formatting
[jalview.git] / src / jalview / schemes / UserColourScheme.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.util.*;\r
22 \r
23 import java.awt.*;\r
24 \r
25 public class UserColourScheme\r
26     extends ResidueColourScheme\r
27 {\r
28   Color[] lowerCaseColours;\r
29 \r
30   protected String schemeName;\r
31 \r
32   public UserColourScheme()\r
33   {}\r
34 \r
35   public UserColourScheme(Color[] newColors)\r
36   {\r
37     colors = newColors;\r
38   }\r
39 \r
40   public UserColourScheme(String colour)\r
41   {\r
42     Color col = getColourFromString(colour);\r
43 \r
44     if (col == null)\r
45     {\r
46       System.out.println("Unknown colour!! " + colour);\r
47       col = createColourFromName(colour);\r
48     }\r
49 \r
50     colors = new Color[24];\r
51     for (int i = 0; i < 24; i++)\r
52     {\r
53       colors[i] = col;\r
54     }\r
55   }\r
56 \r
57   public Color[] getColours()\r
58   {\r
59     return colors;\r
60   }\r
61 \r
62   public Color[] getLowerCaseColours()\r
63   {\r
64     return lowerCaseColours;\r
65   }\r
66 \r
67   public void setName(String name)\r
68   {\r
69     schemeName = name;\r
70   }\r
71 \r
72   public String getName()\r
73   {\r
74     return schemeName;\r
75   }\r
76 \r
77   public Color getColourFromString(String colour)\r
78   {\r
79     colour = colour.trim();\r
80 \r
81     Color col = null;\r
82     try\r
83     {\r
84       int value = Integer.parseInt(colour, 16);\r
85       col = new Color(value);\r
86     }\r
87     catch (NumberFormatException ex)\r
88     {}\r
89 \r
90     if (col == null)\r
91     {\r
92       col = ColourSchemeProperty.getAWTColorFromName(colour);\r
93     }\r
94 \r
95     if (col == null)\r
96     {\r
97       try\r
98       {\r
99         java.util.StringTokenizer st = new java.util.StringTokenizer(colour,\r
100             ",");\r
101         int r = Integer.parseInt(st.nextToken());\r
102         int g = Integer.parseInt(st.nextToken());\r
103         int b = Integer.parseInt(st.nextToken());\r
104         col = new Color(r, g, b);\r
105       }\r
106       catch (Exception ex)\r
107       {}\r
108     }\r
109 \r
110     return col;\r
111 \r
112   }\r
113 \r
114   public Color createColourFromName(String name)\r
115   {\r
116     int r, g, b;\r
117 \r
118     int lsize = name.length();\r
119     int start = 0, end = lsize / 3;\r
120 \r
121     int rgbOffset = Math.abs(name.hashCode() % 10) * 15;\r
122 \r
123     r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;\r
124     start = end;\r
125     end += lsize / 3;\r
126     if (end > lsize)\r
127     {\r
128       end = lsize;\r
129     }\r
130 \r
131     g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;\r
132 \r
133     b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;\r
134 \r
135     Color color = new Color(r, g, b);\r
136 \r
137     return color;\r
138   }\r
139 \r
140   public void parseAppletParameter(String paramValue)\r
141   {\r
142     StringTokenizer st = new StringTokenizer(paramValue, ";");\r
143     StringTokenizer st2;\r
144     String token = null, colour, residues;\r
145     try\r
146     {\r
147       while (st.hasMoreElements())\r
148       {\r
149         token = st.nextToken().trim();\r
150         residues = token.substring(0, token.indexOf("="));\r
151         colour = token.substring(token.indexOf("=") + 1);\r
152 \r
153         st2 = new StringTokenizer(residues, " ,");\r
154         while (st2.hasMoreTokens())\r
155         {\r
156           token = st2.nextToken();\r
157 \r
158           if (ResidueProperties.aaIndex[token.charAt(0)] == -1)\r
159           {\r
160             continue;\r
161           }\r
162 \r
163           int colIndex = ResidueProperties.aaIndex[token.charAt(0)];\r
164 \r
165           if (token.equalsIgnoreCase("lowerCase"))\r
166           {\r
167             if (lowerCaseColours == null)\r
168             {\r
169               lowerCaseColours = new Color[23];\r
170             }\r
171             for (int i = 0; i < 23; i++)\r
172             {\r
173               if (lowerCaseColours[i] == null)\r
174               {\r
175                 lowerCaseColours[i] = getColourFromString(colour);\r
176               }\r
177             }\r
178 \r
179             continue;\r
180           }\r
181 \r
182           if (token.equals(token.toLowerCase()))\r
183           {\r
184             if (lowerCaseColours == null)\r
185             {\r
186               lowerCaseColours = new Color[23];\r
187             }\r
188             lowerCaseColours[colIndex] = getColourFromString(colour);\r
189           }\r
190           else\r
191           {\r
192             colors[colIndex] = getColourFromString(colour);\r
193           }\r
194         }\r
195       }\r
196     }\r
197     catch (Exception ex)\r
198     {\r
199       System.out.println("Error parsing userDefinedColours:\n"\r
200                          + token + "\n" + ex);\r
201     }\r
202 \r
203   }\r
204 \r
205   public Color findColour(char c, int j)\r
206   {\r
207     Color currentColour;\r
208     int index = ResidueProperties.aaIndex[c];\r
209 \r
210     if ( (threshold == 0) || aboveThreshold(c, j))\r
211     {\r
212       if (lowerCaseColours != null && 'a' <= c && c <= 'z')\r
213       {\r
214         currentColour = lowerCaseColours[index];\r
215       }\r
216       else\r
217       {\r
218         currentColour = colors[index];\r
219       }\r
220     }\r
221     else\r
222     {\r
223       currentColour = Color.white;\r
224     }\r
225 \r
226     if (conservationColouring)\r
227     {\r
228       currentColour = applyConservation(currentColour, j);\r
229     }\r
230 \r
231     return currentColour;\r
232   }\r
233 \r
234   public void setLowerCaseColours(Color[] lcolours)\r
235   {\r
236     lowerCaseColours = lcolours;\r
237   }\r
238 \r
239 }\r