From cd57789b7f45258c831f0a6188564dc66a1618d9 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 18 Feb 2011 17:41:42 +0000 Subject: [PATCH] Fix bug #50786 - Fix XSSFColor to fetch the RGB values of old-style indexed colours git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1072082 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/usermodel/XSSFColor.java | 20 ++++++++++++++++-- .../poi/xssf/usermodel/TestXSSFBugs.java | 20 ++++++++++++++++++ .../spreadsheet/50786-indexed_colours.xlsx | Bin 0 -> 7537 bytes 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test-data/spreadsheet/50786-indexed_colours.xlsx diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index fbcb3cc42e..9bf3cf1da3 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 50786 - Fix XSSFColor to fetch the RGB values of old-style indexed colours 50299 - Fix XSSFColor fetching of white and black background themes 50795 - Avoid NPE from xmlbeans when moving XSSF Comments from one cell to another 46664 - When creating HSSF Print Areas, ensure the named range is reference based not value based 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 24bb35e2aa..2194a4db17 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Color; import org.apache.poi.util.Internal; @@ -83,7 +84,22 @@ public class XSSFColor implements Color { * Standard Alpha Red Green Blue ctColor value (ARGB). */ public byte[] getRgb() { - return ctColor.getRgb(); + if(ctColor.isSetIndexed() && ctColor.getIndexed() > 0) { + HSSFColor indexed = HSSFColor.getIndexHash().get((int)ctColor.getIndexed()); + if(indexed != null) { + // Convert it to ARGB form + byte[] rgb = new byte[4]; + rgb[0] = 0; + rgb[1] = (byte)indexed.getTriplet()[0]; + rgb[2] = (byte)indexed.getTriplet()[1]; + rgb[3] = (byte)indexed.getTriplet()[2]; + return rgb; + } else { + // Your indexed value isn't a standard one, sorry... + return null; + } + } + return ctColor.getRgb(); } /** @@ -99,7 +115,7 @@ public class XSSFColor implements Color { /** * Return the ARGB value in hex format, eg FF00FF00. - * For indexed colours, returns null. + * Works for both regular and indexed colours. */ public String getARGBHex() { StringBuffer sb = new StringBuffer(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 349ab9bd8b..b36a92a4dd 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -704,6 +704,26 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor()); } + /** + * Excel .xls style indexed colours in a .xlsx file + */ + public void test50786() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx"); + XSSFSheet s = wb.getSheetAt(0); + XSSFRow r = s.getRow(2); + + // Check we have the right cell + XSSFCell c = r.getCell(1); + assertEquals("test\u00a0", c.getRichStringCellValue().getString()); + + // It should be light green + XSSFCellStyle cs = c.getCellStyle(); + assertEquals(42, cs.getFillForegroundColor()); + assertEquals(42, cs.getFillForegroundColorColor().getIndexed()); + assertNotNull(cs.getFillForegroundColorColor().getRgb()); + assertEquals("00CCFFCC", cs.getFillForegroundColorColor().getARGBHex()); + } + /** * Fonts where their colours come from the theme rather * then being set explicitly still should allow the diff --git a/test-data/spreadsheet/50786-indexed_colours.xlsx b/test-data/spreadsheet/50786-indexed_colours.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..df8bab6cd64cde2935ab2401a6f9d03adb64456f GIT binary patch literal 7537 zcmeHMg|@C}|`|U_kO4 z@4dg@y_f6v4}9O<^PIDvdG=XrW}p4Od#&|uB{>vSA^dq^f9wsEsSYT1@({Gp!|-D& zS?mUo?tiHTQa>+3_4KI{icssRZ-I_qi>3v<@&V?8Gz04T4?6@5V0?1Jo zTfX$2w*`UWAnr-zwG(qkdZ$CwOnVF7F30a3$gzUaLu$EZlRQ!L!m{D$R}MNnnZ5fL zki=#pHfggux>sD&ibOj~N5I#NLA$=u$zm@)NuhsJE-nDG#DA9D5M?8h6x zqA&f*@J0im`@~&g5t}+NKX&HsRL`6=r9K(lW+rYVI+5l+$H*RNM{$g0$E68B!Dk|^ zkU-gfiW%Ey${vL&3J3EY$FAt(t|2rlmjgq|*;YmH*8_`PSg?pbl}0J*Mgn10f6ep} zGdw!di{uf5uvoaR8FU*3vv*%C#T!AYn;R5>(%-2Q)Q_J4+6;jtqKV&QZj$h1hTU3{~V!N_^vEHBf1UzNI@VV|ZhB9B4*6dr)$ z2Qq?=!8b>qXfN(>yiuj0G~`z7Y+`v@#c${_ZQ)v;^$I`Rkk0H4Q4e_}rGfgwqLrR& zeteqY^hHs1SQvRz{3X4i0M!d3Y}l^-`xqYOegpHdxO+t=^YQDVL2cAZoFbDD3k;wK zute-!wp|d{r<$Gk|=?SJgu-+dr}HD;A%0 zj2LM`V4WO*f#hn%{X5)U!1k7gV6f%S;PMCbQGY@o!I=Nwtt@r~)Wt*4ec*RMuwgS% zK}VOiFBQ;g;pG!8pH^*6$hVsHsWm4T8fembOn7G6dwRu}vbS$Z*#%_!=C7_q%!qN! zG_&Y}wV1k@1|;a3(-uXI!eU~WnVeRKl2Qt6%B`Kt&^5chTuxrii#Mi>ObsO%+f<|& zFM2rerLSn{sSAsci5vICILf!XFRB`_#upd;%nOp^Zcg#6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