import java.util.Map;
import java.util.Random;
+/**
+ * A class with utility methods for manipulating AWT colours/colors
+ */
public class ColorUtils
{
- // constant borrowed from java.awt.Color
- private static final float FACTOR = 0.7f;
-
private static final int MAX_CACHE_SIZE = 1729;
+
/*
* a cache for colours generated from text strings
*/
* 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
- {
- int value = Integer.parseInt(colour, 16);
- col = new Color(value);
- } catch (NumberFormatException ex)
+ if (colour.length() == 6 && StringUtils.isHexString(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)
{
if (name == null)
{
return null;
}
- Color col = null;
- name = name.toLowerCase();
-
// or make a static map; or use reflection on the field name
- switch (name)
+ switch (name.toLowerCase())
{
case "black":
- col = Color.black;
- break;
+ return Color.black;
case "blue":
- col = Color.blue;
- break;
+ return Color.blue;
case "cyan":
- col = Color.cyan;
- break;
+ return Color.cyan;
case "darkgray":
- col = Color.darkGray;
- break;
+ return Color.darkGray;
case "gray":
- col = Color.gray;
- break;
+ return Color.gray;
case "green":
- col = Color.green;
- break;
+ return Color.green;
case "lightgray":
- col = Color.lightGray;
- break;
+ return Color.lightGray;
case "magenta":
- col = Color.magenta;
- break;
+ return Color.magenta;
case "orange":
- col = Color.orange;
- break;
+ return Color.orange;
case "pink":
- col = Color.pink;
- break;
+ return Color.pink;
case "red":
- col = Color.red;
- break;
+ return Color.red;
case "white":
- col = Color.white;
- break;
+ return Color.white;
case "yellow":
- col = Color.yellow;
- break;
- }
-
- return col;
- }
-
- /**
- * Generates a colour that is interpolated between
- * <code>colour.darker()</code> and <code>colour.brighter()</code> in
- * proportion as <code>value</code> is between <code>min</code> and
- * <code>max</code>. Note that the 'neutral point' (unchanged colour) is
- * closer to 'brighter' than to 'darker'as this is a geometric range.
- *
- * @param value
- * @param min
- * @param max
- * @param colour
- * @return
- */
- public static Color getGraduatedColour(float value, float min, float max,
- Color colour)
- {
- /*
- * this computes the equivalent of
- * getGraduatedColour(value, min, colour.darker(), max, colour.brighter())
- * but avoiding object creation except for the return value
- */
- if (value < min)
- {
- value = min;
- }
- if (value > max)
- {
- value = max;
+ return Color.yellow;
+ default:
+ return null;
}
-
- int r = colour.getRed();
- int g = colour.getGreen();
- int b = colour.getBlue();
-
- /*
- * rgb for colour.darker():
- */
- float minR = r * FACTOR;
- float minG = g * FACTOR;
- float minB = b * FACTOR;
-
- /*
- * rgb for colour.brighter():
- */
- float maxR = Math.min(255f, r / FACTOR);
- float maxG = Math.min(255f, g / FACTOR);
- float maxB = Math.min(255f, b / FACTOR);
-
- /*
- * interpolation
- */
- float p = (value - min) / (max - min);
- int newR = (int) (minR + p * (maxR - minR));
- int newG = (int) (minG + p * (maxG - minG));
- int newB = (int) (minB + p * (maxB - minB));
-
- return new Color(newR, newG, newB, colour.getAlpha());
}
}