1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.xssf.usermodel;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
24 import java.io.IOException;
26 import org.apache.poi.xssf.XSSFTestDataSamples;
27 import org.junit.Test;
28 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColors;
29 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRgbColor;
31 public final class TestXSSFColor {
34 public void testIndexedColour() throws Exception {
35 XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
37 // Check the CTColor is as expected
38 XSSFColor indexed = wb.getCellStyleAt(1).getFillBackgroundXSSFColor();
39 assertEquals(true, indexed.getCTColor().isSetIndexed());
40 assertEquals(64, indexed.getCTColor().getIndexed());
41 assertEquals(false, indexed.getCTColor().isSetRgb());
42 assertEquals(null, indexed.getCTColor().getRgb());
44 // Now check the XSSFColor
45 // Note - 64 is a special "auto" one with no rgb equiv
46 assertEquals(64, indexed.getIndexed());
47 assertEquals(null, indexed.getRGB());
48 assertEquals(null, indexed.getRGBWithTint());
49 assertEquals(null, indexed.getARGBHex());
50 assertFalse(indexed.hasAlpha());
51 assertFalse(indexed.hasTint());
53 // Now move to one with indexed rgb values
54 indexed.setIndexed(59);
55 assertEquals(true, indexed.getCTColor().isSetIndexed());
56 assertEquals(59, indexed.getCTColor().getIndexed());
57 assertEquals(false, indexed.getCTColor().isSetRgb());
58 assertEquals(null, indexed.getCTColor().getRgb());
60 assertEquals(59, indexed.getIndexed());
61 assertEquals("FF333300", indexed.getARGBHex());
63 assertEquals(3, indexed.getRGB().length);
64 assertEquals(0x33, indexed.getRGB()[0]);
65 assertEquals(0x33, indexed.getRGB()[1]);
66 assertEquals(0x00, indexed.getRGB()[2]);
68 assertEquals(4, indexed.getARGB().length);
69 assertEquals(-1, indexed.getARGB()[0]);
70 assertEquals(0x33, indexed.getARGB()[1]);
71 assertEquals(0x33, indexed.getARGB()[2]);
72 assertEquals(0x00, indexed.getARGB()[3]);
74 // You don't get tinted indexed colours, sorry...
75 assertEquals(null, indexed.getRGBWithTint());
81 public void testRGBColour() throws IOException {
82 XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx");
84 // Check the CTColor is as expected
85 XSSFColor rgb3 = wb.getCellStyleAt((short)25).getFillForegroundXSSFColor();
86 assertEquals(false, rgb3.getCTColor().isSetIndexed());
87 assertEquals(0, rgb3.getCTColor().getIndexed());
88 assertEquals(true, rgb3.getCTColor().isSetTint());
89 assertEquals(-0.34999, rgb3.getCTColor().getTint(), 0.00001);
90 assertEquals(true, rgb3.getCTColor().isSetRgb());
91 assertEquals(3, rgb3.getCTColor().getRgb().length);
93 // Now check the XSSFColor
94 assertEquals(0, rgb3.getIndexed());
95 assertEquals(-0.34999, rgb3.getTint(), 0.00001);
96 assertFalse(rgb3.hasAlpha());
97 assertTrue(rgb3.hasTint());
99 assertEquals("FFFFFFFF", rgb3.getARGBHex());
100 assertEquals(3, rgb3.getRGB().length);
101 assertEquals(-1, rgb3.getRGB()[0]);
102 assertEquals(-1, rgb3.getRGB()[1]);
103 assertEquals(-1, rgb3.getRGB()[2]);
105 assertEquals(4, rgb3.getARGB().length);
106 assertEquals(-1, rgb3.getARGB()[0]);
107 assertEquals(-1, rgb3.getARGB()[1]);
108 assertEquals(-1, rgb3.getARGB()[2]);
109 assertEquals(-1, rgb3.getARGB()[3]);
111 // Tint doesn't have the alpha
113 // 255 * (1 + tint) = 165 truncated
114 // or (byte) -91 (which is 165 - 256)
115 assertEquals(3, rgb3.getRGBWithTint().length);
116 assertEquals(-91, rgb3.getRGBWithTint()[0]);
117 assertEquals(-91, rgb3.getRGBWithTint()[1]);
118 assertEquals(-91, rgb3.getRGBWithTint()[2]);
120 // Set the color to black (no theme).
121 rgb3.setRGB(new byte[] {0, 0, 0});
122 assertEquals("FF000000", rgb3.getARGBHex());
123 assertEquals(0, rgb3.getCTColor().getRgb()[0]);
124 assertEquals(0, rgb3.getCTColor().getRgb()[1]);
125 assertEquals(0, rgb3.getCTColor().getRgb()[2]);
127 // Set another, is fine
128 rgb3.setRGB(new byte[] {16,17,18});
129 assertFalse(rgb3.hasAlpha());
130 assertEquals("FF101112", rgb3.getARGBHex());
131 assertEquals(0x10, rgb3.getCTColor().getRgb()[0]);
132 assertEquals(0x11, rgb3.getCTColor().getRgb()[1]);
133 assertEquals(0x12, rgb3.getCTColor().getRgb()[2]);
139 public void testARGBColour() throws IOException {
140 XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
142 // Check the CTColor is as expected
143 XSSFColor rgb4 = wb.getCellStyleAt((short)1).getFillForegroundXSSFColor();
144 assertEquals(false, rgb4.getCTColor().isSetIndexed());
145 assertEquals(0, rgb4.getCTColor().getIndexed());
146 assertEquals(true, rgb4.getCTColor().isSetRgb());
147 assertEquals(4, rgb4.getCTColor().getRgb().length);
149 // Now check the XSSFColor
150 assertEquals(0, rgb4.getIndexed());
151 assertEquals(0.0, rgb4.getTint(), 0);
152 assertFalse(rgb4.hasTint());
153 assertTrue(rgb4.hasAlpha());
155 assertEquals("FFFF0000", rgb4.getARGBHex());
156 assertEquals(3, rgb4.getRGB().length);
157 assertEquals(-1, rgb4.getRGB()[0]);
158 assertEquals(0, rgb4.getRGB()[1]);
159 assertEquals(0, rgb4.getRGB()[2]);
161 assertEquals(4, rgb4.getARGB().length);
162 assertEquals(-1, rgb4.getARGB()[0]);
163 assertEquals(-1, rgb4.getARGB()[1]);
164 assertEquals(0, rgb4.getARGB()[2]);
165 assertEquals(0, rgb4.getARGB()[3]);
167 // Tint doesn't have the alpha
168 assertEquals(3, rgb4.getRGBWithTint().length);
169 assertEquals(-1, rgb4.getRGBWithTint()[0]);
170 assertEquals(0, rgb4.getRGBWithTint()[1]);
171 assertEquals(0, rgb4.getRGBWithTint()[2]);
174 // Turn on tinting, and check it behaves
175 // TODO These values are suspected to be wrong...
177 assertTrue(rgb4.hasTint());
178 assertEquals(0.4, rgb4.getTint(), 0);
180 assertEquals(3, rgb4.getRGBWithTint().length);
181 assertEquals(-1, rgb4.getRGBWithTint()[0]);
182 assertEquals(102, rgb4.getRGBWithTint()[1]);
183 assertEquals(102, rgb4.getRGBWithTint()[2]);
189 public void testCustomIndexedColour() throws Exception {
190 XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("customIndexedColors.xlsx");
191 XSSFCell cell = wb.getSheetAt(1).getRow(0).getCell(0);
192 XSSFColor color = cell.getCellStyle().getFillForegroundColorColor();
193 CTColors ctColors = wb.getStylesSource().getCTStylesheet().getColors();
195 CTRgbColor ctRgbColor = ctColors.getIndexedColors()
197 .get(color.getIndex());
199 String hexRgb = ctRgbColor.getDomNode().getAttributes().getNamedItem("rgb").getNodeValue();
201 assertEquals(hexRgb, color.getARGBHex());