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
This commit is contained in:
Jason Height 2006-07-24 12:18:48 +00:00
parent b7d6bfa980
commit b00b858ade
4 changed files with 88 additions and 10 deletions

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.record.ExtendedFormatRecord; 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. * High level representation of the style of a cell in a sheet of a workbook.
@ -799,6 +800,29 @@ public class HSSFCellStyle
return format.getAdtlFillPattern(); return format.getAdtlFillPattern();
} }
/**
* Checks if the background and foreground fills are set correctly when one
* or the other is set to the default color.
* <p>Works like the logic table below:</p>
* <p>BACKGROUND FOREGROUND</p>
* <p>NONE AUTOMATIC</p>
* <p>0x41 0x40</p>
* <p>NONE RED/ANYTHING</p>
* <p>0x40 0xSOMETHING</p>
*/
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. * set the background fill color.
* <p> * <p>
@ -807,6 +831,13 @@ public class HSSFCellStyle
* cs.setFillPattern(HSSFCellStyle.FINE_DOTS ); * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
* cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
* </pre> * </pre>
* optionally a Foreground and background fill can be applied:
* <i>Note: Ensure Foreground color is set prior to background</i>
* <pre>
* cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
* cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
* cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
* </pre>
* or, for the special case of SOLID_FILL: * or, for the special case of SOLID_FILL:
* <pre> * <pre>
* cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); * cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
@ -821,6 +852,7 @@ public class HSSFCellStyle
public void setFillBackgroundColor(short bg) public void setFillBackgroundColor(short bg)
{ {
format.setFillBackground(bg); format.setFillBackground(bg);
checkDefaultBackgroundFills();
} }
/** /**
@ -830,17 +862,24 @@ public class HSSFCellStyle
public short getFillBackgroundColor() 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 * set the foreground fill color
* <i>Note: Ensure Foreground color is set prior to background color.</i>
* @param bg color * @param bg color
*/ */
public void setFillForegroundColor(short bg) public void setFillForegroundColor(short bg)
{ {
format.setFillForeground(bg); format.setFillForeground(bg);
checkDefaultBackgroundFills();
} }
/** /**

View File

@ -56,7 +56,7 @@ public class HSSFFont
public final static short BOLDWEIGHT_BOLD = 0x2bc; public final static short BOLDWEIGHT_BOLD = 0x2bc;
/** /**
* normal type of black color * normal type of black color.
*/ */
public final static short COLOR_NORMAL = 0x7fff; public final static short COLOR_NORMAL = 0x7fff;
@ -266,7 +266,7 @@ public class HSSFFont
/** /**
* set the color for the font * set the color for the font
* @param color to use * @param color to use
* @see #COLOR_NORMAL * @see #COLOR_NORMAL Note: Use this rather than HSSFColor.AUTOMATIC for default font color
* @see #COLOR_RED * @see #COLOR_RED
*/ */

View File

@ -44,11 +44,16 @@ public class HSSFPalette
*/ */
public HSSFColor getColor(short index) public HSSFColor getColor(short index)
{ {
//Handle the special AUTOMATIC case
if (index == HSSFColor.AUTOMATIC.index)
return HSSFColor.AUTOMATIC.getInstance();
else {
byte[] b = palette.getColor(index); byte[] b = palette.getColor(index);
if (b != null) if (b != null)
{ {
return new CustomColor(index, b); return new CustomColor(index, b);
} }
}
return null; return null;
} }

View File

@ -1693,4 +1693,38 @@ public class HSSFColor
return hexString; return hexString;
} }
} }
/**
* Special Default/Normal/Automatic color.
* <p><i>Note:</i> 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;
}
}
} }