JAL-3383 JAL-3253-applet Allows for possibility of a sequence caching
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureColourFinder.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.seqfeatures;
22
23 import jalview.api.FeatureRenderer;
24 import jalview.api.FeaturesDisplayedI;
25 import jalview.datamodel.SequenceI;
26 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
27
28 import java.awt.Color;
29 import java.awt.Graphics;
30 import java.awt.image.BufferedImage;
31
32 /**
33  * A helper class to find feature colour using an associated FeatureRenderer
34  * 
35  * @author gmcarstairs
36  *
37  */
38 public class FeatureColourFinder
39 {
40   /*
41    * the class we delegate feature finding to
42    */
43   private FeatureRenderer featureRenderer;
44
45   /*
46    * a 1-pixel image on which features can be drawn, for the case where
47    * transparency allows 'see-through' of multiple feature colours
48    */
49   private BufferedImage offscreenImage;
50
51   private Graphics goff;
52
53   /**
54    * Constructor
55    * 
56    * @param fr
57    */
58   public FeatureColourFinder(FeatureRenderer fr)
59   {
60     featureRenderer = fr;
61     offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
62     goff = offscreenImage.getGraphics();
63   }
64
65   /**
66    * Answers the feature colour to show for the given sequence and column
67    * position. This delegates to the FeatureRenderer to find the colour, which
68    * will depend on feature location, visibility, ordering, colour scheme, and
69    * whether or not transparency is applied. For feature rendering with
70    * transparency, this class provides a dummy 'offscreen' graphics context
71    * where multiple feature colours can be overlaid and the combined colour read
72    * back.
73    * <p>
74    * This method is not thread-safe when transparency is applied, since a shared
75    * BufferedImage would be used by all threads to hold the composite colour at
76    * a position. Each thread should use a separate instance of this class.
77    * 
78    * @param defaultColour
79    * @param seq
80    * @param column
81    *          alignment column position (0..)
82    * @return
83    */
84   public Color findFeatureColour(Color defaultColour, SequenceI seq,
85           int column)
86   {
87     if (noFeaturesDisplayed())
88     {
89       return defaultColour;
90     }
91
92     Graphics g = null;
93
94     /*
95      * if transparency applies, provide a notional 1x1 graphics context 
96      * that has been primed with the default colour
97      */
98     if (featureRenderer.getTransparency() != 1f)
99     {
100       g = goff;
101       if (defaultColour != null)
102       {
103         offscreenImage.setRGB(0, 0, defaultColour.getRGB());
104       }
105     }
106
107     Color c = featureRenderer.findFeatureColour(seq, column + 1, g);
108     if (c == null)
109     {
110       return defaultColour;
111     }
112
113     if (g != null)
114     {
115       c = new Color(offscreenImage.getRGB(0, 0));
116     }
117     return c;
118   }
119
120   /**
121    * Answers true if feature display is turned off, or there are no features
122    * configured to be visible
123    * 
124    * @return
125    */
126   boolean noFeaturesDisplayed()
127   {
128     if (featureRenderer == null
129             || !featureRenderer.getViewport().isShowSequenceFeatures())
130     {
131       return true;
132     }
133
134     if (!((FeatureRendererModel) featureRenderer).hasRenderOrder())
135     {
136       return true;
137     }
138
139     FeaturesDisplayedI displayed = featureRenderer.getFeaturesDisplayed();
140     if (displayed == null || displayed.getVisibleFeatureCount() == 0)
141     {
142       return true;
143     }
144
145     return false;
146   }
147 }