From 424648b2e57e45eaa21c006b44828d8fca418581 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 4 Mar 2019 16:22:40 +0000 Subject: [PATCH] JAL-3206 avoid divide by zero when threshold is graphMin / graphMax --- src/jalview/schemes/AnnotationColourGradient.java | 9 +++++--- .../schemes/AnnotationColourGradientTest.java | 24 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/jalview/schemes/AnnotationColourGradient.java b/src/jalview/schemes/AnnotationColourGradient.java index c28ea5f..c74fdbc 100755 --- a/src/jalview/schemes/AnnotationColourGradient.java +++ b/src/jalview/schemes/AnnotationColourGradient.java @@ -185,7 +185,7 @@ public class AnnotationColourGradient extends FollowerColourScheme } else { - seqannot = new IdentityHashMap(); + seqannot = new IdentityHashMap<>(); } // resolve the context containing all the annotation for the sequence AnnotatedCollectionI alcontext = alignment instanceof AlignmentI @@ -414,14 +414,17 @@ public class AnnotationColourGradient extends FollowerColourScheme && aboveAnnotationThreshold == ABOVE_THRESHOLD && value >= ann.threshold.value) { - range = (value - ann.threshold.value) + range = ann.graphMax == ann.threshold.value ? 1f + : (value - ann.threshold.value) / (ann.graphMax - ann.threshold.value); } else if (thresholdIsMinMax && ann.threshold != null && aboveAnnotationThreshold == BELOW_THRESHOLD && value <= ann.threshold.value) { - range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin); + range = ann.graphMin == ann.threshold.value ? 0f + : (value - ann.graphMin) + / (ann.threshold.value - ann.graphMin); } else { diff --git a/test/jalview/schemes/AnnotationColourGradientTest.java b/test/jalview/schemes/AnnotationColourGradientTest.java index b7a5164..be8b58d 100644 --- a/test/jalview/schemes/AnnotationColourGradientTest.java +++ b/test/jalview/schemes/AnnotationColourGradientTest.java @@ -124,6 +124,18 @@ public class AnnotationColourGradientTest Color result = testee.shadeCalculation(ann, col); assertEquals(result, expected, "for column " + col); } + + /* + * test for boundary case threshold == graphMax (JAL-3206) + */ + float thresh = ann.threshold.value; + ann.threshold.value = ann.graphMax; + Color result = testee.shadeCalculation(ann, WIDTH - 1); + assertEquals(result, maxColour); + testee.setThresholdIsMinMax(false); + result = testee.shadeCalculation(ann, WIDTH - 1); + assertEquals(result, maxColour); + ann.threshold.value = thresh; // reset } /** @@ -174,6 +186,18 @@ public class AnnotationColourGradientTest Color result = testee.shadeCalculation(ann, col); assertEquals(result, expected, "for column " + col); } + + /* + * test for boundary case threshold == graphMin (JAL-3206) + */ + float thresh = ann.threshold.value; + ann.threshold.value = ann.graphMin; + Color result = testee.shadeCalculation(ann, 0); + assertEquals(result, minColour); + testee.setThresholdIsMinMax(false); + result = testee.shadeCalculation(ann, 0); + assertEquals(result, minColour); + ann.threshold.value = thresh; // reset } /** -- 1.7.10.2