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