code: private String simplifyRGBColours(String contents) {
          StringBuffer newContents = new StringBuffer();
          StringBuffer hexColour;
          String[] rgbColours;
          int colourValue;
  
          Pattern pattern = Pattern.compile("rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)");
          Matcher matcher = pattern.matcher(contents);
  
          while (matcher.find()) {
              hexColour = new StringBuffer("#");
              rgbColours = matcher.group(1).split(",");
              for (String rgbColour : rgbColours) {
                  colourValue = Integer.parseInt(rgbColour);
                  if (colourValue < 16) {
                      hexColour.append("0");
                  }
                  hexColour.append(Integer.toHexString(colourValue));
              }
              matcher.appendReplacement(newContents, hexColour.toString());
          }
          matcher.appendTail(newContents);
  
          return newContents.toString();
      }
docs: Convert rgb(51,102,153) to #336699 (this code largely based on YUI code)