Java examples for 2D Graphics:Color String
decode String to Color
//package com.java2s; import java.awt.Color; public class Main { public static void main(String[] argv) throws Exception { String cs = "java2s.com"; System.out.println(decode(cs)); }//from w w w. jav a 2 s. com public static Color decode(String cs) { if (cs.startsWith("#")) cs = cs.substring(1); else if (cs.startsWith("0x")) cs = cs.substring(2); if (cs.length() < 6) return new Color(255, 255, 255, 255); ; boolean hasAlpha = cs.length() == 8; int rgbOffset = hasAlpha ? 2 : 0; int a = hasAlpha ? Integer.parseInt(cs.substring(0, 2), 16) : 255; int r = Integer.parseInt( cs.substring(0 + rgbOffset, 2 + rgbOffset), 16); int g = Integer.parseInt( cs.substring(2 + rgbOffset, 4 + rgbOffset), 16); int b = Integer.parseInt( cs.substring(4 + rgbOffset, 6 + rgbOffset), 16); return new Color(r, g, b, a); } }