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 (ColourSchemes) ApplicationSingletonProvider
48 .getInstance(ColourSchemes.class);
51 private ColourSchemes()
57 * ColourSchemeProperty "static"
59 public Color[] rnaHelices = null;
62 * delete the existing cached RNA helices colours
64 public static void resetRnaHelicesShading()
66 getInstance().rnaHelices = null;
69 public static void initRnaHelicesShading(int n)
72 ColourSchemes j = getInstance();
74 if (j.rnaHelices == null)
76 j.rnaHelices = new Color[n + 1];
78 else if (j.rnaHelices != null && j.rnaHelices.length <= n)
80 Color[] t = new Color[n + 1];
81 System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
82 i = j.rnaHelices.length;
89 // Generate random colors and store
92 j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
97 * a map from scheme name (lower-cased) to an instance of it
99 private Map<String, ColourSchemeI> schemes;
102 * Loads an instance of each standard or user-defined colour scheme
106 void loadColourSchemes()
109 * store in an order-preserving map, so items can be added to menus
110 * in the order in which they are 'discovered'
112 schemes = new LinkedHashMap<>();
114 for (JalviewColourScheme cs : JalviewColourScheme.values())
118 registerColourScheme(
119 cs.getSchemeClass().getDeclaredConstructor().newInstance());
120 } catch (InstantiationException | IllegalAccessException e)
122 System.err.println("Error instantiating colour scheme for "
123 + cs.toString() + " " + e.getMessage());
125 } catch (ReflectiveOperationException roe)
127 roe.printStackTrace();
133 * Registers a colour scheme
137 public void registerColourScheme(ColourSchemeI cs)
139 String name = cs.getSchemeName();
142 System.err.println("ColourScheme name may not be null");
147 * name is lower-case for non-case-sensitive lookup
148 * (name in the colour keeps its true case)
150 String lower = name.toLowerCase(Locale.ROOT);
151 if (schemes.containsKey(lower))
154 .println("Warning: overwriting colour scheme named " + name);
156 schemes.put(lower, cs);
160 * Removes a colour scheme by name
164 public void removeColourScheme(String name)
168 schemes.remove(name.toLowerCase(Locale.ROOT));
173 * Returns an instance of the colour scheme with which the given view may be
177 * name of the colour scheme
180 * the data to be coloured
182 * map from hidden representative sequences to the sequences they
186 public ColourSchemeI getColourScheme(String name,
187 AlignViewportI viewport, AnnotatedCollectionI forData,
188 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
194 ColourSchemeI cs = schemes.get(name.toLowerCase(Locale.ROOT));
195 return cs == null ? null
196 : cs.getInstance(viewport, forData);
200 * Returns an instance of the colour scheme with which the given view may be
204 * name of the colour scheme
206 * the data to be coloured
209 public ColourSchemeI getColourScheme(String name,
210 AnnotatedCollectionI forData)
212 return getColourScheme(name, null, forData, null);
216 * Returns an iterable set of the colour schemes, in the order in which they
221 public Iterable<ColourSchemeI> getColourSchemes()
223 return schemes.values();
227 * Answers true if there is a scheme with the given name, else false. The test
228 * is not case-sensitive.
233 public boolean nameExists(String name)
239 return schemes.containsKey(name.toLowerCase(Locale.ROOT));