Results 1 to 7 of 7
  1. #1
    Repe Sorsa is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default How to convert XSSFColor to java.awt.Color?

    I working with Excel support for a software which uses both xls and xlsx file formats. A small part of the code is shown below.

    I am writing code which gets java.awt.Color from org.apache.poi.ss.usermodel.Color. I haven't find any general way to do that.
    That's why HSSFColor and XSSFColor have separate blocks of code. At least my XSSFColor implementation is hack even if it is working.

    Is there a general way to convert org.apache.poi.ss.usermodel.Color to java.awt.Color? If not, is there a better way to get java.awt.Color from XSSFColor?

    private Color getColor(org.apache.poi.ss.usermodel.Color col) {
    Color color = null;
    if (col instanceof HSSFColor) {
    HSSFColor c = (HSSFColor) col;
    short[] triplet = c.getTriplet();
    color = new Color(triplet[0], triplet[1], triplet[2]);
    } else if (col instanceof XSSFColor) {
    XSSFColor c = (XSSFColor) col;
    String rgbHex = c.getCTColor().getDomNode().getAttributes().getNam edItem("rgb").getNodeValue();
    if (rgbHex.length() > 6) {
    rgbHex = rgbHex.substring(2);
    }
    color = Color.decode("#" + rgbHex);
    }
    return color;
    }

    Thanks for Helping me!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: How to convert XSSFColor to java.awt.Color?

    Wouldn't it be easier to manipulate the byte[] returned by getRgb()?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Repe Sorsa is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to convert XSSFColor to java.awt.Color?

    I tried that. getRgb() was for some reason returning all the time [-64, -64, -64] which was causing problems.

    Thanks anyways!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: How to convert XSSFColor to java.awt.Color?

    What colour should it be (in a 256 scale)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Repe Sorsa is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to convert XSSFColor to java.awt.Color?

    Hex color is COCOCO. If you convert that hex value to rgb color, the result is red: 192, green: 192 and blue: 192.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: How to convert XSSFColor to java.awt.Color?

    Which is what I thought.
    A byte in Java is signed...-64 is the equivalent to 192 if it was unsigned.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Repe Sorsa is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to convert XSSFColor to java.awt.Color?

    You are right. Thanks a lot!

Similar Threads

  1. Convert a color to gray
    By nwboy74 in forum AWT / Swing
    Replies: 0
    Last Post: 04-03-2012, 12:27 AM
  2. Color in Java
    By vishrut_n_shah in forum New To Java
    Replies: 1
    Last Post: 03-04-2012, 07:06 PM
  3. Convert image to RGB based on a color ramp
    By forumuser in forum Java 2D
    Replies: 0
    Last Post: 12-14-2011, 06:57 PM
  4. Convert String To Advanced Color
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 09-21-2011, 04:10 AM
  5. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 04:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •