From b00b858ade286f5b7edfd9fc7f2515ef469145ba Mon Sep 17 00:00:00 2001 From: Jason Height Date: Mon, 24 Jul 2006 12:18:48 +0000 Subject: [PATCH] Bug 38486: Added handling of special 0x40 (64 dec) color. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@425023 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFCellStyle.java | 45 +++++++++++++++++-- .../apache/poi/hssf/usermodel/HSSFFont.java | 4 +- .../poi/hssf/usermodel/HSSFPalette.java | 15 ++++--- .../org/apache/poi/hssf/util/HSSFColor.java | 34 ++++++++++++++ 4 files changed, 88 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 32a3663629..ea4f3b293e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.record.ExtendedFormatRecord; +import org.apache.poi.hssf.util.*; /** * High level representation of the style of a cell in a sheet of a workbook. @@ -798,6 +799,29 @@ public class HSSFCellStyle { return format.getAdtlFillPattern(); } + + /** + * Checks if the background and foreground fills are set correctly when one + * or the other is set to the default color. + *

Works like the logic table below:

+ *

BACKGROUND FOREGROUND

+ *

NONE AUTOMATIC

+ *

0x41 0x40

+ *

NONE RED/ANYTHING

+ *

0x40 0xSOMETHING

+ */ + private void checkDefaultBackgroundFills() { + if (format.getFillForeground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) { + //JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt + //to me now.... But experience has shown that when the fore is set to AUTOMATIC then the + //background needs to be incremented...... + if (format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)) + setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)); + } else if (format.getFillBackground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1) + //Now if the forground changes to a non-AUTOMATIC color the background resets itself!!! + if (format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) + setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index); + } /** * set the background fill color. @@ -805,7 +829,14 @@ public class HSSFCellStyle * For example: *
      * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
-     * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
+     * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); 
+     * 
+ * optionally a Foreground and background fill can be applied: + * Note: Ensure Foreground color is set prior to background + *
+     * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
+     * cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
+     * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); 
      * 
* or, for the special case of SOLID_FILL: *
@@ -819,8 +850,9 @@ public class HSSFCellStyle
      */
 
     public void setFillBackgroundColor(short bg)
-    {
+    {    	
         format.setFillBackground(bg);
+        checkDefaultBackgroundFills();
     }
 
     /**
@@ -830,17 +862,24 @@ public class HSSFCellStyle
 
     public short getFillBackgroundColor()
     {
-        return format.getFillBackground();
+    	short result = format.getFillBackground();
+    	//JMH: Do this ridiculous conversion, and let HSSFCellStyle
+    	//internally migrate back and forth
+    	if (result == (HSSFColor.AUTOMATIC.index+1))
+    		return HSSFColor.AUTOMATIC.index;
+    	else return result;
     }
 
     /**
      * set the foreground fill color
+     * Note: Ensure Foreground color is set prior to background color.
      * @param bg  color
      */
 
     public void setFillForegroundColor(short bg)
     {
         format.setFillForeground(bg);
+        checkDefaultBackgroundFills();
     }
 
     /**
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
index 8d502d82ac..c200441d14 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
@@ -56,7 +56,7 @@ public class HSSFFont
     public final static short  BOLDWEIGHT_BOLD     = 0x2bc;
 
     /**
-     * normal type of black color
+     * normal type of black color.
      */
 
     public final static short  COLOR_NORMAL        = 0x7fff;
@@ -266,7 +266,7 @@ public class HSSFFont
     /**
      * set the color for the font
      * @param color to use
-     * @see #COLOR_NORMAL
+     * @see #COLOR_NORMAL Note: Use this rather than HSSFColor.AUTOMATIC for default font color
      * @see #COLOR_RED
      */
 
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java
index c6de0bb6d2..a5a2f49ceb 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java
@@ -44,11 +44,16 @@ public class HSSFPalette
      */
     public HSSFColor getColor(short index)
     {
-        byte[] b = palette.getColor(index);
-        if (b != null)
-        {
-            return new CustomColor(index, b);
-        }
+    	//Handle the special AUTOMATIC case
+    	if (index == HSSFColor.AUTOMATIC.index)
+    		return HSSFColor.AUTOMATIC.getInstance();
+    	else {
+          byte[] b = palette.getColor(index);
+          if (b != null)
+          {
+             return new CustomColor(index, b);
+          }
+    	}
         return null;
     }
     
diff --git a/src/java/org/apache/poi/hssf/util/HSSFColor.java b/src/java/org/apache/poi/hssf/util/HSSFColor.java
index 95df2bba78..b0621509b9 100644
--- a/src/java/org/apache/poi/hssf/util/HSSFColor.java
+++ b/src/java/org/apache/poi/hssf/util/HSSFColor.java
@@ -1693,4 +1693,38 @@ public class HSSFColor
             return hexString;
         }
     }
+    
+    /**
+     * Special Default/Normal/Automatic color.
+     * 

Note: This class is NOT in the default HashTables returned by HSSFColor. + * The index is a special case which is interpreted in the various setXXXColor calls. + * + * @author Jason + * + */ + public final static class AUTOMATIC extends HSSFColor + { + private static HSSFColor instance = new AUTOMATIC(); + + public final static short index = 0x40; + + public short getIndex() + { + return index; + } + + public short [] getTriplet() + { + return BLACK.triplet; + } + + public String getHexString() + { + return BLACK.hexString; + } + + public static HSSFColor getInstance() { + return instance; + } + } } -- 2.39.5