]> source.dussan.org Git - poi.git/commitdiff
Fix bug #50786 - Fix XSSFColor to fetch the RGB values of old-style indexed colours
authorNick Burch <nick@apache.org>
Fri, 18 Feb 2011 17:41:42 +0000 (17:41 +0000)
committerNick Burch <nick@apache.org>
Fri, 18 Feb 2011 17:41:42 +0000 (17:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1072082 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/50786-indexed_colours.xlsx [new file with mode: 0644]

index fbcb3cc42e6eaff4c9d063ffff11f3549fb3dd22..9bf3cf1da382bbbd3862d12069530671a0f299b3 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta1" date="2010-??-??">
+           <action dev="poi-developers" type="fix">50786 - Fix XSSFColor to fetch the RGB values of old-style indexed colours</action>
            <action dev="poi-developers" type="fix">50299 - Fix XSSFColor fetching of white and black background themes</action>
            <action dev="poi-developers" type="fix">50795 - Avoid NPE from xmlbeans when moving XSSF Comments from one cell to another</action>
            <action dev="poi-developers" type="fix">46664 - When creating HSSF Print Areas, ensure the named range is reference based not value based</action>
index 24bb35e2aab061003fa5e11fce973a246a8301f0..2194a4db17b2e8dceb4fb6752e3088937b4e08db 100644 (file)
@@ -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();
index 349ab9bd8b62e8d97c86d92b93bfb0e9f886c2a7..b36a92a4ddad64b0f7b4e6176f55c663a6bd7502 100644 (file)
@@ -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 (file)
index 0000000..df8bab6
Binary files /dev/null and b/test-data/spreadsheet/50786-indexed_colours.xlsx differ