ColorUtil { public static Color parseColor(String color) { if (color == null || color.isEmpty()) { return null; } if (color.charAt(0) == '#') { return new Color(parseHEX(color)); } Matcher matcher = RGBA.matcher(color); if (matcher.matches()){ return new Color(Integer.valueOf(matcher.group(1)), Integer.valueOf(matcher.group(2)), Integer.valueOf(matcher.group(3)), (int) (Float.parseFloat(matcher.group(4)) * 255 + 0.5) ); } matcher = RGB.matcher(color); if (matcher.matches()) { return new Color(Integer.valueOf(matcher.group(1)), Integer.valueOf(matcher.group(2)), Integer.valueOf(matcher.group(3))); } return new Color(parseHEX(color)); }  static Color parseColor(String color);  }
@Test public void testParseColors() { Color rgb = ColorUtil.parseColor("rgb(255,0,0)"); Color hex = ColorUtil.parseColor("#FF0000"); Color rrggbb = ColorUtil.parseColor("FF0000"); Color rgba = ColorUtil.parseColor("rgba(255,0,0,1)"); Assert.assertEquals(Color.RED, rgb); Assert.assertEquals(Color.RED, hex); Assert.assertEquals(Color.RED, rrggbb); Assert.assertEquals(Color.RED, rgba); }
@Test public void testParseRGBA() { Color rgba = ColorUtil.parseColor("rgba(255,0,0,0.5)"); Assert.assertTrue(128 == rgba.getAlpha()); Assert.assertTrue(Transparency.TRANSLUCENT == rgba.getTransparency()); }