From f0fd407e5fad67185a9813c57bfc50aacaf1de6e Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 17 Oct 2019 09:59:32 +0100 Subject: [PATCH] JAL-3368 removed parsing web colours (now on a separate branch) --- src/jalview/util/ColorUtils.java | 70 +++++++++++++++++++++++++------- src/jalview/util/Platform.java | 51 ----------------------- src/jalview/util/StringUtils.java | 28 ++++++++++++- test/jalview/util/ColorUtilsTest.java | 24 +++++------ test/jalview/util/StringUtilsTest.java | 20 ++++++++- 5 files changed, 112 insertions(+), 81 deletions(-) diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index 3eb080b..d465632 100644 --- a/src/jalview/util/ColorUtils.java +++ b/src/jalview/util/ColorUtils.java @@ -29,9 +29,13 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; +/** + * A class with utility methods for manipulating AWT colours/colors + */ public class ColorUtils { private static final int MAX_CACHE_SIZE = 1729; + /* * a cache for colours generated from text strings */ @@ -203,8 +207,8 @@ public class ColorUtils * Parses a string into a Color, where the accepted formats are * * * @param colour @@ -219,18 +223,20 @@ public class ColorUtils colour = colour.trim(); Color col = null; - try - { - int value = Integer.parseInt(colour, 16); - col = new Color(value); - } catch (NumberFormatException ex) + if (colour.length() == 6 && StringUtils.isHexString(colour)) { - col = Platform.getColorFromName(colour); + try + { + int value = Integer.parseInt(colour, 16); + col = new Color(value); + } catch (NumberFormatException ex) + { + } } if (col == null) { - col = ColorUtils.getAWTColorFromName(colour); + col = ColorUtils.getColorFromName(colour); } if (col == null) @@ -245,7 +251,7 @@ public class ColorUtils int b = Integer.parseInt(tokens[2].trim()); col = new Color(r, g, b); } - } catch (Exception ex) + } catch (IllegalArgumentException ex) { // non-numeric token or out of 0-255 range } @@ -313,15 +319,49 @@ public class ColorUtils /** * Returns the Color constant for a given colour name e.g. "pink", or null if - * the name is not recognised + * the name is not recognised. Currently recognises only AWT colour names, but + * could be extended to support others e.g. standard html colour names. * * @param name * @return */ - public static Color getAWTColorFromName(String name) + public static Color getColorFromName(String name) { - return Platform.getColorFromName(name); // BH 2019 -- allows for wide range - // of JavaScript colors (for - // JavaScript only) + if (name == null) + { + return null; + } + // or make a static map; or use reflection on the field name + switch (name.toLowerCase()) + { + case "black": + return Color.black; + case "blue": + return Color.blue; + case "cyan": + return Color.cyan; + case "darkgray": + return Color.darkGray; + case "gray": + return Color.gray; + case "green": + return Color.green; + case "lightgray": + return Color.lightGray; + case "magenta": + return Color.magenta; + case "orange": + return Color.orange; + case "pink": + return Color.pink; + case "red": + return Color.red; + case "white": + return Color.white; + case "yellow": + return Color.yellow; + default: + return null; + } } } diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 11f7988..121ac1b 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -720,57 +720,6 @@ public class Platform } /** - * @param c - */ - public static Color getColorFromName(String name) - { - if (name == null) - { - return null; - } - /** - * @j2sNative - * - * return swingjs.JSUtil.getColorFromName$S(name); - */ - { - // or make a static map; or use reflection on the field name - switch (name.toLowerCase()) - { - case "black": - return Color.black; - case "blue": - return Color.blue; - case "cyan": - return Color.cyan; - case "darkgray": - return Color.darkGray; - case "gray": - return Color.gray; - case "green": - return Color.green; - case "lightgray": - return Color.lightGray; - case "magenta": - return Color.magenta; - case "orange": - return Color.orange; - case "pink": - return Color.pink; - case "red": - return Color.red; - case "white": - return Color.white; - case "yellow": - return Color.yellow; - default: - return null; - } - - } - } - - /** * Initialize Java debug logging. A representative sample -- adapt as desired. */ public static void startJavaLogging() diff --git a/src/jalview/util/StringUtils.java b/src/jalview/util/StringUtils.java index 2e8ace8..2cbbfbf 100644 --- a/src/jalview/util/StringUtils.java +++ b/src/jalview/util/StringUtils.java @@ -146,7 +146,7 @@ public class StringUtils { return null; } - List jv = new ArrayList(); + List jv = new ArrayList<>(); int cp = 0, pos, escape; boolean wasescaped = false, wasquoted = false; String lstitem = null; @@ -444,4 +444,30 @@ public class StringUtils } return text; } + + /** + * Answers true if the string is not empty and consists only of digits, or + * characters 'a'-'f' or 'A'-'F', else false + * + * @param s + * @return + */ + public static boolean isHexString(String s) + { + int j = s.length(); + if (j == 0) + { + return false; + } + for (int i = 0; i < j; i++) + { + int c = s.charAt(i); + if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') + && !(c >= 'A' && c <= 'F')) + { + return false; + } + } + return true; + } } diff --git a/test/jalview/util/ColorUtilsTest.java b/test/jalview/util/ColorUtilsTest.java index fa4091f..08d77b6 100644 --- a/test/jalview/util/ColorUtilsTest.java +++ b/test/jalview/util/ColorUtilsTest.java @@ -71,9 +71,6 @@ public class ColorUtilsTest assertNull(ColorUtils.brighterThan(null)); } - /** - * @see http://www.rtapo.com/notes/named_colors.html - */ @Test(groups = { "Functional" }) public void testToTkCode() { @@ -188,8 +185,9 @@ public class ColorUtilsTest // 'hex' prefixes _not_ wanted here assertNull(ColorUtils.parseColourString("0x" + hexColour)); assertNull(ColorUtils.parseColourString("#" + hexColour)); - // out of range, but Color constructor just or's the rgb value with 0 - assertEquals(Color.black, ColorUtils.parseColourString("1000000")); + // hex values must be 6 hex digits + assertNull(ColorUtils.parseColourString("1000000")); + assertNull(ColorUtils.parseColourString("0ff00")); /* * by RGB triplet @@ -206,7 +204,7 @@ public class ColorUtilsTest */ assertNull(ColorUtils.parseColourString(null)); assertNull(ColorUtils.parseColourString("rubbish")); - assertEquals(Color.WHITE, ColorUtils.parseColourString("-1")); + assertNull(ColorUtils.parseColourString("-1")); assertNull(ColorUtils.parseColourString(String .valueOf(Integer.MAX_VALUE))); assertNull(ColorUtils.parseColourString("100,200,300")); // out of range @@ -216,13 +214,13 @@ public class ColorUtilsTest @Test(groups = "Functional") public void testGetAWTColorFromName() { - assertEquals(Color.white, ColorUtils.getAWTColorFromName("white")); - assertEquals(Color.white, ColorUtils.getAWTColorFromName("White")); - assertEquals(Color.white, ColorUtils.getAWTColorFromName("WHITE")); - assertEquals(Color.pink, ColorUtils.getAWTColorFromName("pink")); - assertNull(ColorUtils.getAWTColorFromName("mauve")); // no such name - assertNull(ColorUtils.getAWTColorFromName("")); - assertNull(ColorUtils.getAWTColorFromName(null)); + assertEquals(Color.white, ColorUtils.getColorFromName("white")); + assertEquals(Color.white, ColorUtils.getColorFromName("White")); + assertEquals(Color.white, ColorUtils.getColorFromName("WHITE")); + assertEquals(Color.pink, ColorUtils.getColorFromName("pink")); + assertNull(ColorUtils.getColorFromName("mauve")); // no such name + assertNull(ColorUtils.getColorFromName("")); + assertNull(ColorUtils.getColorFromName(null)); } @Test(groups = "Functional") diff --git a/test/jalview/util/StringUtilsTest.java b/test/jalview/util/StringUtilsTest.java index 084219a..0308f72 100644 --- a/test/jalview/util/StringUtilsTest.java +++ b/test/jalview/util/StringUtilsTest.java @@ -21,8 +21,10 @@ package jalview.util; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.fail; import jalview.gui.JvOptionPane; @@ -145,7 +147,7 @@ public class StringUtilsTest public void testListToDelimitedString() { assertEquals("", StringUtils.listToDelimitedString(null, ";")); - List list = new ArrayList(); + List list = new ArrayList<>(); assertEquals("", StringUtils.listToDelimitedString(list, ";")); list.add("now"); assertEquals("now", StringUtils.listToDelimitedString(list, ";")); @@ -250,4 +252,20 @@ public class StringUtilsTest assertEquals("kdHydro < 12.53", StringUtils.stripHtmlTags("kdHydro < 12.53")); } + + @Test(groups = { "Functional" }) + public void testIsHexString() + { + assertFalse(StringUtils.isHexString("")); + assertTrue(StringUtils.isHexString("0123456789abcdefABCDEF")); + assertFalse(StringUtils.isHexString("g")); + try + { + StringUtils.isHexString(null); + fail("expected exception"); + } catch (NullPointerException e) + { + // expected + } + } } -- 1.7.10.2