JAL-2094 first pass with jalview.api.ColorI interface
[jalview.git] / src / jalview / gui / SequenceRenderer.java
index 70acb7b..f215f9d 100755 (executable)
  */
 package jalview.gui;
 
+import jalview.api.ColorI;
 import jalview.api.FeatureRenderer;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.schemes.Colour;
 import jalview.schemes.ColourSchemeI;
+import jalview.util.ColorUtils;
 
 import java.awt.Color;
 import java.awt.FontMetrics;
@@ -89,7 +92,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
   }
 
   @Override
-  public Color getResidueBoxColour(SequenceI seq, int i)
+  public ColorI getResidueBoxColour(SequenceI seq, int i)
   {
     allGroups = av.getAlignment().findAllGroups(seq);
 
@@ -105,7 +108,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
       getBoxColour(av.getGlobalColourScheme(), seq, i);
     }
 
-    return resBoxColour;
+    return new Colour(resBoxColour);
   }
 
   /**
@@ -119,12 +122,12 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
    * @return
    */
   @Override
-  public Color getResidueColour(final SequenceI seq, int position,
+  public ColorI getResidueColour(final SequenceI seq, int position,
           FeatureRenderer fr)
   {
     // TODO replace 8 or so code duplications with calls to this method
     // (refactored as needed)
-    Color col = getResidueBoxColour(seq, position);
+    ColorI col = getResidueBoxColour(seq, position);
 
     if (fr != null)
     {
@@ -217,7 +220,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
           int y1)
   {
     if (seq == null)
-     {
+    {
       return; // fix for racecondition
     }
     int i = start;
@@ -303,7 +306,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
     {
       end = seq.getLength() - 1;
     }
-    graphics.setColor(av.getTextColour());
+    graphics.setColor(ColorUtils.getColor(av.getTextColour()));
 
     if (monospacedFont && av.getShowText() && allGroups.length == 0
             && !av.getColourText() && av.getThresholdTextColour() == 0)
@@ -323,11 +326,17 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
     {
       boolean srep = av.isDisplayReferenceSeq();
       boolean getboxColour = false;
+      boolean isarep = av.getAlignment().getSeqrep() == seq;
+      boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
+              .getSeqrep() == seq : false;
+      char sr_c;
       for (int i = start; i <= end; i++)
       {
-        graphics.setColor(av.getTextColour());
+
+        graphics.setColor(ColorUtils.getColor(av.getTextColour()));
         getboxColour = false;
         s = seq.getCharAt(i);
+
         if (!renderGaps && jalview.util.Comparison.isGap(s))
         {
           continue;
@@ -356,19 +365,20 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
               if (resBoxColour.getRed() + resBoxColour.getBlue()
                       + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
               {
-                graphics.setColor(currentSequenceGroup.textColour2);
+                graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour2));
               }
             }
           }
           else
           {
-            graphics.setColor(currentSequenceGroup.textColour);
+            graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour));
           }
-          if (currentSequenceGroup.getShowNonconserved()) // todo optimize
+          if (!isarep && !isgrep
+                  && currentSequenceGroup.getShowNonconserved()) // todo
+                                                                 // optimize
           {
             // todo - use sequence group consensus
-            s = getDisplayChar(srep, i, s,
-                    '.');
+            s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
 
           }
 
@@ -405,13 +415,12 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
             if (resBoxColour.getRed() + resBoxColour.getBlue()
                     + resBoxColour.getGreen() < av.getThresholdTextColour())
             {
-              graphics.setColor(av.getTextColour2());
+              graphics.setColor(ColorUtils.getColor(av.getTextColour2()));
             }
           }
-          if (av.getShowUnconserved())
+          if (!isarep && av.getShowUnconserved())
           {
-            s = getDisplayChar(srep, i, s,
-                    '.');
+            s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
 
           }
 
@@ -419,8 +428,7 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
 
         charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
         graphics.drawString(String.valueOf(s),
-                charOffset + av.getCharWidth()
-                * (i - start), y1);
+                charOffset + av.getCharWidth() * (i - start), y1);
 
       }
     }
@@ -438,13 +446,21 @@ public class SequenceRenderer implements jalview.api.SequenceRenderer
    * @return
    */
   private char getDisplayChar(final boolean usesrep, int position,
-          char sequenceChar, char conservedChar)
+          char sequenceChar, char conservedChar, SequenceGroup currentGroup)
   {
     // TODO - use currentSequenceGroup rather than alignment
     // currentSequenceGroup.getConsensus()
-    char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
-            .charAt(0);
-    if (conschar != '-'
+    char conschar = (usesrep) ? (currentGroup == null ? av.getAlignment()
+            .getSeqrep().getCharAt(position)
+            : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
+                    .getCharAt(position) : av.getAlignment().getSeqrep()
+                    .getCharAt(position)))
+            : (currentGroup != null && currentGroup.getConsensus() != null) ? currentGroup
+                    .getConsensus().annotations[position].displayCharacter
+                    .charAt(0)
+                    : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
+                            .charAt(0);
+    if (!jalview.util.Comparison.isGap(conschar)
             && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
     {
       sequenceChar = conservedChar;