public static final Color hex2RGB ( final String hex ) { String r , g , b ; final int len = hex . length ( ) ; if ( len == 7 ) { r = hex . substring ( 1 , 3 ) ; g = hex . substring ( 3 , 5 ) ; b = hex . substring ( 5 , 7 ) ; } else if ( len == 4 ) { r = hex . substring ( 1 , 2 ) ; g = hex . substring ( 2 , 3 ) ; b = hex . substring ( 3 , 4 ) ; r = r + r ; g = g + g ; b = b + b ; } else { return null ; // error - invalid length } try { return new Color ( Integer . valueOf ( r , 16 ) , Integer . valueOf ( g , 16 ) , Integer . valueOf ( b , 16 ) ) ; } catch ( final NumberFormatException ignored ) { return null ; } }
Converts Hex string to RGB . Assumes