X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FColorUtils.java;h=d465632b6c6cbee44102bc5a6699617c935ce113;hb=f0fd407e5fad67185a9813c57bfc50aacaf1de6e;hp=3eb080b0e126c8cdcfa486f4078ff802203da659;hpb=f300e30df117ff4be7d1df5c781161efd0285e5f;p=jalview.git 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; + } } }