[bug-62908] populate NamedColors

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1846520 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-11-13 16:08:11 +00:00
parent 3885cb0839
commit d64003436f
2 changed files with 27 additions and 15 deletions

View File

@ -54,26 +54,22 @@ public class CellFormatPart {
private final CellFormatter format;
private final CellFormatType type;
private static final Map<String, Color> NAMED_COLORS;
static final Map<String, Color> NAMED_COLORS;
static {
NAMED_COLORS = new TreeMap<>(
String.CASE_INSENSITIVE_ORDER);
Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash();
for (HSSFColor color : colors.values()) {
Class<? extends HSSFColor> type = color.getClass();
String name = type.getSimpleName();
if (name.equals(name.toUpperCase(Locale.ROOT))) {
short[] rgb = color.getTriplet();
Color c = new Color(rgb[0], rgb[1], rgb[2]);
NAMED_COLORS.put(name, c);
if (name.indexOf('_') > 0)
NAMED_COLORS.put(name.replace('_', ' '), c);
if (name.indexOf("_PERCENT") > 0)
NAMED_COLORS.put(name.replace("_PERCENT", "%").replace('_',
' '), c);
}
for (HSSFColor.HSSFColorPredefined color : HSSFColor.HSSFColorPredefined.values()) {
String name = color.name();
short[] rgb = color.getTriplet();
Color c = new Color(rgb[0], rgb[1], rgb[2]);
NAMED_COLORS.put(name, c);
if (name.indexOf('_') > 0)
NAMED_COLORS.put(name.replace('_', ' '), c);
if (name.indexOf("_PERCENT") > 0)
NAMED_COLORS.put(name.replace("_PERCENT", "%").replace('_',
' '), c);
}
}

View File

@ -17,12 +17,15 @@
package org.apache.poi.ss.format;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.XSSFITestDataProvider;
@ -153,6 +156,19 @@ public class TestCellFormatPart extends CellFormatTestBase {
});
}
@Test
public void testNamedColors() {
assertTrue(CellFormatPart.NAMED_COLORS.size() >= HSSFColor.HSSFColorPredefined.values().length);
assertNotNull(CellFormatPart.NAMED_COLORS.get("GREEN"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("Green"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("RED"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("Red"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("BLUE"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("Blue"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("YELLOW"));
assertNotNull(CellFormatPart.NAMED_COLORS.get("Yellow"));
}
private double extractNumber(String str) {
Matcher m = NUMBER_EXTRACT_FMT.matcher(str);
if (!m.find())