JAL-3127 pass reference to AlignViewportI when creating an instance of ColourSchemeI
[jalview.git] / src / jalview / schemes / ScoreColourScheme.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.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
28
29 import java.awt.Color;
30 import java.util.Map;
31
32 /**
33  * DOCUMENT ME!
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class ScoreColourScheme extends ResidueColourScheme
39 {
40   public double min;
41
42   public double max;
43
44   public double[] scores;
45
46   /**
47    * Creates a new ScoreColourScheme object.
48    * 
49    * @param scores
50    *          DOCUMENT ME!
51    * @param min
52    *          DOCUMENT ME!
53    * @param max
54    *          DOCUMENT ME!
55    */
56   public ScoreColourScheme(int symbolIndex[], double[] scores, double min,
57           double max)
58   {
59     super(symbolIndex);
60
61     this.scores = scores;
62     this.min = min;
63     this.max = max;
64
65     // Make colours in constructor
66     // Why wasn't this done earlier?
67     int iSize = scores.length;
68     colors = new Color[scores.length];
69     for (int i = 0; i < iSize; i++)
70     {
71       /*
72        * scale score between min and max to the range 0.0 - 1.0
73        */
74       float score = (float) (scores[i] - (float) min) / (float) (max - min);
75
76       if (score > 1.0f)
77       {
78         score = 1.0f;
79       }
80
81       if (score < 0.0f)
82       {
83         score = 0.0f;
84       }
85       colors[i] = makeColour(score);
86     }
87   }
88
89   @Override
90   public Color findColour(char c, int j, SequenceI seq)
91   {
92     if (Comparison.isGap(c))
93     {
94       return Color.white;
95     }
96     return super.findColour(c);
97   }
98
99   /**
100    * DOCUMENT ME!
101    * 
102    * @param c
103    *          DOCUMENT ME!
104    * 
105    * @return DOCUMENT ME!
106    */
107   public Color makeColour(float c)
108   {
109     return new Color(c, (float) 0.0, (float) 1.0 - c);
110   }
111
112   @Override
113   public String getSchemeName()
114   {
115     return "Score";
116   }
117
118   /**
119    * Returns a new instance of this colour scheme with which the given data may
120    * be coloured
121    */
122   @Override
123   public ColourSchemeI getInstance(AlignViewportI view,
124           AnnotatedCollectionI coll,
125           Map<SequenceI, SequenceCollectionI> hrs)
126   {
127     return new ScoreColourScheme(symbolIndex, scores, min, max);
128   }
129 }