/** * Returns a Color object that corresponds to the value. * * @param value The string representation of the color. This may be a color name or a text * representation of the color's rgb value. * @param dflt Default color if value cannot be converted to a color. * @return The Color object corresponding to the value, or null if not a recognized value. */ public static Color toColor(String value, Color dflt) { String rgb = getRGBFromName(value); rgb = rgb == null ? value : rgb; try { return rgb == null || rgb.isEmpty() ? dflt : Color.decode(rgb); } catch (Exception e) { return dflt; } }
public void testColorUtil(String testColor, String testRGB) { Color refColor = Color.magenta; String rgb = ColorUtil.getRGBFromName(testColor); assertEquals(rgb, testRGB); String color = ColorUtil.getNameFromRGB(rgb); assertEquals(color.toLowerCase(), testColor.toLowerCase()); Color color1 = ColorUtil.toColor(rgb, refColor); Color color2 = ColorUtil.toColor(color, refColor); assertEquals(color1, color2); Color color3 = ColorUtil.toColor("badvalue", refColor); assertEquals(refColor, color3); }