You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by nb <bl...@vestabs.com> on 2012/07/12 15:06:45 UTC

Problems on getting rgb of an indexed XSSFColor

Hi!

I have problems on getting the rgb-values of an XSSFColor in order to
convert it to an awt-Color.
I use this code:

  public static Color getAwtColor(XSSFColor color, XSSFWorkbook wb)
  {
    if(color==null) return null;
    if(color.getCTColor().isSetIndexed())
    {
      short ix=color.getIndexed();
      if(ix==IndexedColors.AUTOMATIC.index) return null;
      if(ix==IndexedColors.BLACK.index) return BLACK; //check first indexes
manually because of a bug in poi3.8
      if(ix==IndexedColors.WHITE.index) return WHITE;
      if(ix==IndexedColors.RED.index) return RED;
      if(ix==IndexedColors.BRIGHT_GREEN.index) return GREEN;
      if(ix==IndexedColors.BLUE.index) return BLUE;
      if(defaultPaletteRecord==null)
        defaultPaletteRecord=new PaletteRecord();
      byte[] b=defaultPaletteRecord.getColor(ix);
      return b!=null ? new Color(Util.unsignedInt(b[0]),
Util.unsignedInt(b[1]), Util.unsignedInt(b[2])) : null;
    }
    if(color.getCTColor().isSetTheme())
    {
      int theme=color.getTheme();
      switch(theme)
      {
        case 0: case 1: case 2: case 3: //handle bug: MSExcel does not
follow own definitions
          double tint=color.getTint();
          try {
            color=wb.getTheme().getThemeColor(theme^1);
            color.setTint(tint);
          } catch(NullPointerException ex){ }
      }
    }
    if(color.getCTColor().getRgb()==null) return null;
    byte[] rgb=color.getRgbWithTint();
    return rgb!=null ? rgb.length==3 ?
        new Color(unsignedInt(rgb[0]), unsignedInt(rgb[1]),
unsignedInt(rgb[2])) :
        new Color(unsignedInt(rgb[1]), unsignedInt(rgb[2]),
unsignedInt(rgb[3]), unsignedInt(rgb[0])) :
        null;
  }

Normally this works fine, but the background color of the orange cell in
this xlsx-file:
http://apache-poi.1045710.n5.nabble.com/file/n5710445/Color_Test.xlsx
Color_Test.xlsx 
is converted to a bright green since the index of its XSSFColor is
==IndexedColors.BRIGHT_GREEN.index .
There seems to be a custom-palette that is used instead of the default
indexed colors; in HSSF I use HSSFWorkbook.getCustomPalette() but in XSSF
the is not such a method.

Am I doing something wrong? Is there a simpler way or a utility-method that
I didn't find?

Regards,
Nils





--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Problems-on-getting-rgb-of-an-indexed-XSSFColor-tp5710445.html
Sent from the POI - User mailing list archive at Nabble.com.

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