Here you can find the source of decodeHexColor(String hexString)
private static Color decodeHexColor(String hexString)
//package com.java2s; //License from project: Apache License import java.awt.Color; public class Main { private static Color decodeHexColor(String hexString) { if (hexString.length() == 6 || hexString.length() == 8) { // RGB color int r = parseHexColorComponent(hexString, 0); int g = parseHexColorComponent(hexString, 2); int b = parseHexColorComponent(hexString, 4); if (hexString.length() == 8) { // RGBA color int a = parseHexColorComponent(hexString, 6); return new Color(r, g, b, a); }/*from w ww . jav a 2 s. com*/ return new Color(r, g, b); } else { throw new IllegalArgumentException("Color is not a valid hex string: #" + hexString); } } private static int parseHexColorComponent(String hexString, int offset) { try { return Integer.parseInt(hexString.substring(offset, offset + 2), 16); } catch (NumberFormatException e) { throw new IllegalArgumentException("Color is not a valid hex string: #" + hexString); } catch (ArrayIndexOutOfBoundsException e) { throw new IllegalArgumentException("Color is not a valid hex string: #" + hexString, e); } } }