2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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;
22 import java.util.StringTokenizer;
23 import jalview.datamodel.SequenceI;
25 public class UserColourScheme extends ResidueColourScheme
27 Color[] lowerCaseColours;
29 protected String schemeName;
31 public UserColourScheme()
33 super(ResidueProperties.aaIndex);
36 public UserColourScheme(Color[] newColors)
38 super(ResidueProperties.aaIndex);
42 public UserColourScheme(String colour)
44 super(ResidueProperties.aaIndex);
45 Color col = getColourFromString(colour);
49 System.out.println("Unknown colour!! " + colour);
50 col = createColourFromName(colour);
53 colors = new Color[24];
54 for (int i = 0; i < 24; i++)
61 public Color[] getColours()
66 public Color[] getLowerCaseColours()
68 return lowerCaseColours;
71 public void setName(String name)
76 public String getName()
81 public Color getColourFromString(String colour)
83 colour = colour.trim();
88 int value = Integer.parseInt(colour, 16);
89 col = new Color(value);
90 } catch (NumberFormatException ex)
96 col = ColourSchemeProperty.getAWTColorFromName(colour);
103 java.util.StringTokenizer st = new java.util.StringTokenizer(
105 int r = Integer.parseInt(st.nextToken());
106 int g = Integer.parseInt(st.nextToken());
107 int b = Integer.parseInt(st.nextToken());
108 col = new Color(r, g, b);
109 } catch (Exception ex)
118 public Color createColourFromName(String name)
122 int lsize = name.length();
123 int start = 0, end = lsize / 3;
125 int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
127 r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
135 g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
137 b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
139 Color color = new Color(r, g, b);
144 public void parseAppletParameter(String paramValue)
146 StringTokenizer st = new StringTokenizer(paramValue, ";");
148 String token = null, colour, residues;
151 while (st.hasMoreElements())
153 token = st.nextToken().trim();
154 residues = token.substring(0, token.indexOf("="));
155 colour = token.substring(token.indexOf("=") + 1);
157 st2 = new StringTokenizer(residues, " ,");
158 while (st2.hasMoreTokens())
160 token = st2.nextToken();
162 if (ResidueProperties.aaIndex[token.charAt(0)] == -1)
167 int colIndex = ResidueProperties.aaIndex[token.charAt(0)];
169 if (token.equalsIgnoreCase("lowerCase"))
171 if (lowerCaseColours == null)
173 lowerCaseColours = new Color[23];
175 for (int i = 0; i < 23; i++)
177 if (lowerCaseColours[i] == null)
179 lowerCaseColours[i] = getColourFromString(colour);
186 if (token.equals(token.toLowerCase()))
188 if (lowerCaseColours == null)
190 lowerCaseColours = new Color[23];
192 lowerCaseColours[colIndex] = getColourFromString(colour);
196 colors[colIndex] = getColourFromString(colour);
200 } catch (Exception ex)
202 System.out.println("Error parsing userDefinedColours:\n" + token
209 public Color findColour(char c, int j, SequenceI seq)
212 int index = ResidueProperties.aaIndex[c];
214 if ((threshold == 0) || aboveThreshold(c, j))
216 if (lowerCaseColours != null && 'a' <= c && c <= 'z')
218 currentColour = lowerCaseColours[index];
222 currentColour = colors[index];
227 currentColour = Color.white;
230 if (conservationColouring)
232 currentColour = applyConservation(currentColour, j);
235 return currentColour;
238 public void setLowerCaseColours(Color[] lcolours)
240 lowerCaseColours = lcolours;