@Test public void testToColor() { assertEquals(Color.red, ColorParser.toColor("hsla(0, 100%, 0.5f, 1.0)")); assertEquals(Color.red, ColorParser.toColor("hsl(0, 1.0f, .5f)")); assertEquals(Color.red, ColorParser.toColor("red")); assertEquals(Color.red, ColorParser.toColor("red ")); assertEquals(Color.red, ColorParser.toColor("Red")); assertEquals(Color.white, ColorParser.toColor("WHITE")); assertEquals(Color.red, ColorParser.toColor("0xff0000")); assertEquals(Color.red, ColorParser.toColor("#F00")); assertEquals(Color.red, ColorParser.toColor("#FF0000")); assertEquals(Color.red, ColorParser.toColor("#FF0000 ")); assertEquals(Color.red, ColorParser.toColor("rgb(255, 0, 0)")); assertEquals(Color.red, ColorParser.toColor("rgb(255, 0, 0) ")); assertEquals(Color.gray, ColorParser.toColor("rgb(128, 128, 128) ")); assertEquals(Color.red, ColorParser.toColor("rgb(100%, 0%, 0%)")); assertEquals(Color.red, ColorParser.toColor("rgb(100%, 0%, 0%) ")); assertEquals(new Color(1.0f, 0.0f, 0.0f, 0.5f), ColorParser.toColor("rgba(255, 0, 0, 0.5)")); }
public static Color toColor(final String colorString) { String trimmedString = colorString.trim(); Color color = null; if (trimmedString.startsWith("#")) { final int shortHexCode = 4; if (trimmedString.length() == shortHexCode) { StringBuilder builder = new StringBuilder("#"); for (int i = 1; i < trimmedString.length(); i++) { builder.append(trimmedString.charAt(i)); builder.append(trimmedString.charAt(i)); } color = SLD.toColor(builder.toString()); } else { color = SLD.toColor(trimmedString); } } if (color == null) { color = parseRgbColor(trimmedString); } if (color == null) { color = parseRgbaColor(trimmedString); } if (color == null) { color = parseHslColor(trimmedString); } if (color == null) { color = parseHslaColor(trimmedString); } if (color == null) { final Field[] fields = Color.class.getFields(); for (Field field: fields) { if (field.getType() == Color.class && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getName().equalsIgnoreCase(trimmedString)) { try { return (Color) field.get(null); } catch (IllegalAccessException e) { throw new Error(e); } } } color = Color.decode(trimmedString); } return color; }
ColorParser { public static Color toColor(final String colorString) { String trimmedString = colorString.trim(); Color color = null; if (trimmedString.startsWith("#")) { final int shortHexCode = 4; if (trimmedString.length() == shortHexCode) { StringBuilder builder = new StringBuilder("#"); for (int i = 1; i < trimmedString.length(); i++) { builder.append(trimmedString.charAt(i)); builder.append(trimmedString.charAt(i)); } color = SLD.toColor(builder.toString()); } else { color = SLD.toColor(trimmedString); } } if (color == null) { color = parseRgbColor(trimmedString); } if (color == null) { color = parseRgbaColor(trimmedString); } if (color == null) { color = parseHslColor(trimmedString); } if (color == null) { color = parseHslaColor(trimmedString); } if (color == null) { final Field[] fields = Color.class.getFields(); for (Field field: fields) { if (field.getType() == Color.class && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getName().equalsIgnoreCase(trimmedString)) { try { return (Color) field.get(null); } catch (IllegalAccessException e) { throw new Error(e); } } } color = Color.decode(trimmedString); } return color; } }
ColorParser { public static Color toColor(final String colorString) { String trimmedString = colorString.trim(); Color color = null; if (trimmedString.startsWith("#")) { final int shortHexCode = 4; if (trimmedString.length() == shortHexCode) { StringBuilder builder = new StringBuilder("#"); for (int i = 1; i < trimmedString.length(); i++) { builder.append(trimmedString.charAt(i)); builder.append(trimmedString.charAt(i)); } color = SLD.toColor(builder.toString()); } else { color = SLD.toColor(trimmedString); } } if (color == null) { color = parseRgbColor(trimmedString); } if (color == null) { color = parseRgbaColor(trimmedString); } if (color == null) { color = parseHslColor(trimmedString); } if (color == null) { color = parseHslaColor(trimmedString); } if (color == null) { final Field[] fields = Color.class.getFields(); for (Field field: fields) { if (field.getType() == Color.class && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getName().equalsIgnoreCase(trimmedString)) { try { return (Color) field.get(null); } catch (IllegalAccessException e) { throw new Error(e); } } } color = Color.decode(trimmedString); } return color; } private  ColorParser(); }
ColorParser { public static Color toColor(final String colorString) { String trimmedString = colorString.trim(); Color color = null; if (trimmedString.startsWith("#")) { final int shortHexCode = 4; if (trimmedString.length() == shortHexCode) { StringBuilder builder = new StringBuilder("#"); for (int i = 1; i < trimmedString.length(); i++) { builder.append(trimmedString.charAt(i)); builder.append(trimmedString.charAt(i)); } color = SLD.toColor(builder.toString()); } else { color = SLD.toColor(trimmedString); } } if (color == null) { color = parseRgbColor(trimmedString); } if (color == null) { color = parseRgbaColor(trimmedString); } if (color == null) { color = parseHslColor(trimmedString); } if (color == null) { color = parseHslaColor(trimmedString); } if (color == null) { final Field[] fields = Color.class.getFields(); for (Field field: fields) { if (field.getType() == Color.class && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getName().equalsIgnoreCase(trimmedString)) { try { return (Color) field.get(null); } catch (IllegalAccessException e) { throw new Error(e); } } } color = Color.decode(trimmedString); } return color; } private  ColorParser(); static Color toColor(final String colorString); static boolean canParseColor(final String colorString); static String toRGB(final Color color); }
ColorParser { public static Color toColor(final String colorString) { String trimmedString = colorString.trim(); Color color = null; if (trimmedString.startsWith("#")) { final int shortHexCode = 4; if (trimmedString.length() == shortHexCode) { StringBuilder builder = new StringBuilder("#"); for (int i = 1; i < trimmedString.length(); i++) { builder.append(trimmedString.charAt(i)); builder.append(trimmedString.charAt(i)); } color = SLD.toColor(builder.toString()); } else { color = SLD.toColor(trimmedString); } } if (color == null) { color = parseRgbColor(trimmedString); } if (color == null) { color = parseRgbaColor(trimmedString); } if (color == null) { color = parseHslColor(trimmedString); } if (color == null) { color = parseHslaColor(trimmedString); } if (color == null) { final Field[] fields = Color.class.getFields(); for (Field field: fields) { if (field.getType() == Color.class && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && field.getName().equalsIgnoreCase(trimmedString)) { try { return (Color) field.get(null); } catch (IllegalAccessException e) { throw new Error(e); } } } color = Color.decode(trimmedString); } return color; } private  ColorParser(); static Color toColor(final String colorString); static boolean canParseColor(final String colorString); static String toRGB(final Color color);  }