2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
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.
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.
19 package jalview.schemes;
21 import java.awt.Color;
23 import java.util.StringTokenizer;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.datamodel.SequenceCollectionI;
27 import jalview.datamodel.SequenceI;
29 public class UserColourScheme extends ResidueColourScheme
31 Color[] lowerCaseColours;
33 protected String schemeName;
35 public UserColourScheme()
37 super(ResidueProperties.aaIndex);
40 public UserColourScheme(Color[] newColors)
42 super(ResidueProperties.aaIndex);
46 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
47 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
49 UserColourScheme usc = new UserColourScheme(colors);
50 if (lowerCaseColours!=null) {
51 usc.schemeName = new String(schemeName);
52 usc.lowerCaseColours = new Color[lowerCaseColours.length];
53 System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0, lowerCaseColours.length);
57 public UserColourScheme(String colour)
59 super(ResidueProperties.aaIndex);
60 Color col = getColourFromString(colour);
64 System.out.println("Unknown colour!! " + colour);
65 col = createColourFromName(colour);
68 colors = new Color[24];
69 for (int i = 0; i < 24; i++)
76 public Color[] getColours()
81 public Color[] getLowerCaseColours()
83 return lowerCaseColours;
86 public void setName(String name)
91 public String getName()
96 public Color getColourFromString(String colour)
98 colour = colour.trim();
103 int value = Integer.parseInt(colour, 16);
104 col = new Color(value);
105 } catch (NumberFormatException ex)
111 col = ColourSchemeProperty.getAWTColorFromName(colour);
118 java.util.StringTokenizer st = new java.util.StringTokenizer(
120 int r = Integer.parseInt(st.nextToken());
121 int g = Integer.parseInt(st.nextToken());
122 int b = Integer.parseInt(st.nextToken());
123 col = new Color(r, g, b);
124 } catch (Exception ex)
133 public Color createColourFromName(String name)
137 int lsize = name.length();
138 int start = 0, end = lsize / 3;
140 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
142 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
150 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
152 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
154 Color color = new Color(r, g, b);
159 public void parseAppletParameter(String paramValue)
161 // TODO: need a function to generate appletParameter colour string from a UCS
162 StringTokenizer st = new StringTokenizer(paramValue, ";");
164 String token = null, colour, residues;
167 while (st.hasMoreElements())
169 token = st.nextToken().trim();
170 residues = token.substring(0, token.indexOf("="));
171 colour = token.substring(token.indexOf("=") + 1);
173 st2 = new StringTokenizer(residues, " ,");
174 while (st2.hasMoreTokens())
176 token = st2.nextToken();
178 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
183 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
185 if (token.equalsIgnoreCase("lowerCase"))
187 if (lowerCaseColours == null)
189 lowerCaseColours = new Color[23];
191 for (int i = 0; i < 23; i++)
193 if (lowerCaseColours[i] == null)
195 lowerCaseColours[i] = getColourFromString(colour);
202 if (token.equals(token.toLowerCase()))
204 if (lowerCaseColours == null)
206 lowerCaseColours = new Color[23];
208 lowerCaseColours[colIndex] = getColourFromString(colour);
212 colors[colIndex] = getColourFromString(colour);
216 } catch (Exception ex)
218 System.out.println("Error parsing userDefinedColours:\n" + token
225 public Color findColour(char c, int j, SequenceI seq)
228 int index = ResidueProperties.aaIndex[c];
230 if ((threshold == 0) || aboveThreshold(c, j))
232 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
234 currentColour = lowerCaseColours[index];
238 currentColour = colors[index];
243 currentColour = Color.white;
246 if (conservationColouring)
248 currentColour = applyConservation(currentColour, j);
251 return currentColour;
254 public void setLowerCaseColours(Color[] lcolours)
256 lowerCaseColours = lcolours;