Examples of HSSFPalette


Examples of org.apache.poi.hssf.usermodel.HSSFPalette

     * Uses the palette from cell stylings
     */
    public void testPaletteFromCellColours() {
        HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("SimpleWithColours.xls");

        HSSFPalette p = book.getCustomPalette();

        HSSFCell cellA = book.getSheetAt(0).getRow(0).getCell(0);
        HSSFCell cellB = book.getSheetAt(0).getRow(1).getCell(0);
        HSSFCell cellC = book.getSheetAt(0).getRow(2).getCell(0);
        HSSFCell cellD = book.getSheetAt(0).getRow(3).getCell(0);
        HSSFCell cellE = book.getSheetAt(0).getRow(4).getCell(0);

        // Plain
        assertEquals("I'm plain", cellA.getStringCellValue());
        assertEquals(64, cellA.getCellStyle().getFillForegroundColor());
        assertEquals(64, cellA.getCellStyle().getFillBackgroundColor());
        assertEquals(HSSFFont.COLOR_NORMAL, cellA.getCellStyle().getFont(book).getColor());
        assertEquals(0, cellA.getCellStyle().getFillPattern());
        assertEquals("0:0:0", p.getColor((short)64).getHexString());
        assertEquals(null, p.getColor((short)32767));

        // Red
        assertEquals("I'm red", cellB.getStringCellValue());
        assertEquals(64, cellB.getCellStyle().getFillForegroundColor());
        assertEquals(64, cellB.getCellStyle().getFillBackgroundColor());
        assertEquals(10, cellB.getCellStyle().getFont(book).getColor());
        assertEquals(0, cellB.getCellStyle().getFillPattern());
        assertEquals("0:0:0", p.getColor((short)64).getHexString());
        assertEquals("FFFF:0:0", p.getColor((short)10).getHexString());

        // Red + green bg
        assertEquals("I'm red with a green bg", cellC.getStringCellValue());
        assertEquals(11, cellC.getCellStyle().getFillForegroundColor());
        assertEquals(64, cellC.getCellStyle().getFillBackgroundColor());
        assertEquals(10, cellC.getCellStyle().getFont(book).getColor());
        assertEquals(1, cellC.getCellStyle().getFillPattern());
        assertEquals("0:FFFF:0", p.getColor((short)11).getHexString());
        assertEquals("FFFF:0:0", p.getColor((short)10).getHexString());

        // Pink with yellow
        assertEquals("I'm pink with a yellow pattern (none)", cellD.getStringCellValue());
        assertEquals(13, cellD.getCellStyle().getFillForegroundColor());
        assertEquals(64, cellD.getCellStyle().getFillBackgroundColor());
        assertEquals(14, cellD.getCellStyle().getFont(book).getColor());
        assertEquals(0, cellD.getCellStyle().getFillPattern());
        assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString());
        assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString());

        // Pink with yellow - full
        assertEquals("I'm pink with a yellow pattern (full)", cellE.getStringCellValue());
        assertEquals(13, cellE.getCellStyle().getFillForegroundColor());
        assertEquals(64, cellE.getCellStyle().getFillBackgroundColor());
        assertEquals(14, cellE.getCellStyle().getFont(book).getColor());
        assertEquals(0, cellE.getCellStyle().getFillPattern());
        assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString());
        assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString());
    }
