2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
18 package jalview.schemes;
20 import java.awt.Color;
21 import java.util.StringTokenizer;
23 public class UserColourScheme extends ResidueColourScheme
25 Color[] lowerCaseColours;
27 protected String schemeName;
29 public UserColourScheme()
33 public UserColourScheme(Color[] newColors)
38 public UserColourScheme(String colour)
40 Color col = getColourFromString(colour);
44 System.out.println("Unknown colour!! " + colour);
45 col = createColourFromName(colour);
48 colors = new Color[24];
49 for (int i = 0; i < 24; i++)
56 public Color[] getColours()
61 public Color[] getLowerCaseColours()
63 return lowerCaseColours;
66 public void setName(String name)
71 public String getName()
76 public Color getColourFromString(String colour)
78 colour = colour.trim();
83 int value = Integer.parseInt(colour, 16);
84 col = new Color(value);
85 } catch (NumberFormatException ex)
91 col = ColourSchemeProperty.getAWTColorFromName(colour);
98 java.util.StringTokenizer st = new java.util.StringTokenizer(
100 int r = Integer.parseInt(st.nextToken());
101 int g = Integer.parseInt(st.nextToken());
102 int b = Integer.parseInt(st.nextToken());
103 col = new Color(r, g, b);
104 } catch (Exception ex)
113 public Color createColourFromName(String name)
117 int lsize = name.length();
118 int start = 0, end = lsize / 3;
120 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
122 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
130 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
132 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
134 Color color = new Color(r, g, b);
139 public void parseAppletParameter(String paramValue)
141 StringTokenizer st = new StringTokenizer(paramValue, ";");
143 String token = null, colour, residues;
146 while (st.hasMoreElements())
148 token = st.nextToken().trim();
149 residues = token.substring(0, token.indexOf("="));
150 colour = token.substring(token.indexOf("=") + 1);
152 st2 = new StringTokenizer(residues, " ,");
153 while (st2.hasMoreTokens())
155 token = st2.nextToken();
157 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
162 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
164 if (token.equalsIgnoreCase("lowerCase"))
166 if (lowerCaseColours == null)
168 lowerCaseColours = new Color[23];
170 for (int i = 0; i < 23; i++)
172 if (lowerCaseColours[i] == null)
174 lowerCaseColours[i] = getColourFromString(colour);
181 if (token.equals(token.toLowerCase()))
183 if (lowerCaseColours == null)
185 lowerCaseColours = new Color[23];
187 lowerCaseColours[colIndex] = getColourFromString(colour);
191 colors[colIndex] = getColourFromString(colour);
195 } catch (Exception ex)
197 System.out.println("Error parsing userDefinedColours:\n" + token
204 public Color findColour(char c, int j, int sequenceIndex)
207 int index = ResidueProperties.aaIndex[c];
209 if ((threshold == 0) || aboveThreshold(c, j))
211 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
213 currentColour = lowerCaseColours[index];
217 currentColour = colors[index];
222 currentColour = Color.white;
225 if (conservationColouring)
227 currentColour = applyConservation(currentColour, j);
230 return currentColour;
233 public void setLowerCaseColours(Color[] lcolours)
235 lowerCaseColours = lcolours;