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)
108 colour = colour.trim();
113 int value = Integer.parseInt(colour, 16);
114 col = new Color(value);
115 } catch (NumberFormatException ex)
121 col = ColourSchemeProperty.getAWTColorFromName(colour);
128 java.util.StringTokenizer st = new java.util.StringTokenizer(
130 int r = Integer.parseInt(st.nextToken());
131 int g = Integer.parseInt(st.nextToken());
132 int b = Integer.parseInt(st.nextToken());
133 col = new Color(r, g, b);
134 } catch (Exception ex)
143 public static Color createColourFromName(String name)
147 int lsize = name.length();
148 int start = 0, end = lsize / 3;
150 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
152 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
160 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
162 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
164 Color color = new Color(r, g, b);
169 public void parseAppletParameter(String paramValue)
171 // TODO: need a function to generate appletParameter colour string from a
173 StringTokenizer st = new StringTokenizer(paramValue, ";");
175 String token = null, colour, residues;
178 while (st.hasMoreElements())
180 token = st.nextToken().trim();
181 residues = token.substring(0, token.indexOf("="));
182 colour = token.substring(token.indexOf("=") + 1);
184 st2 = new StringTokenizer(residues, " ,");
185 while (st2.hasMoreTokens())
187 token = st2.nextToken();
189 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
194 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
196 if (token.equalsIgnoreCase("lowerCase"))
198 if (lowerCaseColours == null)
200 lowerCaseColours = new Color[23];
202 for (int i = 0; i < 23; i++)
204 if (lowerCaseColours[i] == null)
206 lowerCaseColours[i] = getColourFromString(colour);
213 if (token.equals(token.toLowerCase()))
215 if (lowerCaseColours == null)
217 lowerCaseColours = new Color[23];
219 lowerCaseColours[colIndex] = getColourFromString(colour);
223 colors[colIndex] = getColourFromString(colour);
227 } catch (Exception ex)
229 System.out.println("Error parsing userDefinedColours:\n" + token
236 public Color findColour(char c, int j, SequenceI seq)
239 int index = ResidueProperties.aaIndex[c];
241 if ((threshold == 0) || aboveThreshold(c, j))
243 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
245 currentColour = lowerCaseColours[index];
249 currentColour = colors[index];
254 currentColour = Color.white;
257 if (conservationColouring)
259 currentColour = applyConservation(currentColour, j);
262 return currentColour;
265 public void setLowerCaseColours(Color[] lcolours)
267 lowerCaseColours = lcolours;