View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

        assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString());
    }

    public void testFindSimilar() {
        HSSFWorkbook book = new HSSFWorkbook();
        HSSFPalette p = book.getCustomPalette();
       
        /* first test the defaults */
        assertTrue(
            Arrays.equals(
                new short[] {(short) 255, (short) 255, (short) 0}, // not [204, 255, 255]
                p.findSimilarColor((byte) 204, (byte) 255, (byte) 0).getTriplet()
            )
        );

        assertTrue(
            Arrays.equals(
                new short[] {(short) 153, (short) 204, (short) 0}, // not [128, 0, 0]
                p.findSimilarColor((byte) 128, (byte) 255, (byte) 0).getTriplet()
            )
        );

        assertTrue(
            Arrays.equals(
                new short[] {(short) 0, (short) 255, (short) 0}, // not [0, 51, 102]
                p.findSimilarColor((byte) 0, (byte) 255, (byte) 102).getTriplet()
            )
        );

        assertTrue(
            Arrays.equals(
                new short[] {(short) 0, (short) 102, (short) 204}, // not [255, 102, 0]
                p.findSimilarColor((byte) 0, (byte) 102, (byte) 255).getTriplet()
            )
        );

        assertTrue(
            Arrays.equals(
                new short[] {(short) 255, (short) 0, (short) 255}, // not [128, 0, 0]
                p.findSimilarColor((byte) 128, (byte) 0, (byte) 255).getTriplet()
            )
        );

        assertTrue(
            Arrays.equals(
                new short[] {(short) 255, (short) 0, (short) 255}, // not [255, 255, 153]
                p.findSimilarColor((byte) 255, (byte) 0, (byte) 153).getTriplet()
            )
        );


        // Add a few edge colours in
        p.setColorAtIndex((short)8, (byte)-1, (byte)0, (byte)0);
        p.setColorAtIndex((short)9, (byte)0, (byte)-1, (byte)0);
        p.setColorAtIndex((short)10, (byte)0, (byte)0, (byte)-1);

        // And some near a few of them
        p.setColorAtIndex((short)11, (byte)-1, (byte)2, (byte)2);
        p.setColorAtIndex((short)12, (byte)-2, (byte)2, (byte)10);
        p.setColorAtIndex((short)13, (byte)-4, (byte)0, (byte)0);
        p.setColorAtIndex((short)14, (byte)-8, (byte)0, (byte)0);

        assertEquals(
                "FFFF:0:0", p.getColor((short)8).getHexString()
        );

        // Now check we get the right stuff back
        assertEquals(
                p.getColor((short)8).getHexString(),
                p.findSimilarColor((byte)-1, (byte)0, (byte)0).getHexString()
        );
        assertEquals(
                p.getColor((short)8).getHexString(),
                p.findSimilarColor((byte)-2, (byte)0, (byte)0).getHexString()
        );
        assertEquals(
                p.getColor((short)8).getHexString(),
                p.findSimilarColor((byte)-1, (byte)1, (byte)0).getHexString()
        );
        assertEquals(
                p.getColor((short)11).getHexString(),
                p.findSimilarColor((byte)-1, (byte)2, (byte)1).getHexString()
        );
        assertEquals(
                p.getColor((short)12).getHexString(),
                p.findSimilarColor((byte)-1, (byte)2, (byte)10).getHexString()
        );
       
        // And with ints not bytes
        assertEquals(
                p.getColor((short)11).getHexString(),
                p.findSimilarColor(255, 2, 1).getHexString()
        );
        assertEquals(
                p.getColor((short)12).getHexString(),
                p.findSimilarColor(255, 2, 10).getHexString()
        );
    }
