static int parse(String c){
  try {
    int skipInitial, skipBetween;
    if (c.charAt(0) == '#') {
      skipInitial=1;
      skipBetween=0;
    }
 else     if (c.startsWith("rgb:")) {
      skipInitial=4;
      skipBetween=1;
    }
 else {
      return 0;
    }
    int charsForColors=c.length() - skipInitial - 2 * skipBetween;
    if (charsForColors % 3 != 0)     return 0;
    int componentLength=charsForColors / 3;
    double mult=255 / (Math.pow(2,componentLength * 4) - 1);
    int currentPosition=skipInitial;
    String rString=c.substring(currentPosition,currentPosition + componentLength);
    currentPosition+=componentLength + skipBetween;
    String gString=c.substring(currentPosition,currentPosition + componentLength);
    currentPosition+=componentLength + skipBetween;
    String bString=c.substring(currentPosition,currentPosition + componentLength);
    int r=(int)(Integer.parseInt(rString,16) * mult);
    int g=(int)(Integer.parseInt(gString,16) * mult);
    int b=(int)(Integer.parseInt(bString,16) * mult);
    return 0xFF << 24 | r << 16 | g << 8 | b;
  }
 catch (  NumberFormatException|IndexOutOfBoundsException e) {
    return 0;
  }
}

static int parse ( String c ) { try { int skipInitial , skipBetween ; if ( c . charAt ( 0 ) == '#' ) { skipInitial = 1 ; skipBetween = 0 ; } else if ( c . startsWith ( "rgb:" ) ) { skipInitial = 4 ; skipBetween = 1 ; } else { return 0 ; } int charsForColors = c . length ( ) - skipInitial - 2 * skipBetween ; if ( charsForColors % 3 != 0 ) return 0 ; int componentLength = charsForColors / 3 ; double mult = 255 / ( Math . pow ( 2 , componentLength * 4 ) - 1 ) ; int currentPosition = skipInitial ; String rString = c . substring ( currentPosition , currentPosition + componentLength ) ; currentPosition += componentLength + skipBetween ; String gString = c . substring ( currentPosition , currentPosition + componentLength ) ; currentPosition += componentLength + skipBetween ; String bString = c . substring ( currentPosition , currentPosition + componentLength ) ; int r = ( int ) ( Integer . parseInt ( rString , 16 ) * mult ) ; int g = ( int ) ( Integer . parseInt ( gString , 16 ) * mult ) ; int b = ( int ) ( Integer . parseInt ( bString , 16 ) * mult ) ; return 0xFF << 24 | r << 16 | g << 8 | b ; } catch ( NumberFormatException | IndexOutOfBoundsException e ) { return 0 ; } }
parse color according to http : / / manpages . ubuntu . com / manpages / intrepid / man3 / xquerycolor . 3 . html < p / > highest bit is set if successful , so return value is 0xff $ { r } $ { g } $ { b } . return 0 if failed .
