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