User defined colourschemes are automatically generated from colour names or RGB colou...
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.schemes;
20
21 import java.awt.*;
22
23 /**
24  * ColourSchemeProperty Binds names to hardwired colourschemes and tries to deal
25  * intelligently with mapping unknown names to user defined colourschemes (that
26  * exist or can be created from the string representation of the colourscheme
27  * name - either a hex RGB triplet or a named colour under java.awt.color )
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class ColourSchemeProperty
33 {
34   /** DOCUMENT ME!! */
35   public static final int CLUSTAL = 0;
36
37   /** DOCUMENT ME!! */
38   public static final int BLOSUM = 1;
39
40   /** DOCUMENT ME!! */
41   public static final int PID = 2;
42
43   /** DOCUMENT ME!! */
44   public static final int ZAPPO = 3;
45
46   /** DOCUMENT ME!! */
47   public static final int TAYLOR = 4;
48
49   /** DOCUMENT ME!! */
50   public static final int HYDROPHOBIC = 5;
51
52   /** DOCUMENT ME!! */
53   public static final int HELIX = 6;
54
55   /** DOCUMENT ME!! */
56   public static final int STRAND = 7;
57
58   /** DOCUMENT ME!! */
59   public static final int TURN = 8;
60
61   /** DOCUMENT ME!! */
62   public static final int BURIED = 9;
63
64   /** DOCUMENT ME!! */
65   public static final int NUCLEOTIDE = 10;
66
67   /** DOCUMENT ME!! */
68   public static final int USER_DEFINED = 11;
69
70   /** DOCUMENT ME!! */
71   public static final int NONE = 12;
72
73   /**
74    * DOCUMENT ME!
75    * 
76    * @param name
77    *                DOCUMENT ME!
78    * 
79    * @return DOCUMENT ME!
80    */
81   public static int getColourIndexFromName(String name)
82   {
83     int ret = 12;
84
85     if (name.equalsIgnoreCase("Clustal"))
86     {
87       ret = CLUSTAL;
88     }
89     else if (name.equalsIgnoreCase("Blosum62"))
90     {
91       ret = BLOSUM;
92     }
93     else if (name.equalsIgnoreCase("% Identity"))
94     {
95       ret = PID;
96     }
97     else if (name.equalsIgnoreCase("Zappo"))
98     {
99       ret = ZAPPO;
100     }
101     else if (name.equalsIgnoreCase("Taylor"))
102     {
103       ret = TAYLOR;
104     }
105     else if (name.equalsIgnoreCase("Hydrophobic"))
106     {
107       ret = HYDROPHOBIC;
108     }
109     else if (name.equalsIgnoreCase("Helix Propensity"))
110     {
111       ret = HELIX;
112     }
113     else if (name.equalsIgnoreCase("Strand Propensity"))
114     {
115       ret = STRAND;
116     }
117     else if (name.equalsIgnoreCase("Turn Propensity"))
118     {
119       ret = TURN;
120     }
121     else if (name.equalsIgnoreCase("Buried Index"))
122     {
123       ret = BURIED;
124     }
125     else if (name.equalsIgnoreCase("Nucleotide"))
126     {
127       ret = NUCLEOTIDE;
128     }
129     else if (name.equalsIgnoreCase("User Defined"))
130     {
131       ret = USER_DEFINED;
132     }
133
134     return ret;
135   }
136
137   /**
138    * DOCUMENT ME!
139    * 
140    * @param cs
141    *                DOCUMENT ME!
142    * 
143    * @return DOCUMENT ME!
144    */
145   public static String getColourName(ColourSchemeI cs)
146   {
147
148     int index = 12;
149
150     if (cs instanceof ClustalxColourScheme)
151     {
152       index = CLUSTAL;
153     }
154     else if (cs instanceof Blosum62ColourScheme)
155     {
156       index = BLOSUM;
157     }
158     else if (cs instanceof PIDColourScheme)
159     {
160       index = PID;
161     }
162     else if (cs instanceof ZappoColourScheme)
163     {
164       index = ZAPPO;
165     }
166     else if (cs instanceof TaylorColourScheme)
167     {
168       index = TAYLOR;
169     }
170     else if (cs instanceof HydrophobicColourScheme)
171     {
172       index = HYDROPHOBIC;
173     }
174     else if (cs instanceof HelixColourScheme)
175     {
176       index = HELIX;
177     }
178     else if (cs instanceof StrandColourScheme)
179     {
180       index = STRAND;
181     }
182     else if (cs instanceof TurnColourScheme)
183     {
184       index = TURN;
185     }
186     else if (cs instanceof BuriedColourScheme)
187     {
188       index = BURIED;
189     }
190     else if (cs instanceof NucleotideColourScheme)
191     {
192       index = NUCLEOTIDE;
193     }
194     else if (cs instanceof UserColourScheme)
195     {
196       if ((((UserColourScheme) cs).getName() != null)
197               && (((UserColourScheme) cs).getName().length() > 0))
198       {
199         return ((UserColourScheme) cs).getName();
200       }
201       // get default colourscheme name
202       index = USER_DEFINED;
203     }
204
205     return getColourName(index);
206   }
207
208   /**
209    * DOCUMENT ME!
210    * 
211    * @param index
212    *                DOCUMENT ME!
213    * 
214    * @return DOCUMENT ME!
215    */
216   public static String getColourName(int index)
217   {
218     String ret = null;
219
220     switch (index)
221     {
222     case CLUSTAL:
223       ret = "Clustal";
224
225       break;
226
227     case BLOSUM:
228       ret = "Blosum62";
229
230       break;
231
232     case PID:
233       ret = "% Identity";
234
235       break;
236
237     case ZAPPO:
238       ret = "Zappo";
239
240       break;
241
242     case TAYLOR:
243       ret = "Taylor";
244       break;
245
246     case HYDROPHOBIC:
247       ret = "Hydrophobic";
248
249       break;
250
251     case HELIX:
252       ret = "Helix Propensity";
253
254       break;
255
256     case STRAND:
257       ret = "Strand Propensity";
258
259       break;
260
261     case TURN:
262       ret = "Turn Propensity";
263
264       break;
265
266     case BURIED:
267       ret = "Buried Index";
268
269       break;
270
271     case NUCLEOTIDE:
272       ret = "Nucleotide";
273
274       break;
275
276     case USER_DEFINED:
277       ret = "User Defined";
278
279       break;
280
281     default:
282       ret = "None";
283
284       break;
285     }
286
287     return ret;
288   }
289
290   /**
291    * DOCUMENT ME!
292    * 
293    * @param al
294    *                DOCUMENT ME!
295    * @param name
296    *                DOCUMENT ME!
297    * 
298    * @return DOCUMENT ME!
299    */
300   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
301           String name)
302   {
303     return getColour(al.getSequences(), al.getWidth(), name);
304   }
305
306   /**
307    * DOCUMENT ME!
308    * 
309    * @param seqs
310    *                DOCUMENT ME!
311    * @param width
312    *                DOCUMENT ME!
313    * @param name
314    *                DOCUMENT ME!
315    * 
316    * @return DOCUMENT ME!
317    */
318   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
319           String name)
320   {
321     int colindex = getColourIndexFromName(name);
322     if (colindex > USER_DEFINED)
323     {
324       try
325       {
326         return new UserColourScheme(name);
327       } catch (Exception e)
328       {
329         // System.err.println("Ignoring unknown colourscheme name");
330       }
331     }
332     return getColour(seqs, width, getColourIndexFromName(name));
333   }
334
335   /**
336    * DOCUMENT ME!
337    * 
338    * @param seqs
339    *                DOCUMENT ME!
340    * @param width
341    *                DOCUMENT ME!
342    * @param index
343    *                DOCUMENT ME!
344    * 
345    * @return DOCUMENT ME!
346    */
347   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
348           int index)
349   {
350     ColourSchemeI cs = null;
351
352     switch (index)
353     {
354     case CLUSTAL:
355       cs = new ClustalxColourScheme(seqs, width);
356
357       break;
358
359     case BLOSUM:
360       cs = new Blosum62ColourScheme();
361
362       break;
363
364     case PID:
365       cs = new PIDColourScheme();
366
367       break;
368
369     case ZAPPO:
370       cs = new ZappoColourScheme();
371
372       break;
373
374     case TAYLOR:
375       cs = new TaylorColourScheme();
376       break;
377
378     case HYDROPHOBIC:
379       cs = new HydrophobicColourScheme();
380
381       break;
382
383     case HELIX:
384       cs = new HelixColourScheme();
385
386       break;
387
388     case STRAND:
389       cs = new StrandColourScheme();
390
391       break;
392
393     case TURN:
394       cs = new TurnColourScheme();
395
396       break;
397
398     case BURIED:
399       cs = new BuriedColourScheme();
400
401       break;
402
403     case NUCLEOTIDE:
404       cs = new NucleotideColourScheme();
405
406       break;
407
408     case USER_DEFINED:
409       Color[] col = new Color[24];
410       for (int i = 0; i < 24; i++)
411       {
412         col[i] = Color.white;
413       }
414       cs = new UserColourScheme(col);
415       break;
416
417     default:
418       break;
419     }
420
421     return cs;
422   }
423
424   public static Color getAWTColorFromName(String name)
425   {
426     Color col = null;
427     name = name.toLowerCase();
428     if (name.equals("black"))
429     {
430       col = Color.black;
431     }
432     else if (name.equals("blue"))
433     {
434       col = Color.blue;
435     }
436     else if (name.equals("cyan"))
437     {
438       col = Color.cyan;
439     }
440     else if (name.equals("darkGray"))
441     {
442       col = Color.darkGray;
443     }
444     else if (name.equals("gray"))
445     {
446       col = Color.gray;
447     }
448     else if (name.equals("green"))
449     {
450       col = Color.green;
451     }
452     else if (name.equals("lightGray"))
453     {
454       col = Color.lightGray;
455     }
456     else if (name.equals("magenta"))
457     {
458       col = Color.magenta;
459     }
460     else if (name.equals("orange"))
461     {
462       col = Color.orange;
463     }
464     else if (name.equals("pink"))
465     {
466       col = Color.pink;
467     }
468     else if (name.equals("red"))
469     {
470       col = Color.red;
471     }
472     else if (name.equals("white"))
473     {
474       col = Color.white;
475     }
476     else if (name.equals("yellow"))
477     {
478       col = Color.yellow;
479     }
480
481     return col;
482   }
483 }