JAL-2965 graduated sequence point colour from back to front
[jalview.git] / src / jalview / gui / RotatableCanvas.java
index 617180f..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();
 
@@ -427,46 +433,26 @@ public class RotatableCanvas extends JPanel implements MouseListener,
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
 
-    int halfwidth = getWidth() / 2;
-    int halfheight = getHeight() / 2;
-
     for (int i = 0; i < npoint; i++)
     {
+      /*
+       * sequence point colour as sequence id, but
+       * gray if sequence is currently selected
+       */
       SequencePoint sp = points.elementAt(i);
-      int x = (int) ((sp.coord.x - centre.x) * scale) + halfwidth;
-      int y = (int) ((sp.coord.y - centre.y) * scale)
-              + halfheight;
-      float z = sp.coord.y - centre.z; // todo sp.coord.z JAL-2963
-
-      SequenceI sequence = sp.getSequence();
-      if (av.getSequenceColour(sequence) == Color.black)
-      {
-        g.setColor(Color.white);
-      }
-      else
-      {
-        g.setColor(av.getSequenceColour(sequence));
-      }
-
-      if (av.getSelectionGroup() != null)
-      {
-        if (av.getSelectionGroup().getSequences(null)
-                .contains(sequence))
-        {
-          g.setColor(Color.gray);
-        }
-      }
-
-      if (z < 0)
-      {
-        g.setColor(g.getColor().darker());
-      }
+      Color sequenceColour = getSequencePointColour(sp);
+      g.setColor(sequenceColour);
 
+      int halfwidth = getWidth() / 2;
+      int halfheight = getHeight() / 2;
+      int x = (int) ((sp.coord.x - centre.x) * scale) + halfwidth;
+      int y = (int) ((sp.coord.y - centre.y) * scale) + halfheight;
       g.fillRect(x - 3, y - 3, 6, 6);
+
       if (showLabels)
       {
         g.setColor(Color.red);
-        g.drawString(sequence.getName(), x - 3, y - 4);
+        g.drawString(sp.getSequence().getName(), x - 3, y - 4);
       }
     }
 
@@ -478,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)
   {
@@ -693,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++)
     {
@@ -727,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;