lower case available
[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;\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 \r
150           if (ResidueProperties.aaHash.get(token) == null)\r
151             continue;\r
152 \r
153           int colIndex =\r
154               ( (Integer) ResidueProperties.aaHash.\r
155                get(token)).intValue();\r
156 \r
157            //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release\r
158 \r
159           if(token.equalsIgnoreCase("lowerCase"))\r
160           {\r
161             if (lowerCaseColours == null)\r
162               lowerCaseColours = new Color[23];\r
163             for (int i = 0; i < 23; i++)\r
164               if (lowerCaseColours[i] == null)\r
165                 lowerCaseColours[i] = getColourFromString(colour);\r
166 \r
167               continue;\r
168           }\r
169 \r
170           if(token.equals(token.toLowerCase()))\r
171           {\r
172             if(lowerCaseColours==null)\r
173             {\r
174               lowerCaseColours = new Color[23];\r
175             }\r
176             lowerCaseColours[colIndex] = getColourFromString(colour);\r
177           }\r
178           else\r
179             colors[colIndex] = getColourFromString(colour);\r
180         }\r
181       }\r
182     }\r
183     catch(Exception ex)\r
184   {\r
185     System.out.println("Error parsing userDefinedColours:\n"\r
186                        +token+"\n"+ex);\r
187   }\r
188 \r
189   }\r
190 \r
191 \r
192 \r
193   public Color findColour(String s, int j)\r
194   {\r
195       int index = ((Integer) (ResidueProperties.aaHash.get(s))).intValue();\r
196 \r
197       if ((threshold == 0) || aboveThreshold(ResidueProperties.aa[index], j))\r
198       {\r
199        //AW - LOWER CASE DISABLED IN 2.1.01 bug fix release\r
200         if(lowerCaseColours!=null && 'a' <= s.charAt(0) && s.charAt(0) <= 'z')\r
201           currentColour = lowerCaseColours[index];\r
202         else\r
203           currentColour = colors[index];\r
204       }\r
205       else\r
206       {\r
207           currentColour = Color.white;\r
208       }\r
209 \r
210       if(conservationColouring)\r
211         applyConservation(j);\r
212 \r
213 \r
214       return currentColour;\r
215    }\r
216 \r
217    public void setLowerCaseColours(Color [] lcolours)\r
218    {\r
219      lowerCaseColours = lcolours;\r
220    }\r
221 \r
222 }\r