]> source.dussan.org Git - poi.git/commitdiff
[bug-62908] populate NamedColors
authorPJ Fanning <fanningpj@apache.org>
Tue, 13 Nov 2018 16:08:11 +0000 (16:08 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 13 Nov 2018 16:08:11 +0000 (16:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1846520 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/format/CellFormatPart.java
src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java

index 2651edd8c660fe3b748aa83a98b6b8bf4880428e..e56aa3d8678dc6105bb005e800d65c02a7a30b47 100644 (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);
         }
     }
 
index bc66fc4463dc85db1e8d63817df77a8b059d0014..583fa8b851dc94c5b14fec7075db13abde46237c 100644 (file)
 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())