View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

     */
    private short getColorIndex(Color swtColor) {
        if (!colorIndex.contains(swtColor)) {
            colorIndex.add(swtColor);

            HSSFPalette palette = ((HSSFWorkbook) xlWorkbook)
                    .getCustomPalette();

            palette.setColorAtIndex(
                    (short) (55 - colorIndex.indexOf(swtColor)),
                    (byte) swtColor.getRed(), (byte) swtColor.getGreen(),
                    (byte) swtColor.getBlue());
        }

View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

  private HSSFColor getHSSFColor(final HSSFWorkbook workbook, final Color color) {
    byte red = (byte) color.getRed();
    byte green = (byte) color.getGreen();
    byte blue = (byte) color.getBlue();
    HSSFPalette palette = workbook.getCustomPalette();

    HSSFColor c = palette.findSimilarColor(red, green, blue);
    if (c == null) {
      try {
        c = palette.addColor(red, green, blue);
      } catch (RuntimeException e) {
        c = palette.findSimilarColor(red, green, blue);
      }
    }
    return c;
  }
View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

  private HSSFColor getHSSFColor(final HSSFWorkbook workbook, final Color color) {
    byte red = (byte) color.getRed();
    byte green = (byte) color.getGreen();
    byte blue = (byte) color.getBlue();
    HSSFPalette palette = workbook.getCustomPalette();

    HSSFColor c = palette.findSimilarColor(red, green, blue);
    if (c == null) {
      try {
        c = palette.addColor(red, green, blue);
      } catch (RuntimeException e) {
        c = palette.findSimilarColor(red, green, blue);
      }
    }
    return c;
  }
View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

        standardHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        standardHeader.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        standardHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        // Change PALE_BLUE to actually pale blue
        HSSFPalette palette = workbook.getCustomPalette();
        palette.setColorAtIndex(HSSFColor.PALE_BLUE.index,
                (byte) 200,
                (byte) 200,
                (byte) 250
        );

        // Hierarchical headers
        hierarchicalHeaders.put(0, standardHeader);

        CellStyle subHeader = workbook.createCellStyle();
        subHeader.setFont(f);
        subHeader.setBorderBottom(CellStyle.BORDER_THIN);
        subHeader.setBorderTop(CellStyle.BORDER_THIN);
        subHeader.setBorderLeft(CellStyle.BORDER_THIN);
        subHeader.setBorderRight(CellStyle.BORDER_THIN);
        subHeader.setAlignment(CellStyle.ALIGN_CENTER);
        subHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        subHeader.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        palette.setColorAtIndex(HSSFColor.GREY_50_PERCENT.index,
                (byte) 220,
                (byte) 220,
                (byte) 250
        );
        subHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        hierarchicalHeaders.put(1, subHeader);

        subHeader = workbook.createCellStyle();
        subHeader.setFont(f);
        subHeader.setBorderBottom(CellStyle.BORDER_THIN);
        subHeader.setBorderTop(CellStyle.BORDER_THIN);
        subHeader.setBorderLeft(CellStyle.BORDER_THIN);
        subHeader.setBorderRight(CellStyle.BORDER_THIN);
        subHeader.setAlignment(CellStyle.ALIGN_CENTER);
        subHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        subHeader.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
        palette.setColorAtIndex(HSSFColor.GREY_40_PERCENT.index,
                (byte) 230,
                (byte) 230,
                (byte) 250
        );
        subHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        hierarchicalHeaders.put(2, subHeader);

        subHeader = workbook.createCellStyle();
        subHeader.setFont(f);
        subHeader.setBorderBottom(CellStyle.BORDER_THIN);
        subHeader.setBorderTop(CellStyle.BORDER_THIN);
        subHeader.setBorderLeft(CellStyle.BORDER_THIN);
        subHeader.setBorderRight(CellStyle.BORDER_THIN);
        subHeader.setAlignment(CellStyle.ALIGN_CENTER);
        subHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        subHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        palette.setColorAtIndex(HSSFColor.GREY_25_PERCENT.index,
                (byte) 240,
                (byte) 240,
                (byte) 250
        );
        subHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
View Full Code Here

Examples of org.apache.poi.hssf.usermodel.HSSFPalette

  private HSSFColor getHSSFColor(final HSSFWorkbook workbook, final Color color) {
    byte red = (byte) color.getRed();
    byte green = (byte) color.getGreen();
    byte blue = (byte) color.getBlue();
    HSSFPalette palette = workbook.getCustomPalette();

    HSSFColor c = palette.findSimilarColor(red, green, blue);
    if (c == null) {
      try {
        c = palette.addColor(red, green, blue);
      } catch (RuntimeException e) {
        c = palette.findSimilarColor(red, green, blue);
      }
    }
    return c;
  }
View Full Code Here

Examples of org.zkoss.poi.hssf.usermodel.HSSFPalette

      argb = getRgbWithTint(argb, color.getTint());
    }
    return   "#"+ toHex(argb[0])+ toHex(argb[1])+ toHex(argb[2]);
  }
  private static String indexToHSSFRGB(HSSFWorkbook book, int index) {
    HSSFPalette palette = book.getCustomPalette();
    HSSFColor color = null;
    if (palette != null) {
      color = palette.getColor(index);
    }
    short[] triplet = null;
    if (color != null)
      triplet =  color.getTriplet();
    else {
View Full Code Here

Examples of org.zkoss.poi.hssf.usermodel.HSSFPalette

  }
  private static String tripletToHTML(short[] triplet) {
    return triplet == null ? null : "#"+ toHex(triplet[0])+ toHex(triplet[1])+ toHex(triplet[2]);
  }
  public static short rgbToIndex(Book book, String color) {
    HSSFPalette palette = ((HSSFWorkbook)book).getCustomPalette();
    short red = Short.parseShort(color.substring(1,3), 16); //red
    short green = Short.parseShort(color.substring(3,5), 16); //green
    short blue = Short.parseShort(color.substring(5), 16); //blue
    byte r = (byte)Math.abs((byte)red);
    byte g = (byte)Math.abs((byte)green);
    byte b = (byte)Math.abs((byte)blue);
   
    HSSFColor pcolor = palette.findColor(r, g, b);
    if (pcolor != null) { //find default palette
      return pcolor.getIndex();
    } else {
      final Hashtable<short[], HSSFColor> colors = HSSFColor.getRgbHash();
      HSSFColor tcolor = colors.get(new short[] {red, green, blue});
      if (tcolor != null)
        return tcolor.getIndex();
      else {
        try {
          HSSFColor ncolor = palette.addColor(r, g, b);
          return ncolor.getIndex();
        } catch (RuntimeException ex) {
          //return similar color if can't add new color to palette
          /*
           * TODO: find a better solution for fix this issue
           *
           * While there is no space for adding a color into palette
           * return similar color cause return inexact color
           */
          return palette.findSimilarColor(red, green, blue).getIndex();
        }
       
      }
    }
  }
View Full Code Here

Examples of org.zkoss.poi.hssf.usermodel.HSSFPalette

    byte g = triplet[1];
    byte b = triplet[2];
    short red = (short) (r & 0xff);
    short green = (short) (g & 0xff);
    short blue = (short) (b & 0xff);
    HSSFPalette palette = book.getCustomPalette();
    HSSFColor pcolor = palette != null ? palette.findColor(r, g, b) : null;
    if (pcolor != null) { //find default palette
      return pcolor;
    } else {
      final Hashtable<short[], HSSFColor> colors = HSSFColor.getRgbHash();
      HSSFColor tcolor = colors.get(new short[] {red, green, blue});
      if (tcolor != null)
        return tcolor;
      else {
        try {
          HSSFColor ncolor = palette.addColor(r, g, b);
          return ncolor;
        } catch (RuntimeException ex) {
          //try to create a fullcolor if not a built in palette color
          FullColorExt fullColor = new FullColorExt(red, green, blue);
          return new HSSFColorExt(fullColor);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.