By knowing the color name how can we programmatically find its red-green-blue values in Java?
-
4What color names do you want to recognize? HTML ones? In any case the simplest thing is to build a `Map
` to lookup them. – helios Aug 28 '12 at 11:34 -
same as this. http://stackoverflow.com/questions/4126029/java-color-code-convert-to-color-name – Sajith Aug 28 '12 at 11:38
-
1@Sajith Yes, just the other way around. – Baz Aug 28 '12 at 11:38
5 Answers
The javax.swing.text.html.StyleSheet
can be used for this:
import javax.swing.text.html.StyleSheet;
StyleSheet s = new StyleSheet();
String rgb = "black";
Color c1 = s.stringToColor(rgb);
r1 = c1.getRed();
g1 = c1.getGreen();
b1 = c1.getBlue();
System.out.println(r1 + ", " + g1 + ", " + b1);

- 11,553
- 8
- 64
- 88

- 51
- 1
- 2
Since you are using SWT, you may be able to use the ColorRegistry API. There are a couple of ways to get hold of prepopulated registries (JFaceResources.getColorRegistry()
and ITheme.getColorRegistry()
) though it is not obvious from the javadocs what colors they are prepopulated from, and where the color definitions come from.
Alternatively use create a map and populate it with names based on the SWT.COLOR_XXX constants ans color values obtained by using Display.getSystemColor(...)

- 698,415
- 94
- 811
- 1,216
If you are writing code for the Eclipse platform then the ColorUtil#getColorValue
method is an alternative.
It have access to all the colours for the constants that are defined in the SWT
class, also the system colours.
The method is in the org.eclipse.ui.workbench
plug-in.

- 11,553
- 8
- 64
- 88
Color c= Color.red;
int rgb=c.getRed()*65536+c.getGreen()*256+c.getBlue();
Is this what you wanted to know?

- 49,044
- 25
- 144
- 182

- 204
- 3
- 15
You can get rgb value from hex value.
following code :
Color aColor = new Color(0xFF0096); // Use the hex number syntax
aColor.getRGB()

- 4,134
- 2
- 26
- 37
-
That's not at all what the OP asked for. He wants to have RGB value if the input is, for example, `"blue"`. – Baz Aug 28 '12 at 11:45
-
yes u r right I want to get rgb value from a color name.....and I think it could be helpful by mapping only as... stated above – Alok Aug 28 '12 at 11:54