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
This commit is contained in:
Nick Burch 2010-05-26 17:46:46 +00:00
parent d8dd3f26e4
commit 63793d2fa5
9 changed files with 163 additions and 16 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-SNAPSHOT" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="add">48779 - Allow you to get straight from a CellStyle to a Color, irrespective of if the Color is indexed or inline-defined</action>
<action dev="POI-DEVELOPERS" type="add">48924 - Allow access of the HWPF DateAndTime underlying date values</action>
<action dev="POI-DEVELOPERS" type="add">48926 - Initial support for the HWPF revision marks authors list</action>
<action dev="POI-DEVELOPERS" type="fix">49160 - Ensure that CTDigSigBlob is included in poi-ooxml jar</action>

View File

@ -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

View File

@ -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 */

View File

@ -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

View File

@ -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 {
}

View File

@ -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.
*

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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 javax.swing.*;
import java.awt.*;
import java.util.*;
import static java.awt.Color.*;
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.JLabel;
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