You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2015/07/20 02:46:51 UTC

svn commit: r1691864 - in /poi/trunk/src: java/org/apache/poi/ss/usermodel/ExtendedColor.java ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java

Author: nick
Date: Mon Jul 20 00:46:50 2015
New Revision: 1691864

URL: http://svn.apache.org/r1691864
Log:
Move some of the XSSF Color logic to a base class, so that the HSSF Extended Color wrapper can use it

Added:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/ExtendedColor.java
      - copied, changed from r1691844, poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java

Copied: poi/trunk/src/java/org/apache/poi/ss/usermodel/ExtendedColor.java (from r1691844, poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/ExtendedColor.java?p2=poi/trunk/src/java/org/apache/poi/ss/usermodel/ExtendedColor.java&p1=poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java&r1=1691844&r2=1691864&rev=1691864&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/ExtendedColor.java Mon Jul 20 00:46:50 2015
@@ -14,113 +14,77 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-package org.apache.poi.xssf.usermodel;
+package org.apache.poi.ss.usermodel;
 
 import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.usermodel.Color;
-import org.apache.poi.util.Internal;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 
 /**
- * Represents a color in SpreadsheetML
+ * Represents a XSSF-style color (based on either a
+ *  {@link org.apache.poi.xssf.usermodel.XSSFColor} or a
+ *  {@link org.apache.poi.hssf.record.common.ExtendedColor} 
  */
-public class XSSFColor implements Color {
-
-    private CTColor ctColor;
+public abstract class ExtendedColor implements Color {
+    protected void setColor(java.awt.Color clr) {
+        setRGB(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
+    }
 
     /**
-     * Create an instance of XSSFColor from the supplied XML bean
+     * A boolean value indicating the color is automatic
      */
-    public XSSFColor(CTColor color) {
-        this.ctColor = color;
-    }
+    public abstract boolean isAuto();
 
     /**
-     * Create an new instance of XSSFColor
+     * A boolean value indicating the color is indexed
      */
-    public XSSFColor() {
-        this.ctColor = CTColor.Factory.newInstance();
-    }
-
-    public XSSFColor(java.awt.Color clr) {
-        this();
-        ctColor.setRgb(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
-    }
-
-    public XSSFColor(byte[] rgb) {
-        this();
-        ctColor.setRgb(rgb);
-    }
+    public abstract boolean isIndexed();
 
     /**
-     * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+     * A boolean value indicating the color is RGB / ARGB
      */
-    public boolean isAuto() {
-        return ctColor.getAuto();
-    }
-
+    public abstract boolean isRGB();
+    
     /**
-     * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+     * A boolean value indicating the color is from a Theme
      */
-    public void setAuto(boolean auto) {
-        ctColor.setAuto(auto);
-    }
-
+    public abstract boolean isThemed();
+    
     /**
-     * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+     * Indexed Color value, if {@link #isIndexed()} is true
      */
-    public short getIndexed() {
-        return (short)ctColor.getIndexed();
-    }
-
+    public abstract short getIndex();
+    
     /**
-     * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+     * Index of Theme color, if {@link #isThemed()} is true
      */
-    public void setIndexed(int indexed) {
-        ctColor.setIndexed(indexed);
-    }
-
-   /**
-    * Standard Red Green Blue ctColor value (RGB).
-    * If there was an A (Alpha) value, it will be stripped.
-    */
-   public byte[] getRgb() {
-      byte[] rgb = getRGBOrARGB();
-      if(rgb == null) return null;
-
-      if(rgb.length == 4) {
-         // Need to trim off the alpha
-         byte[] tmp = new byte[3];
-         System.arraycopy(rgb, 1, tmp, 0, 3);
-         return tmp;
-      } else {
-         return rgb;
-      }
-   }
+    public abstract int getTheme();
 
-   /**
-    * Standard Alpha Red Green Blue ctColor value (ARGB).
-    */
-   public byte[] getARgb() {
-      byte[] rgb = getRGBOrARGB();
-      if(rgb == null) return null;
-
-      if(rgb.length == 3) {
-         // Pad with the default Alpha
-         byte[] tmp = new byte[4];
-         tmp[0] = -1;
-         System.arraycopy(rgb, 0, tmp, 1, 3);
-         return tmp;
-      } else {
-         return rgb;
-      }
-   }
+    /**
+     * Standard Red Green Blue ctColor value (RGB).
+     * If there was an A (Alpha) value, it will be stripped.
+     */
+    public abstract byte[] getRGB();
+    /**
+     * Standard Alpha Red Green Blue ctColor value (ARGB).
+     */
+    public abstract byte[] getARGB();
 
-   private byte[] getRGBOrARGB() {
+    /**
+     * RGB or ARGB or null
+     */
+    protected abstract byte[] getStoredRBG();
+    
+    /**
+     * Sets the Red Green Blue or Alpha Red Green Blue
+     */
+    public abstract void setRGB(byte[] rgb);
+   
+   protected byte[] getRGBOrARGB() {
         byte[] rgb = null;
 
-        if (ctColor.isSetIndexed() && ctColor.getIndexed() > 0) {
-            HSSFColor indexed = HSSFColor.getIndexHash().get((int) ctColor.getIndexed());
+        if (isIndexed() && getIndex() > 0) {
+            int indexNum = getIndex();
+            HSSFColor indexed = HSSFColor.getIndexHash().get(indexNum);
             if (indexed != null) {
                rgb = new byte[3];
                rgb[0] = (byte) indexed.getTriplet()[0];
@@ -130,13 +94,8 @@ public class XSSFColor implements Color
             }
          }
 
-         if (!ctColor.isSetRgb()) {
-            // No colour is available, sorry
-            return null;
-         }
-
          // Grab the colour
-         rgb = ctColor.getRgb();
+         rgb = getStoredRBG();
          return rgb;
     }
 
@@ -144,8 +103,8 @@ public class XSSFColor implements Color
      * Standard Red Green Blue ctColor value (RGB) with applied tint.
      * Alpha values are ignored.
      */
-    public byte[] getRgbWithTint() {
-        byte[] rgb = ctColor.getRgb();
+    public byte[] getRGBWithTint() {
+        byte[] rgb = getStoredRBG();
         if (rgb != null) {
             if(rgb.length == 4) {
                byte[] tmp = new byte[3];
@@ -153,7 +112,7 @@ public class XSSFColor implements Color
                rgb = tmp;
             }
             for (int i = 0; i < rgb.length; i++){
-                rgb[i] = applyTint(rgb[i] & 0xFF, ctColor.getTint());
+                rgb[i] = applyTint(rgb[i] & 0xFF, getTint());
             }
         }
         return rgb;
@@ -165,7 +124,7 @@ public class XSSFColor implements Color
      */
     public String getARGBHex() {
        StringBuffer sb = new StringBuffer();
-       byte[] rgb = getARgb();
+       byte[] rgb = getARGB();
        if(rgb == null) {
           return null;
        }
@@ -191,29 +150,6 @@ public class XSSFColor implements Color
     }
 
     /**
-     * Standard Alpha Red Green Blue ctColor value (ARGB).
-     */
-    public void setRgb(byte[] rgb) {
-       ctColor.setRgb(rgb);
-    }
-
-    /**
-     * Index into the <clrScheme> collection, referencing a particular <sysClr> or
-     *  <srgbClr> value expressed in the Theme part.
-     */
-   public int getTheme() {
-      return (int)ctColor.getTheme();
-    }
-
-    /**
-     * Index into the <clrScheme> collection, referencing a particular <sysClr> or
-     *  <srgbClr> value expressed in the Theme part.
-     */
-    public void setTheme(int theme) {
-        ctColor.setTheme(theme);
-    }
-
-    /**
      * Specifies the tint value applied to the ctColor.
      *
      * <p>
@@ -254,9 +190,7 @@ public class XSSFColor implements Color
      *
      * @return the tint value
      */
-    public double getTint() {
-        return ctColor.getTint();
-    }
+    public abstract double getTint();
 
     /**
      * Specifies the tint value applied to the ctColor.
@@ -299,35 +233,5 @@ public class XSSFColor implements Color
      *
      * @param tint the tint value
      */
-    public void setTint(double tint) {
-        ctColor.setTint(tint);
-    }
-
-    /**
-     * Returns the underlying XML bean
-     *
-     * @return the underlying XML bean
-     */
-    @Internal
-    public CTColor getCTColor(){
-        return ctColor;
-    }
-
-    public static XSSFColor toXSSFColor(Color color) {
-        if (color != null && !(color instanceof XSSFColor)) {
-            throw new IllegalArgumentException("Only XSSFColor objects are supported");
-        }
-        return (XSSFColor)color;
-    }
-    
-    public int hashCode(){
-        return ctColor.toString().hashCode();
-    }
-
-    public boolean equals(Object o){
-        if(o == null || !(o instanceof XSSFColor)) return false;
-
-        XSSFColor cf = (XSSFColor)o;
-        return ctColor.toString().equals(cf.getCTColor().toString());
-    }
+    public abstract void setTint(double tint);
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java?rev=1691864&r1=1691863&r2=1691864&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java Mon Jul 20 00:46:50 2015
@@ -16,16 +16,15 @@
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
-import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.usermodel.Color;
+import org.apache.poi.ss.usermodel.ExtendedColor;
 import org.apache.poi.util.Internal;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 
 /**
  * Represents a color in SpreadsheetML
  */
-public class XSSFColor implements Color {
-
+public class XSSFColor extends ExtendedColor {
     private CTColor ctColor;
 
     /**
@@ -44,7 +43,7 @@ public class XSSFColor implements Color
 
     public XSSFColor(java.awt.Color clr) {
         this();
-        ctColor.setRgb(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
+        setColor(clr);
     }
 
     public XSSFColor(byte[] rgb) {
@@ -55,10 +54,10 @@ public class XSSFColor implements Color
     /**
      * A boolean value indicating the ctColor is automatic and system ctColor dependent.
      */
+    @Override
     public boolean isAuto() {
         return ctColor.getAuto();
     }
-
     /**
      * A boolean value indicating the ctColor is automatic and system ctColor dependent.
      */
@@ -67,11 +66,41 @@ public class XSSFColor implements Color
     }
 
     /**
+     * A boolean value indicating the ctColor is Indexed
+     */
+    @Override
+    public boolean isIndexed() {
+        return ctColor.isSetIndexed();
+    }
+
+    /**
+     * A boolean value indicating the ctColor is RGB or ARGB based
+     */
+    @Override
+    public boolean isRGB() {
+        return ctColor.isSetRgb();
+    }
+
+    /**
+     * A boolean value indicating the ctColor is Theme based
+     */
+    @Override
+    public boolean isThemed() {
+        return ctColor.isSetTheme();
+    }
+
+    /**
      * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
      */
-    public short getIndexed() {
+    public short getIndex() {
         return (short)ctColor.getIndexed();
     }
+    /**
+     * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+     */
+    public short getIndexed() {
+        return getIndex();
+    }
 
     /**
      * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
@@ -84,7 +113,7 @@ public class XSSFColor implements Color
     * Standard Red Green Blue ctColor value (RGB).
     * If there was an A (Alpha) value, it will be stripped.
     */
-   public byte[] getRgb() {
+   public byte[] getRGB() {
       byte[] rgb = getRGBOrARGB();
       if(rgb == null) return null;
 
@@ -97,11 +126,17 @@ public class XSSFColor implements Color
          return rgb;
       }
    }
+   /**
+    * @deprecated use {@link #getRGB()}
+    */
+   public byte[] getRgb() {
+       return getRGB();
+   }
 
    /**
     * Standard Alpha Red Green Blue ctColor value (ARGB).
     */
-   public byte[] getARgb() {
+   public byte[] getARGB() {
       byte[] rgb = getRGBOrARGB();
       if(rgb == null) return null;
 
@@ -115,85 +150,35 @@ public class XSSFColor implements Color
          return rgb;
       }
    }
+   /**
+    * @deprecated Use {@link #getARGB()}
+    */
+   public byte[] getARgb() {
+       return getARGB();
+   }
 
-   private byte[] getRGBOrARGB() {
-        byte[] rgb = null;
-
-        if (ctColor.isSetIndexed() && ctColor.getIndexed() > 0) {
-            HSSFColor indexed = HSSFColor.getIndexHash().get((int) ctColor.getIndexed());
-            if (indexed != null) {
-               rgb = new byte[3];
-               rgb[0] = (byte) indexed.getTriplet()[0];
-               rgb[1] = (byte) indexed.getTriplet()[1];
-               rgb[2] = (byte) indexed.getTriplet()[2];
-               return rgb;
-            }
-         }
-
-         if (!ctColor.isSetRgb()) {
-            // No colour is available, sorry
-            return null;
-         }
-
-         // Grab the colour
-         rgb = ctColor.getRgb();
-         return rgb;
-    }
+   protected byte[] getStoredRBG() {
+       return ctColor.getRgb();
+   }
 
     /**
      * Standard Red Green Blue ctColor value (RGB) with applied tint.
      * Alpha values are ignored.
      */
     public byte[] getRgbWithTint() {
-        byte[] rgb = ctColor.getRgb();
-        if (rgb != null) {
-            if(rgb.length == 4) {
-               byte[] tmp = new byte[3];
-               System.arraycopy(rgb, 1, tmp, 0, 3);
-               rgb = tmp;
-            }
-            for (int i = 0; i < rgb.length; i++){
-                rgb[i] = applyTint(rgb[i] & 0xFF, ctColor.getTint());
-            }
-        }
-        return rgb;
+        return getRGBWithTint();
     }
 
     /**
-     * Return the ARGB value in hex format, eg FF00FF00.
-     * Works for both regular and indexed colours.
-     */
-    public String getARGBHex() {
-       StringBuffer sb = new StringBuffer();
-       byte[] rgb = getARgb();
-       if(rgb == null) {
-          return null;
-       }
-       for(byte c : rgb) {
-          int i = c & 0xff;
-          String cs = Integer.toHexString(i);
-          if(cs.length() == 1) {
-             sb.append('0');
-          }
-          sb.append(cs);
-       }
-       return sb.toString().toUpperCase();
-    }
-
-    private static byte applyTint(int lum, double tint){
-        if(tint > 0){
-            return (byte)(lum * (1.0-tint) + (255 - 255 * (1.0-tint)));
-        } else if (tint < 0){
-            return (byte)(lum*(1+tint));
-        } else {
-            return (byte)lum;
-        }
+     * Standard Alpha Red Green Blue ctColor value (ARGB).
+     */
+    public void setRgb(byte[] rgb) {
+       setRGB(rgb);
     }
-
     /**
      * Standard Alpha Red Green Blue ctColor value (ARGB).
      */
-    public void setRgb(byte[] rgb) {
+    public void setRGB(byte[] rgb) {
        ctColor.setRgb(rgb);
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org