3 Replies Latest reply on Apr 10, 2007 5:27 PM by Dustin Norlander

    Submit: rgb color values for PDF

    Dustin Norlander Apprentice

      Hi,

      Not sure what the best method to submit is, but since the code needed is extremely small I thought I would just post it here.

      The following will add support for color in the form

      rgb(255,0,0)
      or
      rgb(100%, 0%, 0%)
      


      I noticed that hex values seem to work fine, though they are not documented.

      here is the code for ITextUtils.java:

       /**
       * return a color value from a string specification.
       */
       public static Color colorValue(String colorName) {
       String clr = colorName.trim().toLowerCase();
       if (clr.startsWith("rgb"))
       return rgbStringToColor(clr);
      
       Color color = colorMap.get(clr);
       if (color == null) {
       color = Color.decode(clr);
       }
       return color;
       }
      
      /*
       * Returns color of the form rgb(r,g,b) or rgb(r,g,b,a)
       * r,g,b,a values can be 0-255 or float values with a '%' sign
       */
      public static Color rgbStringToColor(String rgbString) {
       String rgb[] = rgbString.split(",");
       int r = parseSingleChanel(rgb[0]);
       int g = parseSingleChanel(rgb[1]);
       int b = parseSingleChanel(rgb[2]);
       if (rgb.length != 4)
       return new Color(r,g,b);
       int a = parseSingleChanel(rgb[3]);
       return new Color(r,g,b,a);
      }
      
      public static int parseSingleChanel(String chanel) {
       if (chanel.contains("%")) {
       float percent = Float.parseFloat(chanel.replaceAll("[^0-9\\.]",""));
       return (int)(255 * (percent / 100));
       }
       return Integer.parseInt(chanel.replaceAll("[^0-9]", ""));
      }
      


      Hope this is useful,
      Dustin