Example usage for org.apache.poi.hssf.usermodel HSSFPalette findColor

List of usage examples for org.apache.poi.hssf.usermodel HSSFPalette findColor

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFPalette findColor.

Prototype

public HSSFColor findColor(byte red, byte green, byte blue) 

Source Link

Document

Finds the first occurrence of a given color

Usage

From source file:com.ibm.ioes.bulkupload.utilities.ErrorLogServiceImpl.java

public HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;/* w ww .  ja v a2 s.  com*/
    try {
        hssfColor = palette.findColor(r, g, b);
        if (hssfColor == null) {
            palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g, b);
            hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
        }
    } catch (Exception e) {
        logger.error(e);
        Utility.LOG(true, false, e, "::BULKUPLOAD_ERROR:: Exception occured in setColor method of "
                + this.getClass().getSimpleName());
    }

    return hssfColor;
}

From source file:com.sevenorcas.openstyle.app.service.spreadsheet.SpreadSheet.java

/**
 * Set a color by substituting an used color
 * //w  w  w . j a  v  a2 s.co m
 * Thanks to http://stackoverflow.com/questions/10528516/poi-setting-background-color-to-a-cell
 * Thanks to http://stackoverflow.com/questions/842817/how-does-java-convert-int-into-byte
 * 
 * @param workbook
 * @param r
 * @param g
 * @param b
 * @return
 */
public HSSFColor setColor(HSSFWorkbook workbook, int r, int g, int b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;
    try {
        byte rb = (byte) (r);
        byte gb = (byte) (g);
        byte bb = (byte) (b);

        hssfColor = palette.findColor(rb, gb, bb);
        if (hssfColor == null) {
            short s = colorCustomers.getColorIndex(colorCustomers.getNextIndex());
            palette.setColorAtIndex(s, rb, gb, bb);
            hssfColor = palette.getColor(s);
        }
    } catch (Exception e) {
    }

    return hssfColor;
}

From source file:es.jamisoft.comun.io.excel.ExcelGenerator.java

License:Apache License

/**
 * Este mtodo gestiona el estilo que se proporcionar a las cabeceras.
 *
 * @param wb Objeto Excel./*w ww.  ja va2s.  c  o  m*/
 */
private void initStylesHeader(HSSFWorkbook wb) {

    // create instance of HSSFCellStyle
    HSSFCellStyle headerStyle = wb.createCellStyle();

    // Create Font Header
    createFontCell(wb, headerStyle, ep.getFillFontColorHeader());
    headerStyle.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    // Create style of header cell
    HSSFPalette palette = wb.getCustomPalette();
    HSSFColor colorMapfreHSSF = palette.findColor((byte) ep.getColorMapfre().getRed(),
            (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());

    if (colorMapfreHSSF == null) {
        palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) ep.getColorMapfre().getRed(),
                (byte) ep.getColorMapfre().getGreen(), (byte) ep.getColorMapfre().getBlue());
        colorMapfreHSSF = palette.getColor(HSSFColor.LAVENDER.index);
    }

    short fillBGColor = colorMapfreHSSF.getIndex();

    headerStyle.setFillForegroundColor(fillBGColor);
    headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // create border of header cell
    createBorderCells(headerStyle);

    // set
    ep.setHeaderStyle(headerStyle);
}

From source file:guineu.database.intro.impl.WriteFile.java

License:Open Source License

/**
 * Writes the basic data set into an excel file.
 *
 * @param dataset basic data set//from ww  w. j a v  a2 s. c  o  m
 * @param path Path where the new file will be created
 */
