public static Color getRGBColor ( String name ) throws IllegalArgumentException { int [ ] c = { 0 , 0 , 0 , 0 } ; if ( name . startsWith ( "#" ) ) { if ( name . length ( ) == 4 ) { c [ 0 ] = Integer . parseInt ( name . substring ( 1 , 2 ) , 16 ) * 16 ; c [ 1 ] = Integer . parseInt ( name . substring ( 2 , 3 ) , 16 ) * 16 ; c [ 2 ] = Integer . parseInt ( name . substring ( 3 ) , 16 ) * 16 ; return new Color ( c [ 0 ] , c [ 1 ] , c [ 2 ] , c [ 3 ] ) ; } if ( name . length ( ) == 7 ) { c [ 0 ] = Integer . parseInt ( name . substring ( 1 , 3 ) , 16 ) ; c [ 1 ] = Integer . parseInt ( name . substring ( 3 , 5 ) , 16 ) ; c [ 2 ] = Integer . parseInt ( name . substring ( 5 ) , 16 ) ; return new Color ( c [ 0 ] , c [ 1 ] , c [ 2 ] , c [ 3 ] ) ; } throw new IllegalArgumentException ( "Unknown color format. Must be #RGB or #RRGGBB" ) ; } else if ( name . startsWith ( "rgb(" ) ) { StringTokenizer tok = new StringTokenizer ( name , "rgb(), \t\r\n\f" ) ; for ( int k = 0 ; k < 3 ; ++ k ) { String v = tok . nextToken ( ) ; if ( v . endsWith ( "%" ) ) c [ k ] = Integer . parseInt ( v . substring ( 0 , v . length ( ) - 1 ) ) * 255 / 100 ; else c [ k ] = Integer . parseInt ( v ) ; if ( c [ k ] < 0 ) c [ k ] = 0 ; else if ( c [ k ] > 255 ) c [ k ] = 255 ; } return new Color ( c [ 0 ] , c [ 1 ] , c [ 2 ] , c [ 3 ] ) ; } name = name . toLowerCase ( ) ; if ( ! NAMES . containsKey ( name ) ) throw new IllegalArgumentException ( "Color '" + name + "' not found." ) ; c = ( int [ ] ) NAMES . get ( name ) ; return new Color ( c [ 0 ] , c [ 1 ] , c [ 2 ] , c [ 3 ] ) ; }
Gives you a Color based on a name .