JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / renderer / ResidueColourFinder.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
27 import java.awt.Color;
28
29 public class ResidueColourFinder
30 {
31   public ResidueColourFinder()
32   {
33   }
34
35   /**
36    * Get the colour of a residue in a sequence
37    * 
38    * @param showBoxes
39    *          true if the viewport's Show Boxes setting is true
40    * @param shader
41    *          the viewport's colour scheme
42    * @param allGroups
43    *          all the groups which seq participates in
44    * @param seq
45    *          the sequence containing the residue
46    * @param position
47    *          the position of the residue in the sequence
48    * @param finder
49    *          FeatureColourFinder for the viewport
50    * @return colour of the residue
51    */
52   public Color getResidueColour(boolean showBoxes, ResidueShaderI shader,
53           SequenceGroup[] allGroups, final SequenceI seq, int position,
54           FeatureColourFinder finder)
55   {
56     Color col = getResidueBoxColour(showBoxes, shader, allGroups, seq,
57             position);
58
59     // if there's a FeatureColourFinder we might override the residue colour
60     // here with feature colouring
61     if (finder != null)
62     {
63       col = finder.findFeatureColour(col, seq, position);
64     }
65     return col;
66   }
67
68   /**
69    * Get the residue colour without accounting for any features
70    * 
71    * @param showBoxes
72    *          true if the viewport's Show Boxes setting is true
73    * @param shader
74    *          the viewport's colour scheme
75    * @param allGroups
76    *          all the groups which seq participates in
77    * @param seq
78    *          the sequence containing the residue
79    * @param i
80    *          the position of the residue in the sequence
81    * @return
82    */
83   protected Color getResidueBoxColour(boolean showBoxes,
84           ResidueShaderI shader, SequenceGroup[] allGroups, SequenceI seq,
85           int i)
86   {
87     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
88             i);
89     if (currentSequenceGroup != null)
90     {
91       if (currentSequenceGroup.getDisplayBoxes())
92       {
93         return getBoxColour(currentSequenceGroup.getGroupColourScheme(),
94                 seq, i);
95       }
96     }
97     else if (showBoxes)
98     {
99       return getBoxColour(shader, seq, i);
100     }
101
102     return Color.white;
103   }
104
105   /**
106    * Search all the groups for a sequence to find the one which a given res
107    * falls into
108    * 
109    * @param allGroups
110    *          all the groups a sequence participates in
111    * @param res
112    *          the residue to search for
113    * @return a sequence group for res, or null if no sequence group applies
114    */
115   public SequenceGroup getCurrentSequenceGroup(SequenceGroup[] allGroups,
116           int res)
117   {
118     if (allGroups == null)
119     {
120       return null;
121     }
122
123     for (int i = 0; i < allGroups.length; i++)
124     {
125       if ((allGroups[i].getStartRes() <= res)
126               && (allGroups[i].getEndRes() >= res))
127       {
128         return (allGroups[i]);
129       }
130     }
131
132     return null;
133   }
134
135   /**
136    * DOCUMENT ME!
137    * 
138    * @param shader
139    *          the viewport's colour scheme
140    * @param seq
141    *          the sequence containing the residue
142    * @param i
143    *          the position of the residue in the sequence
144    */
145   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
146   {
147     Color resBoxColour = Color.white;
148     if (shader.getColourScheme() != null)
149     {
150       resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
151     }
152     return resBoxColour;
153   }
154
155 }