/** * 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. * @return The Color object corresponding to the value, or null if not a recognized value. */ public static Color toColor(String value) { return toColor(value, null); }
@Override protected String _toString(Color value) { return ColorUtil.toString(value); }
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); }
/** * Converts a color to a web-friendly string format. * * @param value The color value to convert. * @return The converted value. */ public static String toString(Color value) { String rgb = "#" + Integer.toHexString(value.getRGB() | 0xFF000000).substring(2).toUpperCase(); String name = getNameFromRGB(rgb); return name == null ? rgb : name; }
/** * 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; } }
@Override protected Color _toValue(String text) { return ColorUtil.toColor(text); }
@Override protected String getValue() { Color value = editor.getValue(); return value == null ? null : ColorUtil.toString(value); }
@Override protected void setValue(Object value) { editor.setValue(ColorUtil.toColor((String) value)); updateValue(); }
@Override protected void init(Object target, PropertyInfo propInfo, PropertyGrid propGrid) { super.init(target, propInfo, propGrid); String[] values = propInfo.getConfigValueArray("values"); if (values == null) { //component.setAutoAdd(true); } else { //component.setAutoAdd(false); editor.clear(); for (String choice : values) { String[] color = choice.split("\\:", 2); for (int i = 0; i < color.length; i++) { ColorPickeritem item = new ColorPickeritem(ColorUtil.toColor(color[i])); editor.addChild(item); } } } }