2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
22 import jalview.datamodel.SequenceI;
24 public class UserColourScheme extends ResidueColourScheme
26 Color[] lowerCaseColours;
28 protected String schemeName;
30 public UserColourScheme()
32 super(ResidueProperties.aaIndex);
35 public UserColourScheme(Color[] newColors)
37 super(ResidueProperties.aaIndex);
41 public UserColourScheme(String colour)
43 super(ResidueProperties.aaIndex);
44 Color col = getColourFromString(colour);
48 System.out.println("Unknown colour!! " + colour);
49 col = createColourFromName(colour);
52 colors = new Color[24];
53 for (int i = 0; i < 24; i++)
60 public Color[] getColours()
65 public Color[] getLowerCaseColours()
67 return lowerCaseColours;
70 public void setName(String name)
75 public String getName()
80 public Color getColourFromString(String colour)
82 colour = colour.trim();
87 int value = Integer.parseInt(colour, 16);
88 col = new Color(value);
89 } catch (NumberFormatException ex)
95 col = ColourSchemeProperty.getAWTColorFromName(colour);
102 java.util.StringTokenizer st = new java.util.StringTokenizer(
104 int r = Integer.parseInt(st.nextToken());
105 int g = Integer.parseInt(st.nextToken());
106 int b = Integer.parseInt(st.nextToken());
107 col = new Color(r, g, b);
108 } catch (Exception ex)
117 public Color createColourFromName(String name)
121 int lsize = name.length();
122 int start = 0, end = lsize / 3;
124 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
126 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
134 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
136 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
138 Color color = new Color(r, g, b);
143 public void parseAppletParameter(String paramValue)
145 StringTokenizer st = new StringTokenizer(paramValue, ";");
147 String token = null, colour, residues;
150 while (st.hasMoreElements())
152 token = st.nextToken().trim();
153 residues = token.substring(0, token.indexOf("="));
154 colour = token.substring(token.indexOf("=") + 1);
156 st2 = new StringTokenizer(residues, " ,");
157 while (st2.hasMoreTokens())
159 token = st2.nextToken();
161 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
166 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
168 if (token.equalsIgnoreCase("lowerCase"))
170 if (lowerCaseColours == null)
172 lowerCaseColours = new Color[23];
174 for (int i = 0; i < 23; i++)
176 if (lowerCaseColours[i] == null)
178 lowerCaseColours[i] = getColourFromString(colour);
185 if (token.equals(token.toLowerCase()))
187 if (lowerCaseColours == null)
189 lowerCaseColours = new Color[23];
191 lowerCaseColours[colIndex] = getColourFromString(colour);
195 colors[colIndex] = getColourFromString(colour);
199 } catch (Exception ex)
201 System.out.println("Error parsing userDefinedColours:\n" + token
208 public Color findColour(char c, int j, SequenceI seq)
211 int index = ResidueProperties.aaIndex[c];
213 if ((threshold == 0) || aboveThreshold(c, j))
215 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
217 currentColour = lowerCaseColours[index];
221 currentColour = colors[index];
226 currentColour = Color.white;
229 if (conservationColouring)
231 currentColour = applyConservation(currentColour, j);
234 return currentColour;
237 public void setLowerCaseColours(Color[] lcolours)
239 lowerCaseColours = lcolours;