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()
103 * Parses a string into a Color, where the accepted formats are
105 * <li>an AWT colour name e.g. white</li>
106 * <li>a hex colour value (without prefix) e.g. ff0000</li>
107 * <li>an rgb triple e.g. 100,50,150</li>
111 * @return the parsed colour, or null if parsing fails
113 public static Color getColourFromString(String colour)
119 colour = colour.trim();
124 int value = Integer.parseInt(colour, 16);
125 col = new Color(value);
126 } catch (NumberFormatException ex)
132 col = ColourSchemeProperty.getAWTColorFromName(colour);
139 String[] tokens = colour.split(",");
140 if (tokens.length == 3)
142 int r = Integer.parseInt(tokens[0].trim());
143 int g = Integer.parseInt(tokens[1].trim());
144 int b = Integer.parseInt(tokens[2].trim());
145 col = new Color(r, g, b);
147 } catch (Exception ex)
149 // non-numeric token or out of 0-255 range
156 public static Color createColourFromName(String name)
160 int lsize = name.length();
161 int start = 0, end = lsize / 3;
163 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
165 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
173 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
175 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
177 Color color = new Color(r, g, b);
183 * Parse and save residue colours specified as (for example)
186 * D,E=red; K,R,H=0022FF; c=100,50,75
189 * This should be a semi-colon separated list of colours, which may be defined
190 * by colour name, hex value or comma-separated RGB triple. Each colour is
191 * defined for a comma-separated list of amino acid single letter codes. (Note
192 * that this also allows a colour scheme to be defined for ACGT, but not for
197 public void parseAppletParameter(String paramValue)
199 // TODO: need a function to generate appletParameter colour string from a
201 StringTokenizer st = new StringTokenizer(paramValue, ";");
203 String token = null, colour, residues;
206 while (st.hasMoreElements())
208 token = st.nextToken().trim();
209 residues = token.substring(0, token.indexOf("="));
210 colour = token.substring(token.indexOf("=") + 1);
212 st2 = new StringTokenizer(residues, " ,");
213 while (st2.hasMoreTokens())
215 String residue = st2.nextToken();
217 int colIndex = ResidueProperties.aaIndex[residue.charAt(0)];
223 if (residue.equalsIgnoreCase("lowerCase"))
225 if (lowerCaseColours == null)
227 lowerCaseColours = new Color[23];
229 for (int i = 0; i < 23; i++)
231 if (lowerCaseColours[i] == null)
233 lowerCaseColours[i] = getColourFromString(colour);
240 if (residue.equals(residue.toLowerCase()))
242 if (lowerCaseColours == null)
244 lowerCaseColours = new Color[23];
246 lowerCaseColours[colIndex] = getColourFromString(colour);
250 colors[colIndex] = getColourFromString(colour);
254 } catch (Exception ex)
256 System.out.println("Error parsing userDefinedColours:\n" + token
263 public Color findColour(char c, int j, SequenceI seq)
266 int index = ResidueProperties.aaIndex[c];
268 if ((threshold == 0) || aboveThreshold(c, j))
270 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
272 currentColour = lowerCaseColours[index];
276 currentColour = colors[index];
281 currentColour = Color.white;
284 if (conservationColouring)
286 currentColour = applyConservation(currentColour, j);
289 return currentColour;
292 public void setLowerCaseColours(Color[] lcolours)
294 lowerCaseColours = lcolours;
298 * Returns the colour for the given residue character. If the residue is
299 * lower-case, and there is a specific colour defined for lower case, that
300 * colour is returned, else the colour for the upper case residue.
303 public Color findColour(char c)
305 if ('a' <= c && c <= 'z' && lowerCaseColours != null)
307 Color colour = lowerCaseColours[symbolIndex[c]];
313 return super.findColour(c);