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