JAL-2349 colour shade computation refactored to annotation row renderer
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index 313e3db..35b73a4 100644 (file)
@@ -8,10 +8,10 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.ContactListI;
+import jalview.datamodel.ContactRange;
 import jalview.renderer.api.AnnotationRowRendererI;
 
 import java.awt.Color;
-import java.awt.Font;
 import java.awt.Graphics;
 
 /**
@@ -20,17 +20,7 @@ import java.awt.Graphics;
  */
 public class ContactMapRenderer implements AnnotationRowRendererI
 {
-  /*
-   * 
-   *     // TODO Auto-generated method stub
-      void drawProfileDensity(Graphics g, AlignmentAnnotation _aa,
-              Annotation[] aa_annotations, int sRes, int eRes, float min,
-              float max, int y)
-      {
 
-   * 
-   * 
-   */
   @Override
   public void renderRow(Graphics g, int charWidth, int charHeight,
           boolean hasHiddenColumns, AlignViewportI viewport,
@@ -42,7 +32,6 @@ public class ContactMapRenderer implements AnnotationRowRendererI
     {
       return;
     }
-    Font ofont = g.getFont();
     eRes = Math.min(eRes, aa_annotations.length);
 
     int x = 0, y2 = y;
@@ -75,32 +64,56 @@ public class ContactMapRenderer implements AnnotationRowRendererI
        * {profile type, #values, total count, char1, pct1, char2, pct2...}
        */
       ContactListI contacts = viewport.getContactList(_aa, column);
-
+      min = _aa.graphMin;
+      max = _aa.graphMax;
       if (contacts == null)
       {
         return;
       }
 
-
-      int scale = Math
-              .max(1, _aa.graphHeight / contacts.getContactHeight());
-      int step = _aa.graphHeight / scale;
-      int valuesProcessed = 0;
-      // profl[1] is the number of values in the profile
-      for (int stp = 0, ht = y2, eht = y2 + _aa.graphHeight; ht < eht; ht += scale, stp++)
+      // cell height to render
+      double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
+              : (((double) _aa.graphHeight) / (double) contacts
+                      .getContactHeight());
+      int cstart, cend = -1;
+      for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= scale)
       {
-        valuesProcessed = stp * step;
-        g.setColor(contacts.getColorForScore(stp * step));
+        cstart = cend + 1;
+        cend = -1
+                + (contacts.getContactHeight() * (ht - eht) / _aa.graphHeight);
+        // TODO show maximum colour for range - sort of done
+        // also need a 'getMaxPosForRange(start,end)'
+        g.setColor(getColorForRange(contacts, cstart, cend));
 
         if (scale > 1)
         {
-          g.fillRect(x * charWidth, ht, charWidth, scale);
-        } else {
+          g.fillRect(x * charWidth, ht, charWidth, 1 + (int) scale);
+        }
+        else
+        {
           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
         }
       }
+      x++;
     }
-    x++;
 
   }
+
+  Color minColor = Color.white, maxColor = Color.magenta;
+
+  float min, max;
+
+  Color shadeFor(float value)
+  {
+    return jalview.util.ColorUtils.getGraduatedColour(value, 0, minColor,
+            max, maxColor);
+  }
+
+  public Color getColorForRange(ContactListI cl, int i, int j)
+  {
+    ContactRange cr = cl.getRangeFor(i, j);
+    // average for moment - probably more interested in maxIntProj though
+    return shadeFor((float) cr.getMean());
+  }
+
 }