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 jalview.api.AlignViewportI;
24 import jalview.bin.ApplicationSingletonProvider;
25 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.ColorUtils;
31 import java.awt.Color;
32 import java.util.LinkedHashMap;
35 public class ColourSchemes implements ApplicationSingletonI
39 * Returns the singleton instance of this class
43 public static ColourSchemes getInstance()
45 return (ColourSchemes) ApplicationSingletonProvider
46 .getInstance(ColourSchemes.class);
49 private ColourSchemes()
55 * ColourSchemeProperty "static"
57 public Color[] rnaHelices = null;
60 * delete the existing cached RNA helices colours
62 public static void resetRnaHelicesShading()
64 getInstance().rnaHelices = null;
67 public static void initRnaHelicesShading(int n)
70 ColourSchemes j = getInstance();
72 if (j.rnaHelices == null)
74 j.rnaHelices = new Color[n + 1];
76 else if (j.rnaHelices != null && j.rnaHelices.length <= n)
78 Color[] t = new Color[n + 1];
79 System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
80 i = j.rnaHelices.length;
87 // Generate random colors and store
90 j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
95 * a map from scheme name (lower-cased) to an instance of it
97 private Map<String, ColourSchemeI> schemes;
100 * Loads an instance of each standard or user-defined colour scheme
104 void loadColourSchemes()
107 * store in an order-preserving map, so items can be added to menus
108 * in the order in which they are 'discovered'
110 schemes = new LinkedHashMap<>();
112 for (JalviewColourScheme cs : JalviewColourScheme.values())
116 registerColourScheme(
117 cs.getSchemeClass().getDeclaredConstructor().newInstance());
118 } catch (InstantiationException | IllegalAccessException e)
120 System.err.println("Error instantiating colour scheme for "
121 + cs.toString() + " " + e.getMessage());
123 } catch (ReflectiveOperationException roe)
125 roe.printStackTrace();
131 * Registers a colour scheme
135 public void registerColourScheme(ColourSchemeI cs)
137 String name = cs.getSchemeName();
140 System.err.println("ColourScheme name may not be null");
145 * name is lower-case for non-case-sensitive lookup
146 * (name in the colour keeps its true case)
148 String lower = name.toLowerCase();
149 if (schemes.containsKey(lower))
152 .println("Warning: overwriting colour scheme named " + name);
154 schemes.put(lower, cs);
158 * Removes a colour scheme by name
162 public void removeColourScheme(String name)
166 schemes.remove(name.toLowerCase());
171 * Returns an instance of the colour scheme with which the given view may be
175 * name of the colour scheme
178 * the data to be coloured
180 * map from hidden representative sequences to the sequences they
184 public ColourSchemeI getColourScheme(String name,
185 AlignViewportI viewport, AnnotatedCollectionI forData,
186 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
192 ColourSchemeI cs = schemes.get(name.toLowerCase());
193 return cs == null ? null
194 : 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());