public static String shorthandHex ( String color ) { validateHex ( color ) ; if ( color . length ( ) > 5 ) { StringBuilder shorthandColor = new StringBuilder ( ) ; int startIndex = 0 ; if ( color . startsWith ( "#" ) ) { shorthandColor . append ( "#" ) ; startIndex ++ ; } for ( ; startIndex < color . length ( ) ; startIndex += 2 ) { String shorthand = shorthandHexSingle ( color . substring ( startIndex , startIndex + 2 ) ) ; if ( shorthand . length ( ) > 1 ) { shorthandColor = null ; break ; } shorthandColor . append ( shorthand ) ; } if ( shorthandColor != null ) { color = shorthandColor . toString ( ) ; } } return color ; }
Shorthand the hex color if possible