JAL-2349 JAL-3855 off by one and daft return
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index f5c4c31..58e6ab1 100644 (file)
@@ -67,60 +67,41 @@ public class ContactMapRenderer implements AnnotationRowRendererI
         x++;
         continue;
       }
-      if (_aa.sequenceRef != null)
-      {
-        // get the sequence position for the column
-        column = _aa.sequenceRef.findPosition(column) - 1;
-      }
       ContactListI contacts = viewport.getContactList(_aa, column);
       if (contacts == null)
       {
-        return;
-      }
-      int contact_height = contacts.getContactHeight();
-      // fractional number of contacts covering each pixel
-      double contacts_per_pixel = ((double) contact_height)
-              / ((double) _aa.graphHeight);
-
-      int pixels_step;
-
-      if (contacts_per_pixel >= 1)
-      {
-        // many contacts rendered per pixel
-        pixels_step = 1;
-      }
-      else
-      {
-        // pixel height for each contact
-        pixels_step = (int) Math
-                .ceil(((double) _aa.graphHeight) / (double) contact_height);
+        x++;
+        continue;
       }
+      // Bean holding mapping from contact list to pixels
+      final ContactGeometry cgeom = new ContactGeometry(contacts,
+              _aa.graphHeight);
 
-      int cstart = 0, cend;
-
-      for (int ht = y2,
-              eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
+      for (int ht = y2, eht = y2
+              - _aa.graphHeight; ht >= eht; ht -= cgeom.pixels_step)
       {
-        cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
-        cend = (int) Math.min(contact_height,
-                Math.ceil(cstart + contacts_per_pixel * pixels_step));
+        ContactGeometry.contactInterval ci = cgeom.mapFor(y2 - ht,
+                y2 - ht + cgeom.pixels_step);
+        // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
+        // cend = (int) Math.min(contact_height,
+        // Math.ceil(cstart + contacts_per_pixel * pixels_step));
 
         // TODO show maximum colour for range - sort of done
         // also need a 'getMaxPosForRange(start,end)' to accurately render
         Color col;
         boolean rowsel = false;
-        if (!colsel && columnSelection != null)
+        if (columnSelection != null)
         {
           if (_aa.sequenceRef == null)
           {
-            rowsel = columnSelection.intersects(cstart, cend);
+            rowsel = columnSelection.intersects(ci.cStart, ci.cEnd);
           }
           else
           {
             // TODO check we have correctly mapped cstart to local sequence
             // numbering
-            int s = _aa.sequenceRef.findIndex(cstart);
-            int e = _aa.sequenceRef.findIndex(cend);
+            int s = _aa.sequenceRef.findIndex(ci.cStart);
+            int e = _aa.sequenceRef.findIndex(ci.cEnd);
             if (s > 0 && s < _aa.sequenceRef.getLength())
             {
               rowsel = columnSelection.intersects(s, e);
@@ -131,17 +112,26 @@ public class ContactMapRenderer implements AnnotationRowRendererI
         if (colsel || rowsel)
         {
 
-          col = getSelectedColorForRange(min, max, contacts, cstart, cend);
+          col = getSelectedColorForRange(min, max, contacts, ci.cStart,
+                  ci.cEnd);
+          if (colsel && rowsel)
+          {
+            col = new Color(col.getBlue(), col.getGreen(), col.getRed());
+          }
+          else
+          {
+            col = new Color(col.getBlue(), col.getBlue(), col.getBlue());
+          }
           g.setColor(col);
         }
         else
         {
-          col = getColorForRange(min, max, contacts, cstart, cend);
+          col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
           g.setColor(col);
         }
-        if (pixels_step > 1)
+        if (cgeom.pixels_step > 1)
         {
-          g.fillRect(x * charWidth, ht, charWidth, 1 + pixels_step);
+          g.fillRect(x * charWidth, ht, charWidth, 1 + cgeom.pixels_step);
         }
         else
         {
@@ -154,8 +144,11 @@ public class ContactMapRenderer implements AnnotationRowRendererI
   }
 
   // Shading parameters
-  Color minColor = Color.white, maxColor = Color.green.darker().darker(),
-          selMaxColor = Color.magenta.darker();
+  // currently hardwired for alphafold
+  Color maxColor = new Color(246, 252, 243),
+          minColor = new Color(0, 60, 26),
+          selMinColor = new Color(26, 0, 60),
+          selMaxColor = new Color(243, 246, 252);
 
   Color shadeFor(float min, float max, float value)
   {
@@ -177,7 +170,7 @@ public class ContactMapRenderer implements AnnotationRowRendererI
     ContactRange cr = cl.getRangeFor(i, j);
     // average for moment - probably more interested in maxIntProj though
     return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMean(),
-            0, minColor, max, selMaxColor);
+            0, selMinColor, max, selMaxColor);
   }
 
 }