Java examples for 2D Graphics:Color RGB
Decodes a color of the form #hhhhhhhh or #hhhhhh, (for example, #39F80D8C, or #9A67E9), where each two digit hexadecimal part is the red, green, blue, and alpha, respectively.
//package com.java2s; import java.awt.*; public class Main { public static void main(String[] argv) throws Exception { String s = "java2s.com"; System.out.println(decode(s)); }//from ww w . j a v a2s .c o m /** * Decodes a color of the form #hhhhhhhh or #hhhhhh, (for example, * #39F80D8C, or #9A67E9), where each two digit hexadecimal part is the red, * green, blue, and alpha, respectively. The alpha component is optional, * and will be assumed to be FF if not present. * * @param s * the string to decode * @return a color described by the string parameter */ public static Color decode(String s) { if (s.startsWith("#")) { //hex string if (s.length() == 9) { //includes alpha int alpha = Integer.parseInt(s.substring(7), 16); Color temp = Color.decode(s.substring(0, 7)); return new Color(temp.getRed(), temp.getGreen(), temp.getBlue(), alpha); } else if (s.length() == 7) { //does not include alpha return Color.decode(s); } } return Color.BLACK; } }