Java: How to get the name of colors in Java from Hex or RGB values?
-
Hi, The problem is that I need to get the names of colors using Java. I got the hex values and the RGB values of where ever the cursor goes. I tried using the hex value and match it to a file of hex values with their color names but only white and grey are being displayed, none of the other color names are showing up. I noticed that some of the hex values given by Java are not matching the ones in the file. Same problem with the RGB values. Is there a way that I can use the RGB or hex values, that I get from Java Color class, to get the color name? Any open source library or anything that could get me the color name from hex or RGB values? Thanks
-
Answer:
Those colors specified in "java.awt.Color" are just shortcuts for readily available colors. Given the RGB values (let's say 50,100,75 for example), you can just create a Color instance of that exact color. Color c = new Color(50,100,75); If you actually wanted the names, and wanted it to conform to the definitions of the colors specified by this class, you'd have to iterate over each of the colors and compare. And the annoying part..is that those colors aren't defined in a fancy-shmancy enum... so you have to construct your own list (or use some even fancier but horribly-engineered reflection). So to keep it simple: import java.awt.Color; public enum JavaColor { RED (Color.RED), BLUE (Color.BLUE), GREEN (Color.GREEN), ... Do all the colors ... private final Color color; // in meters Planet(Color color) { this.color = color; } public Color getColor() { return color; } } Color myColor = new Color(50,100,75); Color javaColor = null; for(JavaColor jc: JavaColor.values()) { if(jc.getColor().equals(myColor)) javaColor = jc.getColor(); } if(javaColor == null) { System.out.println("Your RGB values don't match any Java colors."); } else { System.out.println("Your RGB values match the Java color \"%s\".",javaColor); } Using Strings with hex values is not much different, but java.awt.Color only provides constructors to handle RGB, so you'd need to convert it first: http://stackoverflow.com/questions/4129666/how-to-convert-hex-to-rgb But just copy and paste that :)
Scott Danzig at Quora Visit the source
Related Q & A:
- How to get output on screen with java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to get real path of an image in Java?Best solution by Stack Overflow
- How to get the name of the currently connected WiFi network?Best solution by Stack Overflow
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.