e42fd29a0db1f75abfdc8d80fc5b3851278e2d64
[jalview.git] / src / jalview / schemes / UserColourScheme.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.schemes;
20
21 import java.util.*;
22
23 import java.awt.*;
24
25 public class UserColourScheme extends ResidueColourScheme
26 {
27   Color[] lowerCaseColours;
28
29   protected String schemeName;
30
31   public UserColourScheme()
32   {
33   }
34
35   public UserColourScheme(Color[] newColors)
36   {
37     colors = newColors;
38   }
39
40   public UserColourScheme(String colour)
41   {
42     Color col = getColourFromString(colour);
43
44     if (col == null)
45     {
46       System.out.println("Unknown colour!! " + colour);
47       col = createColourFromName(colour);
48     }
49
50     colors = new Color[24];
51     for (int i = 0; i < 24; i++)
52     {
53       colors[i] = col;
54     }
55     schemeName = colour;
56   }
57
58   public Color[] getColours()
59   {
60     return colors;
61   }
62
63   public Color[] getLowerCaseColours()
64   {
65     return lowerCaseColours;
66   }
67
68   public void setName(String name)
69   {
70     schemeName = name;
71   }
72
73   public String getName()
74   {
75     return schemeName;
76   }
77
78   public Color getColourFromString(String colour)
79   {
80     colour = colour.trim();
81
82     Color col = null;
83     try
84     {
85       int value = Integer.parseInt(colour, 16);
86       col = new Color(value);
87     } catch (NumberFormatException ex)
88     {
89     }
90
91     if (col == null)
92     {
93       col = ColourSchemeProperty.getAWTColorFromName(colour);
94     }
95
96     if (col == null)
97     {
98       try
99       {
100         java.util.StringTokenizer st = new java.util.StringTokenizer(
101                 colour, ",");
102         int r = Integer.parseInt(st.nextToken());
103         int g = Integer.parseInt(st.nextToken());
104         int b = Integer.parseInt(st.nextToken());
105         col = new Color(r, g, b);
106       } catch (Exception ex)
107       {
108       }
109     }
110
111     return col;
112
113   }
114
115   public Color createColourFromName(String name)
116   {
117     int r, g, b;
118
119     int lsize = name.length();
120     int start = 0, end = lsize / 3;
121
122     int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
123
124     r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
125     start = end;
126     end += lsize / 3;
127     if (end > lsize)
128     {
129       end = lsize;
130     }
131
132     g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
133
134     b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
135
136     Color color = new Color(r, g, b);
137
138     return color;
139   }
140
141   public void parseAppletParameter(String paramValue)
142   {
143     StringTokenizer st = new StringTokenizer(paramValue, ";");
144     StringTokenizer st2;
145     String token = null, colour, residues;
146     try
147     {
148       while (st.hasMoreElements())
149       {
150         token = st.nextToken().trim();
151         residues = token.substring(0, token.indexOf("="));
152         colour = token.substring(token.indexOf("=") + 1);
153
154         st2 = new StringTokenizer(residues, " ,");
155         while (st2.hasMoreTokens())
156         {
157           token = st2.nextToken();
158
159           if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
160           {
161             continue;
162           }
163
164           int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
165
166           if (token.equalsIgnoreCase("lowerCase"))
167           {
168             if (lowerCaseColours == null)
169             {
170               lowerCaseColours = new Color[23];
171             }
172             for (int i = 0; i < 23; i++)
173             {
174               if (lowerCaseColours[i] == null)
175               {
176                 lowerCaseColours[i] = getColourFromString(colour);
177               }
178             }
179
180             continue;
181           }
182
183           if (token.equals(token.toLowerCase()))
184           {
185             if (lowerCaseColours == null)
186             {
187               lowerCaseColours = new Color[23];
188             }
189             lowerCaseColours[colIndex] = getColourFromString(colour);
190           }
191           else
192           {
193             colors[colIndex] = getColourFromString(colour);
194           }
195         }
196       }
197     } catch (Exception ex)
198     {
199       System.out.println("Error parsing userDefinedColours:\n" + token
200               + "\n" + ex);
201     }
202
203   }
204
205   public Color findColour(char c, int j)
206   {
207     Color currentColour;
208     int index = ResidueProperties.aaIndex[c];
209
210     if ((threshold == 0) || aboveThreshold(c, j))
211     {
212       if (lowerCaseColours != null && 'a' <= c && c <= 'z')
213       {
214         currentColour = lowerCaseColours[index];
215       }
216       else
217       {
218         currentColour = colors[index];
219       }
220     }
221     else
222     {
223       currentColour = Color.white;
224     }
225
226     if (conservationColouring)
227     {
228       currentColour = applyConservation(currentColour, j);
229     }
230
231     return currentColour;
232   }
233
234   public void setLowerCaseColours(Color[] lcolours)
235   {
236     lowerCaseColours = lcolours;
237   }
238
239 }