+package jalview.renderer.seqfeatures;
+
+import jalview.api.FeaturesDisplayedI;
+import jalview.datamodel.SequenceI;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+
+/**
+ * A helper class to find feature colour using an associated FeatureRenderer
+ *
+ * @author gmcarstairs
+ *
+ */
+public class FeatureColourFinder
+{
+ /*
+ * the class we delegate feature finding to
+ */
+ private jalview.api.FeatureRenderer featureRenderer;
+
+ /*
+ * a 1-pixel image on which features can be drawn, for the case where
+ * transparency allows 'see-through' of multiple feature colours
+ */
+ private BufferedImage offscreenImage;
+
+ /**
+ * Constructor
+ *
+ * @param fr
+ */
+ public FeatureColourFinder(jalview.api.FeatureRenderer fr)
+ {
+ featureRenderer = fr;
+ offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
+ }
+
+ /**
+ * Answers the feature colour to show for the given sequence and column
+ * position. This delegates to the FeatureRenderer to find the colour, which
+ * will depend on feature location, visibility, ordering, and whether or not
+ * transparency is applied. For feature rendering with transparency, this
+ * class provides a dummy 'offscreen' graphics context where multiple feature
+ * colours can be overlaid and the combined colour read back.
+ *
+ * @param defaultColour
+ * @param seq
+ * @param column
+ * @return
+ */
+ public Color findFeatureColour(Color defaultColour, SequenceI seq,
+ int column)
+ {
+ if (!((FeatureRendererModel) featureRenderer).hasRenderOrder())
+ {
+ return defaultColour;
+ }
+
+ FeaturesDisplayedI displayed = featureRenderer.getFeaturesDisplayed();
+ if (displayed == null || displayed.getVisibleFeatureCount() == 0)
+ {
+ return defaultColour; // nothing to see here folks
+ }
+
+ Graphics g = featureRenderer.getTransparency() == 1f ? null
+ : offscreenImage.getGraphics();
+
+ Color c = featureRenderer.findFeatureColour(defaultColour, seq, column,
+ g);
+ if (c != null && g != null)
+ {
+ c = new Color(offscreenImage.getRGB(0, 0));
+ }
+ return c == null ? defaultColour : c;
+ }
+}