JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[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
26 /**
27  * ColourSchemeProperty binds names to hardwired colourschemes and tries to deal
28  * intelligently with mapping unknown names to user defined colourschemes (that
29  * exist or can be created from the string representation of the colourscheme
30  * name - either a hex RGB triplet or a named colour under java.awt.color ). The
31  * values of the colourscheme constants is important for callers of
32  * getColourName(int i), since it can be used to enumerate the set of built in
33  * colours. The FIRST_COLOUR and LAST_COLOUR symbols are provided for this.
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class ColourSchemeProperty
39 {
40
41   private ColourSchemeProperty()
42   {
43     // requires static call to getColourScheme(...).
44   }
45
46   /**
47    * Returns a colour scheme for the given name, with which the given data may
48    * be coloured. The name is not case-sensitive, and may be one of
49    * <ul>
50    * <li>any currently registered colour scheme; Jalview by default
51    * provides</li>
52    * <ul>
53    * <li>Clustal</li>
54    * <li>Blosum62</li>
55    * <li>% Identity</li>
56    * <li>Hydrophobic</li>
57    * <li>Zappo</li>
58    * <li>Taylor</li>
59    * <li>Helix Propensity</li>
60    * <li>Strand Propensity</li>
61    * <li>Turn Propensity</li>
62    * <li>Buried Index</li>
63    * <li>Nucleotide</li>
64    * <li>Purine/Pyrimidine</li>
65    * <li>T-Coffee Scores</li>
66    * <li>RNA Helices</li>
67    * </ul>
68    * <li>the name of a programmatically added colour scheme</li>
69    * <li>an AWT colour name e.g. red</li>
70    * <li>an AWT hex rgb colour e.g. ff2288</li>
71    * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
72    * </ul>
73    * 
74    * If none of these formats is matched, the string is converted to a colour
75    * using a hashing algorithm. For name "None", returns null.
76    * 
77    * @param forData
78    * @param name
79    * @return
80    */
81   public static ColourSchemeI getColourScheme(AlignViewportI view,
82           AnnotatedCollectionI forData,
83           String name)
84   {
85     if (ResidueColourScheme.NONE.equalsIgnoreCase(name))
86     {
87       return null;
88
89     }
90
91     /*
92      * if this is the name of a registered colour scheme, just
93      * create a new instance of it
94      */
95     ColourSchemeI scheme = ColourSchemes.getInstance().getColourScheme(name,
96             view,
97             forData, null);
98     if (scheme != null)
99     {
100       return scheme;
101     }
102
103     /*
104      * try to parse the string as a residues colour scheme
105      * e.g. A=red;T,G=blue etc
106      * else parse the name as a colour specification
107      * e.g. "red" or "ff00ed",
108      * or failing that hash the name to a colour
109      */
110     UserColourScheme ucs = new UserColourScheme(name);
111     return ucs;
112   }
113
114   /**
115    * Returns the name of the colour scheme (or "None" if it is null)
116    * 
117    * @param cs
118    * @return
119    */
120   public static String getColourName(ColourSchemeI cs)
121   {
122     return cs == null ? ResidueColourScheme.NONE : cs.getSchemeName();
123   }
124
125 }