JAL-2965 graduated sequence point colour from back to front
[jalview.git] / src / jalview / gui / RotatableCanvas.java
index 6fabd95..615b403 100755 (executable)
@@ -27,6 +27,7 @@ import jalview.datamodel.SequenceI;
 import jalview.datamodel.SequencePoint;
 import jalview.math.RotatableMatrix;
 import jalview.math.RotatableMatrix.Axis;
+import jalview.util.ColorUtils;
 import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 
@@ -160,9 +161,14 @@ public class RotatableCanvas extends JPanel implements MouseListener,
 
   }
 
-  public void showLabels(boolean b)
+  /**
+   * Refreshes the display with labels shown (or not)
+   * 
+   * @param show
+   */
+  public void showLabels(boolean show)
   {
-    showLabels = b;
+    showLabels = show;
     repaint();
   }
 
@@ -204,7 +210,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
    * Resets axes to the initial state: x-axis to the right, y-axis up, z-axis to
    * back (so obscured in a 2-D display)
    */
-  public void resetAxes()
+  protected void resetAxes()
   {
     axisEndPoints[0] = new Point(1f, 0f, 0f);
     axisEndPoints[1] = new Point(0f, 1f, 0f);
@@ -216,7 +222,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
    * sequence point, and also the min-max range (width) for each dimension, and
    * the maximum width for all dimensions
    */
-  public void findWidth()
+  protected void findWidth()
   {
     max = new float[DIMS];
     min = new float[DIMS];
@@ -251,7 +257,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
    * 
    * @return DOCUMENT ME!
    */
-  public float findScale()
+  protected float findScale()
   {
     int dim;
     int w;
@@ -283,7 +289,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
   /**
    * Computes and saves the position of the centre of the view
    */
-  public void findCentre()
+  protected void findCentre()
   {
     findWidth();
 
@@ -434,26 +440,8 @@ public class RotatableCanvas extends JPanel implements MouseListener,
        * gray if sequence is currently selected
        */
       SequencePoint sp = points.elementAt(i);
-      SequenceI sequence = sp.getSequence();
-      Color sequenceColour = av.getSequenceColour(sequence);
-      g.setColor(
-              sequenceColour == Color.black ? Color.white : sequenceColour);
-      if (av.getSelectionGroup() != null)
-      {
-        if (av.getSelectionGroup().getSequences(null)
-                .contains(sequence))
-        {
-          g.setColor(Color.gray);
-        }
-      }
-
-      /*
-       * dim sequence points 'at the back'
-       */
-      if (sp.coord.z < centre.z)
-      {
-        g.setColor(g.getColor().darker());
-      }
+      Color sequenceColour = getSequencePointColour(sp);
+      g.setColor(sequenceColour);
 
       int halfwidth = getWidth() / 2;
       int halfheight = getHeight() / 2;
@@ -464,7 +452,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
       if (showLabels)
       {
         g.setColor(Color.red);
-        g.drawString(sequence.getName(), x - 3, y - 4);
+        g.drawString(sp.getSequence().getName(), x - 3, y - 4);
       }
     }
 
@@ -476,6 +464,40 @@ public class RotatableCanvas extends JPanel implements MouseListener,
     // }
   }
 
+  /**
+   * Determines the colour to use when drawing a sequence point. The colour is
+   * taken from the sequence id, with black converted to white, and then
+   * graduated from darker (at the back) to brighter (at the front) based on the
+   * z-axis coordinate of the point.
+   * 
+   * @param sp
+   * @return
+   */
+  protected Color getSequencePointColour(SequencePoint sp)
+  {
+    SequenceI sequence = sp.getSequence();
+    Color sequenceColour = av.getSequenceColour(sequence);
+    if (sequenceColour == Color.black)
+    {
+      sequenceColour = Color.white;
+    }
+    if (av.getSelectionGroup() != null)
+    {
+      if (av.getSelectionGroup().getSequences(null).contains(sequence))
+      {
+        sequenceColour = Color.gray;
+      }
+    }
+
+    /*
+     * graduate from front (brighter) to back (darker)
+     */
+    sequenceColour = ColorUtils.getGraduatedColour(sp.coord.z, min[2],
+            max[2], sequenceColour);
+
+    return sequenceColour;
+  }
+
   @Override
   public void keyTyped(KeyEvent evt)
   {
@@ -691,7 +713,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
    * @param x2
    * @param y2
    */
-  public void rectSelect(int x1, int y1, int x2, int y2)
+  protected void rectSelect(int x1, int y1, int x2, int y2)
   {
     for (int i = 0; i < npoint; i++)
     {
@@ -725,7 +747,7 @@ public class RotatableCanvas extends JPanel implements MouseListener,
    * 
    * @return
    */
-  public SequenceI findSequenceAtPoint(int x, int y)
+  protected SequenceI findSequenceAtPoint(int x, int y)
   {
     int halfwidth = getWidth() / 2;
     int halfheight = getHeight() / 2;