Browse Source

Bug 54373: Include alpha/transparency value when creating an XSSFColor from an AWT Color object

Use the alpha-value from Color as well

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907106 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_4
Dominik Stadler 1 year ago
parent
commit
edc7f8fd6f

+ 5
- 1
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTextRun.java View File

@@ -81,7 +81,11 @@ public class XSSFTextRun {
if(fill.isSetSrgbClr()){
CTSRgbColor clr = fill.getSrgbClr();
byte[] rgb = clr.getVal();
return new Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]);
if (rgb.length == 3) {
return new Color(0xFF & rgb[0], 0xFF & rgb[1], 0xFF & rgb[2]);
} else {
return new Color(0xFF & rgb[1], 0xFF & rgb[2], 0xFF & rgb[3], 0xFF & rgb[0]);
}
}
}


+ 2
- 2
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java View File

@@ -255,7 +255,7 @@ class TestXSSFDrawing {
assertTrue(rPr.getI());
assertEquals(STTextUnderlineType.SNG, rPr.getU());
assertArrayEquals(
new byte[]{0, (byte)128, (byte)128} ,
new byte[]{-1, 0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal());

checkRewrite(wb);
@@ -325,7 +325,7 @@ class TestXSSFDrawing {
CTTextCharacterProperties rPr = pr.getRArray(0).getRPr();
assertEquals("Arial", rPr.getLatin().getTypeface());
assertArrayEquals(
new byte[]{0, (byte)128, (byte)128} ,
new byte[]{-1, 0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal());
checkRewrite(wb);
wb.close();

+ 27
- 1
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFFont.java View File

@@ -20,8 +20,8 @@ package org.apache.poi.xssf.usermodel;
import static org.apache.poi.ss.usermodel.FontCharset.*;
import static org.junit.jupiter.api.Assertions.*;

import java.awt.Color;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

import org.apache.poi.common.usermodel.fonts.FontCharset;
@@ -536,6 +536,32 @@ public final class TestXSSFFont extends BaseTestFont {
}
}

@Test
public void testBug62272b() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("test");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);

// create a font with alpha
XSSFFont font = wb.createFont();
font.setColor(new XSSFColor(new Color(0x33, 0xCC, 0x66, 0x7f), null));

XSSFCellStyle style = wb.createCellStyle();
style.setFont(font);

cell.setCellStyle(style);
cell.setCellValue("testtext");

// make sure the alpha-value was stored properly
checkFontColor(wb);

/*try (OutputStream out = new FileOutputStream("/tmp/testout.xlsx")) {
wb.write(out);
}*/
}
}

private static void checkFontColor(Workbook wb) {
int fontIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFontIndex();
Font font = wb.getFontAt(fontIdx);

+ 1
- 1
poi/src/main/java/org/apache/poi/ss/usermodel/ExtendedColor.java View File

@@ -30,7 +30,7 @@ public abstract class ExtendedColor implements Color {
* @param clr awt Color to set
*/
protected void setColor(java.awt.Color clr) {
setRGB(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
setRGB(new byte[]{(byte)clr.getAlpha(), (byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
}

/**

Loading…
Cancel
Save