I have a requirement to convert color into Hexadecimal code in Java. I have color available in Java class like Red, Green, Anthracite . I need to convert it into Hex code and return back to JSP.
Asked
Active
Viewed 62 times
-3
-
provide what you have tried so far. – Jonathan Portorreal Mar 01 '17 at 05:20
-
2And your question is? – Guy Mar 01 '17 at 05:21
-
I saw a method String.format("#%06x", color.getRGB() & 0x00FFFFFF) somewhere. But I did not get how can I use the existing color available in java class and can change it – Techie Mar 01 '17 at 05:22
1 Answers
0
There are different way for your question
You can use your_color.getRGB()
for get rgb
and use Integer.toHexString(your_color.getRGB())
return string
String hex = "#" + Integer.toHexString(your_color.getRGB()).substring(2);
substring for don't select alpha

Yashar Panahi
- 2,816
- 1
- 16
- 22
-
"your_color " is available with type String. Do I need to typecast it ? – Techie Mar 01 '17 at 05:36
-
-
The color available in my java class is of Type String. How can I typecast string into Color? – Techie Mar 01 '17 at 05:46
-
-
I used Color color = Color.decode(string); and getting Number Format Exception – Techie Mar 01 '17 at 06:03
-
Can you hekp on this. I tried try { Field field = Class.forName("java.awt.Color").getField("yellow"); color = (Color)field.get(null); } catch (Exception e) { color = null; // Not defined } But it is also not working – Techie Mar 01 '17 at 07:06