JAL-2438 clarify code and javadoc, reinstate updateFeatures() check
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureColourFinder.java
1 package jalview.renderer.seqfeatures;
2
3 import jalview.api.FeatureRenderer;
4 import jalview.api.FeaturesDisplayedI;
5 import jalview.datamodel.SequenceI;
6 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
7
8 import java.awt.Color;
9 import java.awt.Graphics;
10 import java.awt.image.BufferedImage;
11
12 /**
13  * A helper class to find feature colour using an associated FeatureRenderer
14  * 
15  * @author gmcarstairs
16  *
17  */
18 public class FeatureColourFinder
19 {
20   /*
21    * the class we delegate feature finding to
22    */
23   private FeatureRenderer featureRenderer;
24
25   /*
26    * a 1-pixel image on which features can be drawn, for the case where
27    * transparency allows 'see-through' of multiple feature colours
28    */
29   private BufferedImage offscreenImage;
30
31   /**
32    * Constructor
33    * 
34    * @param fr
35    */
36   public FeatureColourFinder(FeatureRenderer fr)
37   {
38     featureRenderer = fr;
39     offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
40   }
41
42   /**
43    * Answers the feature colour to show for the given sequence and column
44    * position. This delegates to the FeatureRenderer to find the colour, which
45    * will depend on feature location, visibility, ordering, colour scheme, and
46    * whether or not transparency is applied. For feature rendering with
47    * transparency, this class provides a dummy 'offscreen' graphics context
48    * where multiple feature colours can be overlaid and the combined colour read
49    * back.
50    * 
51    * @param defaultColour
52    * @param seq
53    * @param column
54    *          alignment column position (base zero)
55    * @return
56    */
57   public Color findFeatureColour(Color defaultColour, SequenceI seq,
58           int column)
59   {
60     if (!((FeatureRendererModel) featureRenderer).hasRenderOrder())
61     {
62       return defaultColour;
63     }
64
65     FeaturesDisplayedI displayed = featureRenderer.getFeaturesDisplayed();
66     if (displayed == null || displayed.getVisibleFeatureCount() == 0)
67     {
68       return defaultColour; // nothing to see here folks
69     }
70
71     Graphics g = null;
72
73     /*
74      * if transparency applies, provide a notional 1x1 graphics context 
75      * that has been primed with the default colour
76      */
77     if (featureRenderer.getTransparency() != 1f)
78     {
79       g = offscreenImage.getGraphics();
80       if (defaultColour != null)
81       {
82         offscreenImage.setRGB(0, 0, defaultColour.getRGB());
83       }
84     }
85
86     Color c = featureRenderer.findFeatureColour(seq, column, g);
87     if (c == null)
88     {
89       return defaultColour;
90     }
91
92     if (g != null)
93     {
94       c = new Color(offscreenImage.getRGB(0, 0));
95     }
96     return c;
97   }
98 }