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