public void WriteXLSFileBasicDataset(Dataset dataset, String path) {
    FileOutputStream fileOut = null;
    try {
        HSSFSheet sheet;
        try {
            FileInputStream fileIn = new FileInputStream(path);
            POIFSFileSystem fs = new POIFSFileSystem(fileIn);
            wb = new HSSFWorkbook(fs);
            int NumberOfSheets = wb.getNumberOfSheets();
            sheet = wb.createSheet(String.valueOf(NumberOfSheets));
        } catch (Exception exception) {
            wb = new HSSFWorkbook();
            sheet = wb.createSheet("Page 1");
        }
        HSSFRow row = sheet.getRow(0);
        if (row == null) {
            row = sheet.createRow(0);
        }
        int cont = 0;
        for (String experimentName : dataset.getAllColumnNames()) {
            this.setCell(row, cont++, experimentName, null);

        }

        HSSFPalette palette = wb.getCustomPalette();
        Color[] colors = dataset.getRowColor();
        if (colors.length > 0) {
            for (int i = 0; i < dataset.getNumberRows(); i++) {
                palette.setColorAtIndex((short) 0x12, (byte) colors[i].getRed(), (byte) colors[i].getGreen(),
                        (byte) colors[i].getBlue());
                HSSFColor mycolor = palette.getColor((short) 0x12); //unmodified
                SimplePeakListRowOther lipid = (SimplePeakListRowOther) dataset.getRow(i);

                row = sheet.getRow(i + 1);
                if (row == null) {
                    row = sheet.createRow(i + 1);
                }
                int c = 0;
                for (String experimentName : dataset.getAllColumnNames()) {
                    if (lipid.getPeak(experimentName) == null) {
                        this.setCell(row, c++, "", mycolor);
                    } else {
                        this.setCell(row, c++, lipid.getPeak(experimentName), mycolor);
                    }
                }

            }
        } else {

            List<String> names = dataset.getAllColumnNames();
            for (int i = 0; i < dataset.getNumberRows(); i++) {
                SimplePeakListRowOther lipid = (SimplePeakListRowOther) dataset.getRow(i);
                row = sheet.getRow(i + 1);
                if (row == null) {
                    row = sheet.createRow(i + 1);
                }
                for (int j = 0; j < names.size(); j++) {
                    Color c = dataset.getCellColor(i, j + 1);
                    HSSFColor mycolor = null;
                    if (c != null) {
                        mycolor = palette.findColor((byte) c.getRed(), (byte) c.getGreen(), (byte) c.getBlue());
                    }
                    if (lipid.getPeak(names.get(j)) == null) {
                        this.setCell(row, j, "", null);
                    } else {
                        this.setCell(row, j, lipid.getPeak(names.get(j)), mycolor);
                    }
                }
            }
        }

        //Write the output to a file
        fileOut = new FileOutputStream(path);
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception exception) {
    }
}

From source file:jdbreport.model.io.xls.poi.Excel2003Writer.java

License:Apache License

protected short colorToIndex(Workbook wb, Color color) {
    if (color == null) {
        return 0;
    }/*from w  w w.ja  v a 2 s  . c o  m*/
    if (Color.black.equals(color)) {
        return IndexedColors.BLACK.getIndex();
    }
    if (Color.white.equals(color)) {
        return IndexedColors.WHITE.getIndex();
    }
    if (Color.blue.equals(color) || Color.blue.darker().equals(color) || Color.blue.brighter().equals(color)) {
        return IndexedColors.BLUE.getIndex();
    }
    if (Color.red.equals(color) || Color.red.darker().equals(color) || Color.red.brighter().equals(color)) {
        return IndexedColors.RED.getIndex();
    }
    if (Color.LIGHT_GRAY.equals(color)) {
        return IndexedColors.GREY_25_PERCENT.getIndex();
    }
    if (Color.GRAY.equals(color)) {
        return IndexedColors.GREY_50_PERCENT.getIndex();
    }
    if (Color.DARK_GRAY.equals(color)) {
        return IndexedColors.GREY_80_PERCENT.getIndex();
    }
    if (Color.green.equals(color) || Color.green.brighter().equals(color)
            || Color.green.darker().equals(color)) {
        return IndexedColors.GREEN.getIndex();
    }
    if (Color.magenta.equals(color) || Color.magenta.darker().equals(color)
            || Color.magenta.brighter().equals(color)) {
        return IndexedColors.MAROON.getIndex();
    }
    if (Color.orange.equals(color) || Color.orange.darker().equals(color)
            || Color.orange.brighter().equals(color)) {
        return IndexedColors.ORANGE.getIndex();
    }
    if (Color.pink.equals(color) || Color.pink.darker().equals(color) || Color.pink.brighter().equals(color)) {
        return IndexedColors.PINK.getIndex();
    }
    if (Color.yellow.equals(color) || Color.yellow.darker().equals(color)
            || Color.yellow.brighter().equals(color)) {
        return IndexedColors.YELLOW.getIndex();
    }

    byte r = (byte) color.getRed();
    byte g = (byte) color.getGreen();
    byte b = (byte) color.getBlue();
    HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
    HSSFColor hssColor = palette.findColor(r, g, b);

    try {
        if (hssColor == null) {
            hssColor = palette.addColor(r, g, b);
        }
        return hssColor.getIndex();
    } catch (RuntimeException e) {
        hssColor = palette.findSimilarColor(r, g, b);
        return hssColor != null ? hssColor.getIndex() : 0;
    }
}

