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