Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.schemes

File ColourSchemes.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
37% of files have more coverage

Code metrics

20
45
13
1
255
138
26
0.58
3.46
13
2

Classes

Class Line # Actions
ColourSchemes 39 45 26
0.5128205451.3%
 

Contributing tests

This file is covered by 199 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.schemes;
22   
23    import java.util.LinkedHashMap;
24    import java.util.Locale;
25    import java.util.Map;
26   
27    import jalview.api.AlignViewportI;
28    import jalview.bin.ApplicationSingletonProvider;
29    import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
30    import jalview.datamodel.AnnotatedCollectionI;
31    import jalview.datamodel.SequenceCollectionI;
32    import jalview.datamodel.SequenceI;
33    import jalview.util.ColorUtils;
34   
35    import java.awt.Color;
36    import java.util.LinkedHashMap;
37    import java.util.Map;
38   
 
39    public class ColourSchemes implements ApplicationSingletonI
40    {
41   
42    /**
43    * Returns the singleton instance of this class
44    *
45    * @return
46    */
 
47  666 toggle public static ColourSchemes getInstance()
48    {
49  666 return ApplicationSingletonProvider.getInstance(ColourSchemes.class);
50    }
51   
 
52  54 toggle private ColourSchemes()
53    {
54  54 loadColourSchemes();
55    }
56   
57    /**
58    * ColourSchemeProperty "static"
59    */
60    public Color[] rnaHelices = null;
61   
62    /**
63    * delete the existing cached RNA helices colours
64    */
 
65  0 toggle public static void resetRnaHelicesShading()
66    {
67  0 getInstance().rnaHelices = null;
68    }
69   
 
70  0 toggle public static void initRnaHelicesShading(int n)
71    {
72  0 int i = 0;
73  0 ColourSchemes j = getInstance();
74   
75  0 if (j.rnaHelices == null)
76    {
77  0 j.rnaHelices = new Color[n + 1];
78    }
79  0 else if (j.rnaHelices != null && j.rnaHelices.length <= n)
80    {
81  0 Color[] t = new Color[n + 1];
82  0 System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
83  0 i = j.rnaHelices.length;
84  0 j.rnaHelices = t;
85    }
86    else
87    {
88  0 return;
89    }
90    // Generate random colors and store
91  0 for (; i <= n; i++)
92    {
93  0 j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
94    }
95    }
96   
97    /**
98    * a map from scheme name (lower-cased) to an instance of it
99    */
100    private Map<String, ColourSchemeI> schemes;
101   
102    /**
103    * Loads an instance of each standard or user-defined colour scheme
104    *
105    * @return
106    */
 
107  54 toggle void loadColourSchemes()
108    {
109    /*
110    * store in an order-preserving map, so items can be added to menus
111    * in the order in which they are 'discovered'
112    */
113  54 schemes = new LinkedHashMap<>();
114   
115  54 for (JalviewColourScheme cs : JalviewColourScheme.values())
116    {
117  1242 try
118    {
119  1242 registerColourScheme(
120    cs.getSchemeClass().getDeclaredConstructor().newInstance());
121    } catch (InstantiationException | IllegalAccessException e)
122    {
123  0 jalview.bin.Console
124    .errPrintln("Error instantiating colour scheme for "
125    + cs.toString() + " " + e.getMessage());
126  0 e.printStackTrace();
127    } catch (ReflectiveOperationException roe)
128    {
129  0 roe.printStackTrace();
130    }
131    }
132    }
133   
134    /**
135    * Registers a colour scheme
136    *
137    * @param cs
138    */
 
139  1244 toggle public void registerColourScheme(ColourSchemeI cs)
140    {
141  1244 String name = cs.getSchemeName();
142  1244 if (name == null)
143    {
144  0 jalview.bin.Console.errPrintln("ColourScheme name may not be null");
145  0 return;
146    }
147   
148    /*
149    * name is lower-case for non-case-sensitive lookup
150    * (name in the colour keeps its true case)
151    */
152  1244 String lower = getColourSchemeShortName(cs);
153  1244 if (schemes.containsKey(lower))
154    {
155  0 System.err
156    .println("Warning: overwriting colour scheme named " + name);
157    }
158  1244 schemes.put(lower, cs);
159    }
160   
 
161  1244 toggle private String getColourSchemeShortName(ColourSchemeI cs)
162    {
163  1244 return getColourSchemeShortName(cs.getSchemeName());
164    }
165   
 
166  1377 toggle private String getColourSchemeShortName(String name)
167    {
168  1377 if (name == null)
169  0 return null;
170  1377 return name.toLowerCase(Locale.ROOT).replaceAll("%", "pc")
171    .replaceAll("[^a-z0-9]", "-").replaceAll("--+", "-");
172    }
173   
174    /**
175    * Removes a colour scheme by name
176    *
177    * @param name
178    */
 
179  0 toggle public void removeColourScheme(String name)
180    {
181  0 if (name != null)
182    {
183  0 schemes.remove(getColourSchemeShortName(name));
184    }
185    }
186   
187    /**
188    * Returns an instance of the colour scheme with which the given view may be
189    * coloured
190    *
191    * @param name
192    * name of the colour scheme
193    * @param viewport
194    * @param forData
195    * the data to be coloured
196    * @param optional
197    * map from hidden representative sequences to the sequences they
198    * represent
199    * @return
200    */
 
201  127 toggle public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
202    AnnotatedCollectionI forData,
203    Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
204    {
205  127 if (name == null)
206    {
207  0 return null;
208    }
209  127 ColourSchemeI cs = schemes.get(getColourSchemeShortName(name));
210  127 return cs == null ? null : cs.getInstance(viewport, forData);
211    }
212   
213    /**
214    * Returns an instance of the colour scheme with which the given view may be
215    * coloured
216    *
217    * @param name
218    * name of the colour scheme
219    * @param forData
220    * the data to be coloured
221    * @return
222    */
 
223  25 toggle public ColourSchemeI getColourScheme(String name,
224    AnnotatedCollectionI forData)
225    {
226  25 return getColourScheme(name, null, forData, null);
227    }
228   
229    /**
230    * Returns an iterable set of the colour schemes, in the order in which they
231    * were added
232    *
233    * @return
234    */
 
235  558 toggle public Iterable<ColourSchemeI> getColourSchemes()
236    {
237  558 return schemes.values();
238    }
239   
240    /**
241    * Answers true if there is a scheme with the given name, else false. The test
242    * is not case-sensitive.
243    *
244    * @param name
245    * @return
246    */
 
247  7 toggle public boolean nameExists(String name)
248    {
249  7 if (name == null)
250    {
251  1 return false;
252    }
253  6 return schemes.containsKey(getColourSchemeShortName(name));
254    }
255    }