Results 1 to 6 of 6
Thread: RGB from Color Name
- 03-26-2010, 05:16 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
- 03-26-2010, 05:48 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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' ;) )
- 03-26-2010, 05:53 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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?
- 03-26-2010, 05:56 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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".
- 03-26-2010, 04:06 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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 DepotLast edited by winklerd; 03-26-2010 at 04:08 PM.
-
I've simply used enums with good success. But then I'm allergic to reflection.
Similar Threads
-
[COLOR="Navy"]execute .bat file in mysql [/COLOR]
By msankar.ravi in forum NetworkingReplies: 0Last Post: 02-24-2010, 04:27 AM -
Another Color TabbedPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:37 PM -
Color TabbedPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:36 PM -
Color gradient
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:50 PM -
A bit of color!
By tim in forum Java 2DReplies: 8Last Post: 02-11-2008, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks