poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 51236] Font color set incorrectly for black and white color using RGB values
Date Wed, 25 May 2011 15:52:04 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=51236

--- Comment #8 from jxz164@hotmail.com 2011-05-25 15:52:04 UTC ---
Your recent changes of correctRGB to reverse black and white are for rgb.length
= 3. In my code, I have worked around it by adding alpha value. Please see my
code attached.

Now the problem is when reading the xlsx file created back, the color is wrong.

package org.apache.poi.ss.examples;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFFont;

import java.io.FileOutputStream;

public class TestFontColor {
    protected static XSSFColor getXSSFColor(String RGB) {

        int red = Integer.parseInt(RGB.substring(0,2), 16);
        int green = Integer.parseInt(RGB.substring(2,4), 16);
        int blue = Integer.parseInt(RGB.substring(4,6), 16);

         //add alpha to avoid bug 51236
        byte[] rgb = new byte[] { (byte) -1, (byte) red, (byte) green, (byte)
blue };

        return new XSSFColor(rgb);
    }

    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();  //or new HSSFWorkbook();
        Sheet sheet = wb.createSheet("Fonts");

        Font font0 = wb.createFont();
        Font font1 = wb.createFont();

        XSSFColor color_white = getXSSFColor("FFFFFF");
        XSSFColor color_black = getXSSFColor("000000");

        ((XSSFFont) font0).setColor(color_white); //FFFFFF returns black
        ((XSSFFont) font1).setColor(color_black); //000000 returns white

        font0.setFontHeightInPoints((short) 18);
        CellStyle style0 = wb.createCellStyle();
        CellStyle style1 = wb.createCellStyle();
        style0.setFont(font0);
        style1.setFont(font1);

        Cell cell0 = sheet.createRow(0).createCell(0);
        cell0.setCellValue("FFFFFF");
        cell0.setCellStyle(style0);

        Cell cell1 = sheet.createRow(1).createCell(0);
        cell1.setCellValue("000000");
        cell1.setCellStyle(style1);

        // Write the output to a file
        FileOutputStream fileOut = new
FileOutputStream("C:\\workspace\\TestFontColor.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }
}

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Mime
View raw message