Results 1 to 6 of 6
  1. #1
    winklerd is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default RGB from Color Name

    Hello,

    I'm trying to get the RGB hex value from a color name ("red" for instance) and am having trouble. Here's a snippet of my code:

    Java Code:
    private String colorToRGB(String color) {
        return "#" + Integer.toString(Color.getColor(color).getRGB(), 16);
    }

    I'm getting a NullPointerException from Color.getColor(color), though. The String parameter is guaranteed to be one of the static colors defined in the Color class. Am I not understanding the getColor method properly?
    Last edited by winklerd; 03-26-2010 at 05:19 AM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    getColor looks for a system property with the provided name, and returns null if it can't find it. It doesn't matter that 'color' is one of the named colors in the Color class, as those are not automatically set as system properties. Since getColro is returnign null, the 'null.getRGB()' throws a null pointer exception

    (heh, even the authors of java couldn't keep themselves from referring to a reference as a pointer when they named 'NullPointerException' ;) )

  3. #3
    winklerd is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Oh I know where the NPE is coming from. :) I guess my question is... what is it looking for in that method? Also, what/where are the "system properties" you refer to? Is there a way to get a list of acceptable values?

    Alternatively, given the string "red", is there another way to grab Color.RED?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Look at the API for the 'System' class. There are several methods that allow you to peruse the properties.

    Try running your program again, but this time add "-Dblue=255" to the java command line (prior to the class specification) and see if it now knows how to decode "blue".

  5. #5
    winklerd is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the info! Very informative.

    I found another way to do what I wanted:

    Java Code:
      String color = "GREEN;
      Field field = Class.forName("java.awt.Color").getField(color);
      int rgb = ((Color)field.get(null)).getRGB();

    Found and modified the code here: Retrieving a Predefined Color by Name | Example Depot
    Last edited by winklerd; 03-26-2010 at 04:08 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 04:27 AM
  2. Another Color TabbedPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:37 PM
  3. Color TabbedPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:36 PM
  4. Color gradient
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:50 PM
  5. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-11-2008, 11:57 PM

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
  •