2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.schemes;
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
27 import java.awt.Color;
29 import java.util.StringTokenizer;
31 public class UserColourScheme extends ResidueColourScheme
33 Color[] lowerCaseColours;
35 protected String schemeName;
37 public UserColourScheme()
39 super(ResidueProperties.aaIndex);
42 public UserColourScheme(Color[] newColors)
44 super(ResidueProperties.aaIndex);
49 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
50 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
52 UserColourScheme usc = new UserColourScheme(colors);
53 if (lowerCaseColours != null)
55 usc.schemeName = new String(schemeName);
56 usc.lowerCaseColours = new Color[lowerCaseColours.length];
57 System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0,
58 lowerCaseColours.length);
63 public UserColourScheme(String colour)
65 super(ResidueProperties.aaIndex);
66 Color col = getColourFromString(colour);
70 System.out.println("Making colour from name: " + colour);
71 col = createColourFromName(colour);
74 colors = new Color[24];
75 for (int i = 0; i < 24; i++)
82 public Color[] getColours()
87 public Color[] getLowerCaseColours()
89 return lowerCaseColours;
92 public void setName(String name)
97 public String getName()
102 public static Color getColourFromString(String colour)
104 colour = colour.trim();
109 int value = Integer.parseInt(colour, 16);
110 col = new Color(value);
111 } catch (NumberFormatException ex)
117 col = ColourSchemeProperty.getAWTColorFromName(colour);
124 java.util.StringTokenizer st = new java.util.StringTokenizer(
126 int r = Integer.parseInt(st.nextToken());
127 int g = Integer.parseInt(st.nextToken());
128 int b = Integer.parseInt(st.nextToken());
129 col = new Color(r, g, b);
130 } catch (Exception ex)
139 public static Color createColourFromName(String name)
143 int lsize = name.length();
144 int start = 0, end = lsize / 3;
146 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
148 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
156 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
158 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
160 Color color = new Color(r, g, b);
165 public void parseAppletParameter(String paramValue)
167 // TODO: need a function to generate appletParameter colour string from a
169 StringTokenizer st = new StringTokenizer(paramValue, ";");
171 String token = null, colour, residues;
174 while (st.hasMoreElements())
176 token = st.nextToken().trim();
177 residues = token.substring(0, token.indexOf("="));
178 colour = token.substring(token.indexOf("=") + 1);
180 st2 = new StringTokenizer(residues, " ,");
181 while (st2.hasMoreTokens())
183 token = st2.nextToken();
185 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
190 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
192 if (token.equalsIgnoreCase("lowerCase"))
194 if (lowerCaseColours == null)
196 lowerCaseColours = new Color[23];
198 for (int i = 0; i < 23; i++)
200 if (lowerCaseColours[i] == null)
202 lowerCaseColours[i] = getColourFromString(colour);
209 if (token.equals(token.toLowerCase()))
211 if (lowerCaseColours == null)
213 lowerCaseColours = new Color[23];
215 lowerCaseColours[colIndex] = getColourFromString(colour);
219 colors[colIndex] = getColourFromString(colour);
223 } catch (Exception ex)
225 System.out.println("Error parsing userDefinedColours:\n" + token
232 public Color findColour(char c, int j, SequenceI seq)
235 int index = ResidueProperties.aaIndex[c];
237 if ((threshold == 0) || aboveThreshold(c, j))
239 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
241 currentColour = lowerCaseColours[index];
245 currentColour = colors[index];
250 currentColour = Color.white;
253 if (conservationColouring)
255 currentColour = applyConservation(currentColour, j);
258 return currentColour;
261 public void setLowerCaseColours(Color[] lcolours)
263 lowerCaseColours = lcolours;