Java tutorial
//package com.java2s; import java.awt.Color; import java.util.HashMap; import java.util.Map; public class Main { static Map<String, Color> colorMap = new HashMap<String, Color>(); /** * return a color value from a string specification. */ public static Color colorValue(String colorName) { if (colorName == null) { return null; } colorName = colorName.trim().toLowerCase(); Color color = colorMap.get(colorName); if (color == null && colorName.startsWith("rgb")) { color = rgbStringToColor(colorName); } if (color == null) { color = Color.decode(colorName); } return color; } public static Color rgbStringToColor(String rgbString) { String rgb[] = rgbString.split(","); if (rgb.length == 3) { return new Color(parseSingleChanel(rgb[0]), parseSingleChanel(rgb[1]), parseSingleChanel(rgb[2])); } else if (rgb.length == 4) { return new Color(parseSingleChanel(rgb[0]), parseSingleChanel(rgb[1]), parseSingleChanel(rgb[2]), parseSingleChanel(rgb[3])); } throw new RuntimeException("invalid rgb color specification: " + rgbString); } private static int parseSingleChanel(String chanel) { if (chanel.contains("%")) { float percent = Float.parseFloat(chanel.replaceAll("[^0-9\\.]", "")); return (int) (255 * (percent / 100)); } return Integer.parseInt(chanel.replaceAll("[^0-9]", "")); } }