X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FColorUtils.java;h=d465632b6c6cbee44102bc5a6699617c935ce113;hb=f6d94f44ef8adcc960ac0cf419928bdf2198fcc0;hp=a2d8d3efb13488796b17fa5eafe61276f9a2c81b;hpb=0a94254977a8824ad154325a8ccb6d6d1d4e4dec;p=jalview.git diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index a2d8d3e..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 */ @@ -199,48 +203,12 @@ public class ColorUtils } } - public static int bleachColourInt(int colour, float bleachFactor) - { - if (bleachFactor >= 1f) - { - return -1;// Color.WHITE; - } - if (bleachFactor <= -1f) - { - return 0xFF000000;// Color.BLACK; - } - if (bleachFactor == 0f) - { - return colour; - } - - int red = (colour >> 16) & 0xFF;// getRed(); - int green = (colour >> 8) & 0xFF;// colour.getGreen(); - int blue = colour & 0xFF;// .getBlue(); - - if (bleachFactor > 0) - { - red += (255 - red) * bleachFactor; - green += (255 - green) * bleachFactor; - blue += (255 - blue) * bleachFactor; - } - else - { - float factor = 1 + bleachFactor; - red *= factor; - green *= factor; - blue *= factor; - } - return 0xFF000000 | (red << 16) | (green << 8) | blue;// new Color(red, - // green, blue); - } - /** * Parses a string into a Color, where the accepted formats are * * * @param colour @@ -255,18 +223,20 @@ public class ColorUtils colour = colour.trim(); Color col = null; - try + if (colour.length() == 6 && StringUtils.isHexString(colour)) { - int value = Integer.parseInt(colour, 16); - col = new Color(value); - } catch (NumberFormatException ex) - { - 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) @@ -281,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 } @@ -349,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; + } } }