From 6cd7769206685dceae269f340db9abb81030b504 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 23 May 2011 19:55:00 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/model/ThemesTable.java | 3 +- .../apache/poi/xssf/usermodel/XSSFColor.java | 42 ++++++++++++------- .../poi/xssf/usermodel/TestXSSFColor.java | 15 +++++++ 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 2c3ab3fc6f..cd9a6ede28 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51236 - XSSF set colour support for black/white to match getter 51196 - Initial support for Spreadsheet Chart API Add support for OOXML Agile Encryption 51160 - Initial version of SXSSF, a low memory foortprint API to produce xlsx files diff --git a/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java index 562837157a..61ed79a325 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java @@ -87,7 +87,8 @@ public class ThemesTable extends POIXMLDocumentPart { // Get the theme colour XSSFColor themeColor = getThemeColor(color.getTheme()); // 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 } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java index 46b2ef2361..b15eaa55ba 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java @@ -79,6 +79,27 @@ public class XSSFColor implements Color { public void setIndexed(int 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). @@ -138,20 +159,8 @@ public class XSSFColor implements Color { // Grab the colour rgb = ctColor.getRgb(); - if(rgb.length == 4) { - // Good to go, return it as-is - 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; + // Correct it as needed, and return + return correctRGB(rgb); } /** @@ -211,9 +220,10 @@ public class XSSFColor implements Color { * Standard Alpha Red Green Blue ctColor value (ARGB). */ public void setRgb(byte[] rgb) { - ctColor.setRgb(rgb); + // Correct it and save + ctColor.setRgb(correctRGB(rgb)); } - + /** * Index into the collection, referencing a particular or * value expressed in the Theme part. diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFColor.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFColor.java index 60dfb4f97d..b2aa11447b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFColor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFColor.java @@ -97,6 +97,21 @@ public final class TestXSSFColor extends TestCase { assertEquals(0, rgb3.getRgbWithTint()[0]); assertEquals(0, rgb3.getRgbWithTint()[1]); 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 { -- 2.39.5