From source file:org.apache.metamodel.excel.ExcelUpdateCallback.java

License:Apache License

public short getColorIndex(Color color) {
    Workbook workbook = getWorkbook(true);
    if (workbook instanceof HSSFWorkbook) {
        HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette();
        byte r = toRgb(color.getRed());
        byte g = toRgb(color.getGreen());
        byte b = toRgb(color.getBlue());

        HSSFColor index = palette.findColor(r, g, b);
        if (index == null) {
            index = palette.findSimilarColor(r, g, b);
        }// w  w w  .j av  a 2 s  . com
        return index.getIndex();
    }
    throw new IllegalStateException("Unexpected workbook type: " + workbook.getClass());
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.DynamicExcelColorProducer.java

License:Open Source License

public short getNearestColor(final Color awtColor) {
    if (lastUsedColor > 64) {
        // we ran out of palette... try to get nearest color then
        return StaticExcelColorSupport.getNearestColor(awtColor, usedTripplets);
    }//from  w ww.  ja  v a  2  s .  c o  m

    final HSSFPalette palette = workbook.getCustomPalette();
    final HSSFColor hssfColor = palette.findColor((byte) awtColor.getRed(), (byte) awtColor.getGreen(),
            (byte) awtColor.getBlue());

    if (hssfColor != null && hssfColor.getIndex() < lastUsedColor) {
        return hssfColor.getIndex();
    } else {
        palette.setColorAtIndex(lastUsedColor, (byte) awtColor.getRed(), (byte) awtColor.getGreen(),
                (byte) awtColor.getBlue());
        final HSSFColor color = palette.getColor(lastUsedColor);
        usedTripplets.put(color.getHexString(), color);
        return lastUsedColor++;
    }
}

From source file:org.sevenorcas.style.app.mod.ss.SpreadSheet.java

/**
 * Set a color by substituting an used color
 * // www  .j  a  v  a  2  s. c  om
 * Thanks to http://stackoverflow.com/questions/10528516/poi-setting-background-color-to-a-cell
 * Thanks to http://stackoverflow.com/questions/842817/how-does-java-convert-int-into-byte
 * 
 * @param workbook
 * @param r
 * @param g
 * @param b
 * @return
 */
public HSSFColor setColor(HSSFWorkbook workbook, int r, int g, int b) {
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor hssfColor = null;
    try {
        byte rb = (byte) (r);
        byte gb = (byte) (g);
        byte bb = (byte) (b);

        hssfColor = palette.findColor(rb, gb, bb);
        if (hssfColor == null && customColors != null) {
            short s = customColors.getColorIndex(customColors.getNextIndex());
            palette.setColorAtIndex(s, rb, gb, bb);
            hssfColor = palette.getColor(s);
        }
    } catch (Exception e) {
    }

    return hssfColor;
}

From source file:uk.co.spudsoft.birt.emitters.excel.StyleManagerHUtils.java

License:Open Source License

/**
 * Get an HSSFPalette index for a workbook that closely approximates the passed in colour.
 * @param workbook// ww  w .j a va  2 s.co  m
 * The workbook for which the colour is being sought.
 * @param colour
 * The colour, in the form "rgb(<i>r</i>, <i>g</i>, <i>b</i>)".
 * @return
 * The index into the HSSFPallete for the workbook for a colour that approximates the passed in colour.
 */
private short getHColour(HSSFWorkbook workbook, String colour) {
    int[] rgbInt = ColorUtil.getRGBs(colour);
    if (rgbInt == null) {
        return 0;
    }

    byte[] rgbByte = new byte[] { (byte) rgbInt[0], (byte) rgbInt[1], (byte) rgbInt[2] };
    HSSFPalette palette = workbook.getCustomPalette();

    HSSFColor result = palette.findColor(rgbByte[0], rgbByte[1], rgbByte[2]);
    if (result == null) {
        if (paletteIndex > minPaletteIndex) {
            --paletteIndex;
            palette.setColorAtIndex(paletteIndex, rgbByte[0], rgbByte[1], rgbByte[2]);
            return paletteIndex;
        } else {
            result = palette.findSimilarColor(rgbByte[0], rgbByte[1], rgbByte[2]);
        }
    }
    return result.getIndex();
}