2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.util.Locale;
25 import jalview.api.AlignViewportI;
26 import jalview.bin.ApplicationSingletonProvider;
27 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
28 import jalview.datamodel.AnnotatedCollectionI;
29 import jalview.datamodel.SequenceCollectionI;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.ColorUtils;
33 import java.awt.Color;
34 import java.util.LinkedHashMap;
37 public class ColourSchemes implements ApplicationSingletonI
41 * Returns the singleton instance of this class
45 public static ColourSchemes getInstance()
47 return ApplicationSingletonProvider.getInstance(ColourSchemes.class);
50 private ColourSchemes()
56 * ColourSchemeProperty "static"
58 public Color[] rnaHelices = null;
61 * delete the existing cached RNA helices colours
63 public static void resetRnaHelicesShading()
65 getInstance().rnaHelices = null;
68 public static void initRnaHelicesShading(int n)
71 ColourSchemes j = getInstance();
73 if (j.rnaHelices == null)
75 j.rnaHelices = new Color[n + 1];
77 else if (j.rnaHelices != null && j.rnaHelices.length <= n)
79 Color[] t = new Color[n + 1];
80 System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
81 i = j.rnaHelices.length;
88 // Generate random colors and store
91 j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
96 * a map from scheme name (lower-cased) to an instance of it
98 private Map<String, ColourSchemeI> schemes;
101 * Loads an instance of each standard or user-defined colour scheme
105 void loadColourSchemes()
108 * store in an order-preserving map, so items can be added to menus
109 * in the order in which they are 'discovered'
111 schemes = new LinkedHashMap<>();
113 for (JalviewColourScheme cs : JalviewColourScheme.values())
117 registerColourScheme(
118 cs.getSchemeClass().getDeclaredConstructor().newInstance());
119 } catch (InstantiationException | IllegalAccessException e)
121 System.err.println("Error instantiating colour scheme for "
122 + cs.toString() + " " + e.getMessage());
124 } catch (ReflectiveOperationException roe)
126 roe.printStackTrace();
132 * Registers a colour scheme
136 public void registerColourScheme(ColourSchemeI cs)
138 String name = cs.getSchemeName();
141 System.err.println("ColourScheme name may not be null");
146 * name is lower-case for non-case-sensitive lookup
147 * (name in the colour keeps its true case)
149 String lower = name.toLowerCase(Locale.ROOT);
150 if (schemes.containsKey(lower))
153 .println("Warning: overwriting colour scheme named " + name);
155 schemes.put(lower, cs);
159 * Removes a colour scheme by name
163 public void removeColourScheme(String name)
167 schemes.remove(name.toLowerCase(Locale.ROOT));
172 * Returns an instance of the colour scheme with which the given view may be
176 * name of the colour scheme
179 * the data to be coloured
181 * map from hidden representative sequences to the sequences they
185 public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
186 AnnotatedCollectionI forData,
187 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
193 ColourSchemeI cs = schemes.get(name.toLowerCase(Locale.ROOT));
194 return cs == null ? null : cs.getInstance(viewport, forData);
198 * Returns an instance of the colour scheme with which the given view may be
202 * name of the colour scheme
204 * the data to be coloured
207 public ColourSchemeI getColourScheme(String name,
208 AnnotatedCollectionI forData)
210 return getColourScheme(name, null, forData, null);
214 * Returns an iterable set of the colour schemes, in the order in which they
219 public Iterable<ColourSchemeI> getColourSchemes()
221 return schemes.values();
225 * Answers true if there is a scheme with the given name, else false. The test
226 * is not case-sensitive.
231 public boolean nameExists(String name)
237 return schemes.containsKey(name.toLowerCase(Locale.ROOT));