Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a RGB Color Value to a Hexadecimal String

Tags:

java

swing

In my Java application, I was able to get the Color of a JButton in terms of red, green and blue; I have stored these values in three ints.

How do I convert those RGB values into a String containing the equivalent hexadecimal representation? Such as #0033fA

like image 252
Lalchand Avatar asked Aug 31 '10 09:08

Lalchand


People also ask

How do I convert RGB to string in Java?

Color your_color = new Color(128,128,128); String hex = "#"+Integer. toHexString(your_color. getRGB()). substring(2);


1 Answers

You can use

String hex = String.format("#%02x%02x%02x", r, g, b);   

Use capital X's if you want your resulting hex-digits to be capitalized (#FFFFFF vs. #ffffff).

like image 178
mhshams Avatar answered Oct 05 '22 17:10

mhshams