X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FColorUtils.java;h=008c8b0f66522ea3d88a7c0f9a1a1b99b0df2169;hb=3cc6cd558fa548219f4e12b3abbb6b88d0d20c23;hp=d4be3228152d22b78dbdcab4568ccba86b9598f6;hpb=3d0101179759ef157b088ea135423cd909512d9f;p=jalview.git diff --git a/src/jalview/util/ColorUtils.java b/src/jalview/util/ColorUtils.java index d4be322..008c8b0 100644 --- a/src/jalview/util/ColorUtils.java +++ b/src/jalview/util/ColorUtils.java @@ -24,11 +24,21 @@ package jalview.util; +import java.util.Locale; + import java.awt.Color; +import java.util.HashMap; +import java.util.Map; import java.util.Random; public class ColorUtils { + private static final int MAX_CACHE_SIZE = 1729; + + /* + * a cache for colours generated from text strings + */ + static Map myColours = new HashMap<>(); /** * Generates a random color, will mix with input color. Code taken from @@ -212,12 +222,15 @@ public class ColorUtils colour = colour.trim(); Color col = null; - try - { - int value = Integer.parseInt(colour, 16); - col = new Color(value); - } catch (NumberFormatException ex) + if (StringUtils.isHexString(colour)) { + try + { + int value = Integer.parseInt(colour, 16); + col = new Color(value); + } catch (NumberFormatException ex) + { + } } if (col == null) @@ -260,6 +273,10 @@ public class ColorUtils { return Color.white; } + if (myColours.containsKey(name)) + { + return myColours.get(name); + } int lsize = name.length(); int start = 0; int end = lsize / 3; @@ -291,6 +308,11 @@ public class ColorUtils Color color = new Color(r, g, b); + if (myColours.size() < MAX_CACHE_SIZE) + { + myColours.put(name, color); + } + return color; } @@ -308,7 +330,7 @@ public class ColorUtils return null; } Color col = null; - name = name.toLowerCase(); + name = name.toLowerCase(Locale.ROOT); // or make a static map; or use reflection on the field name switch (name)