JAL-2588 Added tests
[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   public Color getResidueColour(ResidueShaderI shader,
36           SequenceGroup[] allGroups,
37           final SequenceI seq, int position, FeatureColourFinder finder)
38   {
39     Color col = getResidueBoxColour(shader, allGroups, seq, position);
40
41     if (finder != null)
42     {
43       col = finder.findFeatureColour(col, seq, position);
44     }
45     return col;
46   }
47
48   private Color getResidueBoxColour(ResidueShaderI shader,
49           SequenceGroup[] allGroups,
50           SequenceI seq, int i)
51   {
52
53     ResidueShaderI currentShader;
54
55     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
56             i);
57     if (currentSequenceGroup != null)
58     {
59       currentShader = currentSequenceGroup.getGroupColourScheme();
60     }
61     else
62     {
63       currentShader = shader;
64     }
65
66     return getBoxColour(currentShader, seq, i);
67   }
68
69   public SequenceGroup getCurrentSequenceGroup(SequenceGroup[] allGroups,
70           int res)
71   {
72     if (allGroups == null)
73     {
74       return null;
75     }
76
77     for (int i = 0; i < allGroups.length; i++)
78     {
79       if ((allGroups[i].getStartRes() <= res)
80               && (allGroups[i].getEndRes() >= res))
81       {
82         return (allGroups[i]);
83       }
84     }
85
86     return null;
87   }
88
89   /**
90    * DOCUMENT ME!
91    * 
92    * @param shader
93    *          DOCUMENT ME!
94    * @param seq
95    *          DOCUMENT ME!
96    * @param i
97    *          DOCUMENT ME!
98    */
99   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
100   {
101     Color resBoxColour = Color.white;
102     if (shader.getColourScheme() != null)
103     {
104       resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
105     }
106     return resBoxColour;
107   }
108
109 }