Hopefully fix bug #51236 - make the xssf colour black/white 3 rgb fix apply for set as well as get

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1126696 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-05-23 19:55:00 +00:00
parent 8144d5e1ae
commit ab6caf7f49
4 changed files with 44 additions and 17 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.8-beta3" date="2011-??-??"> <release version="3.8-beta3" date="2011-??-??">
<action dev="poi-developers" type="fix">51236 - XSSF set colour support for black/white to match getter</action>
<action dev="poi-developers" type="add">51196 - Initial support for Spreadsheet Chart API</action> <action dev="poi-developers" type="add">51196 - Initial support for Spreadsheet Chart API</action>
<action dev="poi-developers" type="add">Add support for OOXML Agile Encryption</action> <action dev="poi-developers" type="add">Add support for OOXML Agile Encryption</action>
<action dev="poi-developers" type="add">51160 - Initial version of SXSSF, a low memory foortprint API to produce xlsx files</action> <action dev="poi-developers" type="add">51160 - Initial version of SXSSF, a low memory foortprint API to produce xlsx files</action>

View File

@ -87,7 +87,8 @@ public class ThemesTable extends POIXMLDocumentPart {
// Get the theme colour // Get the theme colour
XSSFColor themeColor = getThemeColor(color.getTheme()); XSSFColor themeColor = getThemeColor(color.getTheme());
// Set the raw colour, not the adjusted one // Set the raw colour, not the adjusted one
color.setRgb(themeColor.getCTColor().getRgb()); // Do a raw set, no adjusting at the XSSFColor layer either
color.getCTColor().setRgb(themeColor.getCTColor().getRgb());
// All done // All done
} }

View File

@ -79,6 +79,27 @@ public class XSSFColor implements Color {
public void setIndexed(int indexed) { public void setIndexed(int indexed) {
ctColor.setIndexed(indexed); ctColor.setIndexed(indexed);
} }
/**
* For RGB colours, but not ARGB (we think...)
* Excel gets black and white the wrong way around, so switch them
*/
private byte[] correctRGB(byte[] rgb) {
if(rgb.length == 4) {
// Excel doesn't appear to get these wrong
// Nothing to change
return rgb;
} else {
// Excel gets black and white the wrong way around, so switch them
if (rgb[0] == 0 && rgb[1] == 0 && rgb[2] == 0) {
rgb = new byte[] {-1, -1, -1};
}
else if (rgb[0] == -1 && rgb[1] == -1 && rgb[2] == -1) {
rgb = new byte[] {0, 0, 0};
}
return rgb;
}
}
/** /**
* Standard Red Green Blue ctColor value (RGB). * Standard Red Green Blue ctColor value (RGB).
@ -138,20 +159,8 @@ public class XSSFColor implements Color {
// Grab the colour // Grab the colour
rgb = ctColor.getRgb(); rgb = ctColor.getRgb();
if(rgb.length == 4) { // Correct it as needed, and return
// Good to go, return it as-is return correctRGB(rgb);
return rgb;
}
// For RGB colours, but not ARGB (we think...)
// Excel gets black and white the wrong way around, so switch them
if (rgb[0] == 0 && rgb[1] == 0 && rgb[2] == 0) {
rgb = new byte[] {-1, -1, -1};
}
else if (rgb[0] == -1 && rgb[1] == -1 && rgb[2] == -1) {
rgb = new byte[] {0, 0, 0};
}
return rgb;
} }
/** /**
@ -211,9 +220,10 @@ public class XSSFColor implements Color {
* Standard Alpha Red Green Blue ctColor value (ARGB). * Standard Alpha Red Green Blue ctColor value (ARGB).
*/ */
public void setRgb(byte[] rgb) { public void setRgb(byte[] rgb) {
ctColor.setRgb(rgb); // Correct it and save
ctColor.setRgb(correctRGB(rgb));
} }
/** /**
* Index into the <clrScheme> collection, referencing a particular <sysClr> or * Index into the <clrScheme> collection, referencing a particular <sysClr> or
* <srgbClr> value expressed in the Theme part. * <srgbClr> value expressed in the Theme part.

View File

@ -97,6 +97,21 @@ public final class TestXSSFColor extends TestCase {
assertEquals(0, rgb3.getRgbWithTint()[0]); assertEquals(0, rgb3.getRgbWithTint()[0]);
assertEquals(0, rgb3.getRgbWithTint()[1]); assertEquals(0, rgb3.getRgbWithTint()[1]);
assertEquals(0, rgb3.getRgbWithTint()[2]); assertEquals(0, rgb3.getRgbWithTint()[2]);
// Set the colour to black, will get translated internally
// (Excel stores 3 colour white and black wrong!)
rgb3.setRgb(new byte[] {-1,-1,-1});
assertEquals("FFFFFFFF", rgb3.getARGBHex());
assertEquals(0, rgb3.getCTColor().getRgb()[0]);
assertEquals(0, rgb3.getCTColor().getRgb()[1]);
assertEquals(0, rgb3.getCTColor().getRgb()[2]);
// Set another, is fine
rgb3.setRgb(new byte[] {16,17,18});
assertEquals("FF101112", rgb3.getARGBHex());
assertEquals(0x10, rgb3.getCTColor().getRgb()[0]);
assertEquals(0x11, rgb3.getCTColor().getRgb()[1]);
assertEquals(0x12, rgb3.getCTColor().getRgb()[2]);
} }
public void testARGBColour() throws Exception { public void testARGBColour() throws Exception {