From 04a1f49daf769f0cb827dbf8f4872e65d65cd1cb Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 27 Oct 2016 10:45:06 +0100 Subject: [PATCH] JAL-98 refactor bleachColour() to ColorUtils --- src/jalview/schemes/ResidueColourScheme.java | 29 +++------------ src/jalview/util/ColorUtils.java | 49 ++++++++++++++++++++++++++ test/jalview/util/ColorUtilsTest.java | 24 +++++++++++++ 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/jalview/schemes/ResidueColourScheme.java b/src/jalview/schemes/ResidueColourScheme.java index 56c573c..a15ca20 100755 --- a/src/jalview/schemes/ResidueColourScheme.java +++ b/src/jalview/schemes/ResidueColourScheme.java @@ -25,6 +25,7 @@ import jalview.analysis.Profile; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceI; +import jalview.util.ColorUtils; import jalview.util.Comparison; import jalview.util.MessageManager; @@ -311,33 +312,13 @@ public class ResidueColourScheme implements ColourSchemeI float bleachFactor = (11 - (conservationScore - '0')) / 10f; /* - * scale this by the percentage slider / 20 - */ - bleachFactor *= (inc / 20f); - - int red = currentColour.getRed(); - int green = currentColour.getGreen(); - int blue = currentColour.getBlue(); - - /* - * bleach colours towards white (255, 255, 255), - * depending on the consensus score and the conservation slider value - * scores of: 0 1 2 3 4 5 6 7 8 9 + * scale this up by 0-5 (percentage slider / 20) + * as a result, scores of: 0 1 2 3 4 5 6 7 8 9 * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100% */ - red += (255 - red) * bleachFactor; - green += (255 - green) * bleachFactor; - blue += (255 - blue) * bleachFactor; + bleachFactor *= (inc / 20f); - if (red > 255 || green > 255 || blue > 255) - { - currentColour = Color.white; - } - else - { - currentColour = new Color(red, green, blue); - } - return currentColour; + return ColorUtils.bleachColour(currentColour, bleachFactor); } @Override diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index 31d1ded..525bfdb 100644 --- a/src/jalview/util/ColorUtils.java +++ b/src/jalview/util/ColorUtils.java @@ -142,4 +142,53 @@ public class ColorUtils * (maxColour.getBlue() - minColour.getBlue()); return new Color(r / 255, g / 255, b / 255); } + + /** + * 'Fades' the given colour towards white by the specified proportion. A + * factor of 1 or more results in White, a factor of 0 leaves the colour + * unchanged, and a factor between 0 and 1 results in a proportionate change + * of RGB values towards (255, 255, 255). + *

+ * A negative bleachFactor can be specified to darken the colour towards Black + * (0, 0, 0). + * + * @param colour + * @param bleachFactor + * @return + */ + public static Color bleachColour(Color colour, float bleachFactor) + { + if (bleachFactor >= 1f) + { + return Color.WHITE; + } + if (bleachFactor <= -1f) + { + return Color.BLACK; + } + if (bleachFactor == 0f) + { + return colour; + } + + int red = colour.getRed(); + int green = colour.getGreen(); + int blue = colour.getBlue(); + + if (bleachFactor > 0) + { + red += (255 - red) * bleachFactor; + green += (255 - green) * bleachFactor; + blue += (255 - blue) * bleachFactor; + return new Color(red, green, blue); + } + else + { + float factor = 1 + bleachFactor; + red *= factor; + green *= factor; + blue *= factor; + return new Color(red, green, blue); + } + } } diff --git a/test/jalview/util/ColorUtilsTest.java b/test/jalview/util/ColorUtilsTest.java index 69675f7..77a023f 100644 --- a/test/jalview/util/ColorUtilsTest.java +++ b/test/jalview/util/ColorUtilsTest.java @@ -22,6 +22,7 @@ package jalview.util; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertSame; import java.awt.Color; @@ -132,4 +133,27 @@ public class ColorUtilsTest .getGraduatedColour(40f, 10f, minColour, 10f, maxColour); assertEquals(minColour, col); } + + @Test(groups = { "Functional" }) + public void testBleachColour() + { + Color colour = new Color(155, 105, 55); + assertSame(colour, ColorUtils.bleachColour(colour, 0)); + assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 1)); + assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 2)); + assertEquals(new Color(175, 135, 95), + ColorUtils.bleachColour(colour, 0.2f)); + assertEquals(new Color(225, 210, 195), + ColorUtils.bleachColour(colour, 0.7f)); + + /* + * and some 'negative fade' + */ + assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -1)); + assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -2)); + assertEquals(new Color(124, 84, 44), + ColorUtils.bleachColour(colour, -0.2f)); + assertEquals(new Color(46, 31, 16), // with rounding down + ColorUtils.bleachColour(colour, -0.7f)); + } } -- 1.7.10.2