From: Nick Burch Date: Wed, 26 May 2010 17:46:46 +0000 (+0000) Subject: Solution for bug #48779 - Allow you to get straight from a CellStyle to a Color,... X-Git-Tag: REL_3_7_BETA1~51 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=63793d2fa52b1aa70c4a833c76691a6db58304f4;p=poi.git Solution for bug #48779 - Allow you to get straight from a CellStyle to a Color, irrespective of if the Color is indexed or inline-defined git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@948511 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 05ed35d260..00170a430d 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 48779 - Allow you to get straight from a CellStyle to a Color, irrespective of if the Color is indexed or inline-defined 48924 - Allow access of the HWPF DateAndTime underlying date values 48926 - Initial support for the HWPF revision marks authors list 49160 - Ensure that CTDigSigBlob is included in poi-ooxml jar diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 92642b30eb..72ba1749e2 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -702,6 +702,15 @@ public final class HSSFCellStyle implements CellStyle { } return result; } + + public HSSFColor getFillBackgroundColorColor() { + HSSFPalette pallette = new HSSFPalette( + _workbook.getCustomPalette() + ); + return pallette.getColor( + getFillBackgroundColor() + ); + } /** * set the foreground fill color @@ -726,6 +735,15 @@ public final class HSSFCellStyle implements CellStyle { return _format.getFillForeground(); } + public HSSFColor getFillForegroundColorColor() { + HSSFPalette pallette = new HSSFPalette( + _workbook.getCustomPalette() + ); + return pallette.getColor( + getFillForegroundColor() + ); + } + /** * Gets the name of the user defined style. * Returns null for built in styles, and diff --git a/src/java/org/apache/poi/hssf/util/HSSFColor.java b/src/java/org/apache/poi/hssf/util/HSSFColor.java index 6c1e77ec6e..b640b37e1c 100644 --- a/src/java/org/apache/poi/hssf/util/HSSFColor.java +++ b/src/java/org/apache/poi/hssf/util/HSSFColor.java @@ -20,6 +20,8 @@ package org.apache.poi.hssf.util; import java.lang.reflect.Field; import java.util.Hashtable; +import org.apache.poi.ss.usermodel.Color; + /** * Intends to provide support for the very evil index to triplet issue and @@ -34,7 +36,7 @@ import java.util.Hashtable; * @author Andrew C. Oliver (acoliver at apache dot org) * @author Brian Sanders (bsanders at risklabs dot com) - full default color palette */ -public class HSSFColor { +public class HSSFColor implements Color { // TODO make subclass instances immutable /** Creates a new instance of HSSFColor */ diff --git a/src/java/org/apache/poi/ss/usermodel/CellStyle.java b/src/java/org/apache/poi/ss/usermodel/CellStyle.java index ec4fd3b377..7b62c1ee80 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/src/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -646,10 +646,20 @@ public interface CellStyle { void setFillBackgroundColor(short bg); /** - * get the background fill color - * @return fill color + * get the background fill color, if the fill + * is defined with an indexed color. + * @return fill color index, or 0 if not indexed (XSSF only) */ short getFillBackgroundColor(); + + /** + * Gets the color object representing the current + * background fill, resolving indexes using + * the supplied workbook. + * This will work for both indexed and rgb + * defined colors. + */ + Color getFillBackgroundColorColor(); /** * set the foreground fill color @@ -659,10 +669,20 @@ public interface CellStyle { void setFillForegroundColor(short bg); /** - * get the foreground fill color - * @return fill color + * get the foreground fill color, if the fill + * is defined with an indexed color. + * @return fill color, or 0 if not indexed (XSSF only) */ short getFillForegroundColor(); + + /** + * Gets the color object representing the current + * foreground fill, resolving indexes using + * the supplied workbook. + * This will work for both indexed and rgb + * defined colors. + */ + Color getFillForegroundColorColor(); /** * Clones all the style information from another diff --git a/src/java/org/apache/poi/ss/usermodel/Color.java b/src/java/org/apache/poi/ss/usermodel/Color.java new file mode 100644 index 0000000000..29555a1849 --- /dev/null +++ b/src/java/org/apache/poi/ss/usermodel/Color.java @@ -0,0 +1,21 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +public interface Color { +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 0bbbd5ff07..d2d17bd57b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -24,13 +24,13 @@ import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.util.Internal; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.ThemesTable; import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; -import org.apache.poi.util.Internal; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; @@ -381,6 +381,10 @@ public class XSSFCellStyle implements CellStyle { XSSFColor clr = getFillBackgroundXSSFColor(); return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed(); } + + public XSSFColor getFillBackgroundColorColor() { + return getFillBackgroundXSSFColor(); + } /** * Get the background fill color. @@ -418,6 +422,10 @@ public class XSSFCellStyle implements CellStyle { return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed(); } + public XSSFColor getFillForegroundColorColor() { + return getFillForegroundXSSFColor(); + } + /** * Get the foreground fill color. * 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 6ed2a40524..24bb35e2aa 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java @@ -17,12 +17,13 @@ package org.apache.poi.xssf.usermodel; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; +import org.apache.poi.ss.usermodel.Color; import org.apache.poi.util.Internal; /** * Represents a color in SpreadsheetML */ -public class XSSFColor { +public class XSSFColor implements Color { private CTColor ctColor; @@ -95,6 +96,30 @@ public class XSSFColor { } return rgb; } + + /** + * Return the ARGB value in hex format, eg FF00FF00. + * For indexed colours, returns null. + */ + public String getARGBHex() { + StringBuffer sb = new StringBuffer(); + byte[] rgb = getRgb(); + if(rgb == null) { + return null; + } + for(byte c : rgb) { + int i = (int)c; + if(i < 0) { + i += 256; + } + String cs = Integer.toHexString(i); + if(cs.length() == 1) { + sb.append('0'); + } + sb.append(cs); + } + return sb.toString().toUpperCase(); + } private static byte applyTint(int lum, double tint){ if(tint > 0){ @@ -239,5 +264,4 @@ public class XSSFColor { XSSFColor cf = (XSSFColor)o; return ctColor.toString().equals(cf.getCTColor().toString()); } - } 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 a5f19b3dbc..36747bd61d 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; public final class TestXSSFBugs extends BaseTestBugzillaIssues { @@ -224,4 +225,39 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Now all of them XSSFFormulaEvaluator.evaluateAllFormulaCells(wb); } + + /** + * Foreground colours should be found even if + * a theme is used + */ + public void test48779() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx"); + XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0); + XSSFCellStyle cs = cell.getCellStyle(); + + assertNotNull(cs); + assertEquals(1, cs.getIndex()); + + // Look at the low level xml elements + assertEquals(2, cs.getCoreXf().getFillId()); + assertEquals(0, cs.getCoreXf().getXfId()); + assertEquals(true, cs.getCoreXf().getApplyFill()); + + XSSFCellFill fg = wb.getStylesSource().getFillAt(2); + assertEquals(0, fg.getFillForegroundColor().getIndexed()); + assertEquals(0.0, fg.getFillForegroundColor().getTint()); + assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex()); + assertEquals(64, fg.getFillBackgroundColor().getIndexed()); + + // Now look higher up + assertNotNull(cs.getFillForegroundXSSFColor()); + assertEquals(0, cs.getFillForegroundColor()); + assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex()); + assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex()); + + assertNotNull(cs.getFillBackgroundColor()); + assertEquals(64, cs.getFillBackgroundColor()); + assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex()); + assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex()); + } } diff --git a/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java b/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java index b0fd8b9ab1..b8d6925120 100644 --- a/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java +++ b/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java @@ -16,16 +16,33 @@ ==================================================================== */ package org.apache.poi.ss.format; -import junit.framework.TestCase; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.ITestDataProvider; +import static java.awt.Color.BLACK; +import static java.awt.Color.BLUE; +import static java.awt.Color.CYAN; +import static java.awt.Color.GREEN; +import static java.awt.Color.MAGENTA; +import static java.awt.Color.ORANGE; +import static java.awt.Color.RED; +import static java.awt.Color.WHITE; +import static java.awt.Color.YELLOW; + +import java.awt.Color; +import java.io.IOException; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; -import javax.swing.*; -import java.awt.*; -import java.util.*; +import javax.swing.JLabel; -import static java.awt.Color.*; -import java.io.IOException; +import junit.framework.TestCase; + +import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; /** * This class is a base class for spreadsheet-based tests, such as are used for