JAL-3253 preliminary static fixes for JavaScript part 3 of 3
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
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 jalview.api.AlignViewportI;
24 import jalview.bin.Jalview;
25 import jalview.datamodel.AnnotatedCollectionI;
26 import jalview.util.ColorUtils;
27
28 import java.awt.Color;
29
30 /**
31  * ColourSchemeProperty binds names to hardwired colourschemes and tries to deal
32  * intelligently with mapping unknown names to user defined colourschemes (that
33  * exist or can be created from the string representation of the colourscheme
34  * name - either a hex RGB triplet or a named colour under java.awt.color ). The
35  * values of the colourscheme constants is important for callers of
36  * getColourName(int i), since it can be used to enumerate the set of built in
37  * colours. The FIRST_COLOUR and LAST_COLOUR symbols are provided for this.
38  * 
39  * @author $author$
40  * @version $Revision$
41  */
42 public class ColourSchemeProperty
43 {
44
45   /**
46    * Returns a colour scheme for the given name, with which the given data may
47    * be coloured. The name is not case-sensitive, and may be one of
48    * <ul>
49    * <li>any currently registered colour scheme; Jalview by default
50    * provides</li>
51    * <ul>
52    * <li>Clustal</li>
53    * <li>Blosum62</li>
54    * <li>% Identity</li>
55    * <li>Hydrophobic</li>
56    * <li>Zappo</li>
57    * <li>Taylor</li>
58    * <li>Helix Propensity</li>
59    * <li>Strand Propensity</li>
60    * <li>Turn Propensity</li>
61    * <li>Buried Index</li>
62    * <li>Nucleotide</li>
63    * <li>Purine/Pyrimidine</li>
64    * <li>T-Coffee Scores</li>
65    * <li>RNA Helices</li>
66    * </ul>
67    * <li>the name of a programmatically added colour scheme</li>
68    * <li>an AWT colour name e.g. red</li>
69    * <li>an AWT hex rgb colour e.g. ff2288</li>
70    * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
71    * </ul>
72    * 
73    * If none of these formats is matched, the string is converted to a colour
74    * using a hashing algorithm. For name "None", returns null.
75    * 
76    * @param forData
77    * @param name
78    * @return
79    */
80   public static ColourSchemeI getColourScheme(AlignViewportI view,
81           AnnotatedCollectionI forData,
82           String name)
83   {
84     if (ResidueColourScheme.NONE.equalsIgnoreCase(name))
85     {
86       return null;
87
88     }
89
90     /*
91      * if this is the name of a registered colour scheme, just
92      * create a new instance of it
93      */
94     ColourSchemeI scheme = ColourSchemes.getInstance().getColourScheme(name,
95             view,
96             forData, null);
97     if (scheme != null)
98     {
99       return scheme;
100     }
101
102     /*
103      * try to parse the string as a residues colour scheme
104      * e.g. A=red;T,G=blue etc
105      * else parse the name as a colour specification
106      * e.g. "red" or "ff00ed",
107      * or failing that hash the name to a colour
108      */
109     UserColourScheme ucs = new UserColourScheme(name);
110     return ucs;
111   }
112
113   public static void initRnaHelicesShading(int n)
114   {
115     int i = 0;
116     Jalview j = Jalview.getInstance();
117
118     if (j.rnaHelices == null)
119     {
120       j.rnaHelices = new Color[n + 1];
121     }
122     else if (j.rnaHelices != null && j.rnaHelices.length <= n)
123     {
124       Color[] t = new Color[n + 1];
125       System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
126       i = j.rnaHelices.length;
127       j.rnaHelices = t;
128     }
129     else
130     {
131       return;
132     }
133     // Generate random colors and store
134     for (; i <= n; i++)
135     {
136       j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
137     }
138   }
139
140   /**
141    * delete the existing cached RNA helices colours
142    */
143   public static void resetRnaHelicesShading()
144   {
145     Jalview.getInstance().rnaHelices = null;
146   }
147
148   /**
149    * Returns the name of the colour scheme (or "None" if it is null)
150    * 
151    * @param cs
152    * @return
153    */
154   public static String getColourName(ColourSchemeI cs)
155   {
156     return cs == null ? ResidueColourScheme.NONE : cs.getSchemeName();
157   }
158
159 }