[seam-commits] Seam SVN: r8055 - trunk/src/pdf/org/jboss/seam/pdf.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Apr 28 12:48:42 EDT 2008


Author: norman.richards at jboss.com
Date: 2008-04-28 12:48:42 -0400 (Mon, 28 Apr 2008)
New Revision: 8055

Modified:
   trunk/src/pdf/org/jboss/seam/pdf/ITextUtils.java
Log:
JBSEAM-1165

Modified: trunk/src/pdf/org/jboss/seam/pdf/ITextUtils.java
===================================================================
--- trunk/src/pdf/org/jboss/seam/pdf/ITextUtils.java	2008-04-28 16:46:45 UTC (rev 8054)
+++ trunk/src/pdf/org/jboss/seam/pdf/ITextUtils.java	2008-04-28 16:48:42 UTC (rev 8055)
@@ -9,25 +9,25 @@
 import com.lowagie.text.Rectangle;
 
 public class ITextUtils {
-	static Map<String,Color> colorMap = new HashMap<String,Color>(); 
-	
-	static {
-		colorMap.put("white", Color.white);
-		colorMap.put("gray", Color.gray);
-		colorMap.put("lightgray", Color.lightGray);
-		colorMap.put("darkgray", Color.darkGray);
-		colorMap.put("black", Color.black);
-		colorMap.put("red", Color.red);
-		colorMap.put("pink", Color.pink);
-		colorMap.put("yellow", Color.yellow);
-		colorMap.put("green", Color.green);
-		colorMap.put("magenta", Color.magenta);
-		colorMap.put("cyan", Color.cyan);
-		colorMap.put("blue", Color.blue);
-		colorMap.put("orange", Color.orange);
-	}
+    static Map<String,Color> colorMap = new HashMap<String,Color>(); 
 
-	
+    static {
+        colorMap.put("white", Color.white);
+        colorMap.put("gray", Color.gray);
+        colorMap.put("lightgray", Color.lightGray);
+        colorMap.put("darkgray", Color.darkGray);
+        colorMap.put("black", Color.black);
+        colorMap.put("red", Color.red);
+        colorMap.put("pink", Color.pink);
+        colorMap.put("yellow", Color.yellow);
+        colorMap.put("green", Color.green);
+        colorMap.put("magenta", Color.magenta);
+        colorMap.put("cyan", Color.cyan);
+        colorMap.put("blue", Color.blue);
+        colorMap.put("orange", Color.orange);
+    }
+
+
     /**
      *  not all itext objects accept a string value as input,
      *  so we'll copy that logic here. 
@@ -35,12 +35,12 @@
     public static int alignmentValue(String alignment) {
         return ElementTags.alignmentValue(alignment);     
     }
-    
+
     public static Rectangle pageSizeValue(String name)  {
         return PageSize.getRectangle(name);
     }
-    
-    
+
+
     /**
      * return a color value from a string specification.  
      */ 
@@ -48,32 +48,68 @@
         if (colorName == null) {
             return null;
         }
+
+        colorName = colorName.trim().toLowerCase();
+
+        Color color = colorMap.get(colorName);
+
+        if (color == null && colorName.startsWith("rgb")) {
+            color = rgbStringToColor(colorName);
+        } 
+        if (color == null) {
+            color = Color.decode(colorName);
+        }
+
+        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(",");
         
-    	Color color = colorMap.get(colorName.toLowerCase());
-    	if (color == null) {
-    	    color = Color.decode(colorName);
-    	}
-    	return color;
+        if (rgb.length == 3) {
+            return new Color(parseSingleChanel(rgb[0]),
+                             parseSingleChanel(rgb[1]),
+                             parseSingleChanel(rgb[2]));
+        } else if (rgb.length == 4) {
+            return new Color(parseSingleChanel(rgb[0]),
+                             parseSingleChanel(rgb[1]),
+                             parseSingleChanel(rgb[2]),
+                             parseSingleChanel(rgb[3]));
+        }
+
+        throw new RuntimeException("invalid rgb color specification: " + rgbString);
     }
 
+    private 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]", ""));
+    }
+
     public static float[] stringToFloatArray(String text) {
-    	String[] parts = text.split("\\s");
-    	float[]  values = new float[parts.length];
-    	for (int i=0;i<parts.length;i++) {
-    	   values[i] = Float.valueOf(parts[i]);
-    	}
-    	
-    	return values;
+        String[] parts = text.split("\\s");
+        float[]  values = new float[parts.length];
+        for (int i=0;i<parts.length;i++) {
+            values[i] = Float.valueOf(parts[i]);
+        }
+
+        return values;
     }
-    
-    
+
+
     public static int[] stringToIntArray(String text) {
         String[] parts = text.split("\\s");
         int[]  values = new int[parts.length];
         for (int i=0;i<parts.length;i++) {
-           values[i] = Integer.valueOf(parts[i]);
+            values[i] = Integer.valueOf(parts[i]);
         }
-        
+
         return values;
     }
 }




More information about the seam-commits mailing list