JAL-3383 removing colour caching (to a separate branch)
[jalview.git] / src / jalview / renderer / OverviewResColourFinder.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.renderer;
22
23 import jalview.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.util.Comparison;
27
28 import java.awt.Color;
29
30 public class OverviewResColourFinder extends ResidueColourFinder
31 {
32   public static final Color OVERVIEW_DEFAULT_GAP = Color.lightGray;
33
34   public static final Color OVERVIEW_DEFAULT_LEGACY_GAP = Color.white;
35
36   public static final Color OVERVIEW_DEFAULT_HIDDEN = Color.darkGray
37           .darker();
38
39   final Color gapColour; // colour to use for gaps
40
41   final int gapColourInt;
42
43   final Color residueColour; // colour to use for uncoloured residues
44
45   final int residueColourInt;
46
47   final Color hiddenColour; // colour for hidden regions
48
49   boolean useLegacy = false;
50
51   /**
52    * Constructor without colour settings (used by applet)
53    */
54   public OverviewResColourFinder()
55   {
56     this(false, OVERVIEW_DEFAULT_GAP, OVERVIEW_DEFAULT_HIDDEN);
57   }
58
59   /**
60    * Constructor with colour settings
61    * 
62    * @param useLegacyColouring
63    *          whether to use legacy gap colouring (white gaps, grey residues)
64    * @param gapCol
65    *          gap colour if not legacy
66    * @param hiddenCol
67    *          hidden region colour (transparency applied by rendering code)
68    */
69   public OverviewResColourFinder(boolean useLegacyColouring, Color gapCol,
70           Color hiddenCol)
71   {
72     if (useLegacyColouring)
73     {
74       gapColour = Color.white;
75       residueColour = Color.lightGray;
76     }
77     else
78     {
79       gapColour = gapCol;
80       residueColour = Color.WHITE;
81     }
82     gapColourInt = gapColour.getRGB();
83     residueColourInt = residueColour.getRGB();
84     hiddenColour = hiddenCol;
85   }
86
87   @Override
88   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
89   {
90     return new Color(getBoxColourInt(shader, seq, i));
91   }
92
93   public int getBoxColourInt(ResidueShaderI shader, SequenceI seq, int i)
94   {
95     char currentChar = seq.getCharAt(i);
96     // In the overview window, gaps are coloured grey, unless the colour scheme
97     // specifies a gap colour, in which case gaps honour the colour scheme
98     // settings
99     boolean isGap = Comparison.isGap(currentChar);
100     if (shader.getColourScheme() == null)
101     {
102       return (isGap ? gapColourInt : residueColourInt);
103     }
104     return (isGap && !shader.getColourScheme().hasGapColour() ? gapColourInt
105             : shader.findColourInt(currentChar, i, seq));
106   }
107
108   @Override
109   public Color getResidueColour(boolean showBoxes, ResidueShaderI shader,
110           SequenceGroup[] allGroups, final SequenceI seq, int i,
111           FeatureColourFinder finder)
112   {
113     Color col = getResidueBoxColour(showBoxes, shader, allGroups, seq, i);
114
115     // if there's a FeatureColourFinder we might override the residue colour
116     // here with feature colouring
117     col = finder == null || finder.noFeaturesDisplayed() ? col
118             : finder.findFeatureColour(col, seq, i);
119     return col;
120   }
121
122
123   public int getResidueColourInt(boolean showBoxes, ResidueShaderI shader,
124           SequenceGroup[] allGroups, final SequenceI seq, int i,
125           FeatureColourFinder finder)
126   {
127     int col = getResidueBoxColourInt(showBoxes, shader, allGroups, seq, i);
128
129     // if there's a FeatureColourFinder we might override the residue colour
130     // here with feature colouring
131     col = finder == null || finder.noFeaturesDisplayed() ? col
132             : finder.findFeatureColourInt(col, seq, i);
133     return col;
134   }
135
136   @Override
137   protected Color getResidueBoxColour(boolean showBoxes,
138           ResidueShaderI shader, SequenceGroup[] allGroups, SequenceI seq,
139           int i)
140   {
141     return new Color(
142             getResidueBoxColourInt(showBoxes, shader, allGroups, seq, i));
143   }
144
145   /**
146    * In the overview, the showBoxes setting is ignored, as the overview displays
147    * the colours regardless.
148    */
149   protected int getResidueBoxColourInt(boolean showBoxes,
150           ResidueShaderI shader, SequenceGroup[] allGroups, SequenceI seq,
151           int i)
152   {
153     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
154             i);
155     ResidueShaderI currentShader = (currentSequenceGroup == null ? shader
156             : currentSequenceGroup.getGroupColourScheme());
157     return getBoxColourInt(currentShader, seq, i);
158   }
159
160   /**
161    * Supply hidden colour
162    * 
163    * @return colour of hidden regions
164    */
165   protected Color getHiddenColour()
166   {
167     return hiddenColour;
168   }
169 }