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
*/
}
}
- 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
* <ul>
* <li>an AWT colour name e.g. white</li>
- * <li>a hex colour value (without prefix) e.g. ff0000</li>
- * <li>an rgb triple e.g. 100,50,150</li>
+ * <li>a six digit rgb hex colour value (without prefix) e.g. ff0000</li>
+ * <li>a comma-separated rgb triple e.g. 100,50,150</li>
* </ul>
*
* @param colour
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)
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
}
/**
* 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;
+ }
}
}