From: gmungoc Date: Fri, 22 Apr 2016 14:20:00 +0000 (+0100) Subject: JAL-2068 convenience helper method for graduated score colour X-Git-Tag: Release_2_10_0~249^2~16 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=2f5b2fa149a0bdedff23293ce7c0522b3a3c77ec;p=jalview.git JAL-2068 convenience helper method for graduated score colour --- diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index b8cd563..042654d 100644 --- a/src/jalview/util/ColorUtils.java +++ b/src/jalview/util/ColorUtils.java @@ -103,4 +103,39 @@ public class ColorUtils return col == null ? null : col.brighter().brighter().brighter(); } + /** + * Returns a color between minColour and maxColour; the RGB values are in + * proportion to where 'value' lies between minValue and maxValue + * + * @param value + * @param minValue + * @param minColour + * @param maxValue + * @param maxColour + * @return + */ + public static Color getGraduatedColour(float value, float minValue, + Color minColour, float maxValue, Color maxColour) + { + if (value < minValue) + { + value = minValue; + } + if (value > maxValue) + { + value = maxValue; + } + + /* + * prop = proportion of the way value is from minValue to maxValue + */ + float prop = (value - minValue) / (maxValue - minValue); + float r = minColour.getRed() + prop + * (maxColour.getRed() - minColour.getRed()); + float g = minColour.getGreen() + prop + * (maxColour.getGreen() - minColour.getGreen()); + float b = minColour.getBlue() + prop + * (maxColour.getBlue() - minColour.getBlue()); + return new Color(r / 255, g / 255, b / 255); + } } diff --git a/test/jalview/util/ColorUtilsTest.java b/test/jalview/util/ColorUtilsTest.java index 2b12a72..ca1aca0 100644 --- a/test/jalview/util/ColorUtilsTest.java +++ b/test/jalview/util/ColorUtilsTest.java @@ -72,4 +72,58 @@ public class ColorUtilsTest assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime } + + @Test(groups = { "Functional" }) + public void testGetGraduatedColour() + { + Color minColour = new Color(100, 100, 100); + Color maxColour = new Color(180, 200, 220); + + /* + * value half-way between min and max + */ + Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f, + maxColour); + assertEquals(140, col.getRed()); + assertEquals(150, col.getGreen()); + assertEquals(160, col.getBlue()); + + /* + * value two-thirds of the way between min and max + */ + col = ColorUtils + .getGraduatedColour(30f, 10f, minColour, 40f, maxColour); + assertEquals(153, col.getRed()); + // Color constructor rounds float value to nearest int + assertEquals(167, col.getGreen()); + assertEquals(180, col.getBlue()); + + /* + * value = min + */ + col = ColorUtils + .getGraduatedColour(10f, 10f, minColour, 30f, maxColour); + assertEquals(minColour, col); + + /* + * value = max + */ + col = ColorUtils + .getGraduatedColour(30f, 10f, minColour, 30f, maxColour); + assertEquals(maxColour, col); + + /* + * value < min + */ + col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour); + assertEquals(minColour, col); + + /* + * value > max + */ + col = ColorUtils + .getGraduatedColour(40f, 10f, minColour, 30f, + maxColour); + assertEquals(maxColour, col); + } }