2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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 java.awt.Color;
25 import java.util.StringTokenizer;
27 import jalview.datamodel.AnnotatedCollectionI;
28 import jalview.datamodel.SequenceCollectionI;
29 import jalview.datamodel.SequenceI;
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);
48 public ColourSchemeI applyTo(AnnotatedCollectionI sg,
49 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
51 UserColourScheme usc = new UserColourScheme(colors);
52 if (lowerCaseColours!=null) {
53 usc.schemeName = new String(schemeName);
54 usc.lowerCaseColours = new Color[lowerCaseColours.length];
55 System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0, lowerCaseColours.length);
59 public UserColourScheme(String colour)
61 super(ResidueProperties.aaIndex);
62 Color col = getColourFromString(colour);
66 System.out.println("Unknown colour!! " + colour);
67 col = createColourFromName(colour);
70 colors = new Color[24];
71 for (int i = 0; i < 24; i++)
78 public Color[] getColours()
83 public Color[] getLowerCaseColours()
85 return lowerCaseColours;
88 public void setName(String name)
93 public String getName()
98 public Color getColourFromString(String colour)
100 colour = colour.trim();
105 int value = Integer.parseInt(colour, 16);
106 col = new Color(value);
107 } catch (NumberFormatException ex)
113 col = ColourSchemeProperty.getAWTColorFromName(colour);
120 java.util.StringTokenizer st = new java.util.StringTokenizer(
122 int r = Integer.parseInt(st.nextToken());
123 int g = Integer.parseInt(st.nextToken());
124 int b = Integer.parseInt(st.nextToken());
125 col = new Color(r, g, b);
126 } catch (Exception ex)
135 public Color createColourFromName(String name)
139 int lsize = name.length();
140 int start = 0, end = lsize / 3;
142 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
144 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
152 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
154 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
156 Color color = new Color(r, g, b);
161 public void parseAppletParameter(String paramValue)
163 // TODO: need a function to generate appletParameter colour string from a UCS
164 StringTokenizer st = new StringTokenizer(paramValue, ";");
166 String token = null, colour, residues;
169 while (st.hasMoreElements())
171 token = st.nextToken().trim();
172 residues = token.substring(0, token.indexOf("="));
173 colour = token.substring(token.indexOf("=") + 1);
175 st2 = new StringTokenizer(residues, " ,");
176 while (st2.hasMoreTokens())
178 token = st2.nextToken();
180 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
185 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
187 if (token.equalsIgnoreCase("lowerCase"))
189 if (lowerCaseColours == null)
191 lowerCaseColours = new Color[23];
193 for (int i = 0; i < 23; i++)
195 if (lowerCaseColours[i] == null)
197 lowerCaseColours[i] = getColourFromString(colour);
204 if (token.equals(token.toLowerCase()))
206 if (lowerCaseColours == null)
208 lowerCaseColours = new Color[23];
210 lowerCaseColours[colIndex] = getColourFromString(colour);
214 colors[colIndex] = getColourFromString(colour);
218 } catch (Exception ex)
220 System.out.println("Error parsing userDefinedColours:\n" + token
227 public Color findColour(char c, int j, SequenceI seq)
230 int index = ResidueProperties.aaIndex[c];
232 if ((threshold == 0) || aboveThreshold(c, j))
234 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
236 currentColour = lowerCaseColours[index];
240 currentColour = colors[index];
245 currentColour = Color.white;
248 if (conservationColouring)
250 currentColour = applyConservation(currentColour, j);
253 return currentColour;
256 public void setLowerCaseColours(Color[] lcolours)
258 lowerCaseColours = lcolours;