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