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