将RGB颜色值转换为十六进制字符串 [英] Convert a RGB Color Value to a Hexadecimal String
本文介绍了将RGB颜色值转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Java应用程序中,我能够用红色,绿色和蓝色获得JButton
的Color
.我已将这些值存储在三个int
中.
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 int
s.
如何将这些RGB值转换为包含等效十六进制表示形式的String
?如#0033fA
How do I convert those RGB values into a String
containing the equivalent hexadecimal representation? Such as #0033fA
推荐答案
您可以使用
String hex = String.format("#%02x%02x%02x", r, g, b);
如果您希望将所得的十六进制数字大写(#FFFFFF
与#ffffff
),请使用大写X.
Use capital X's if you want your resulting hex-digits to be capitalized (#FFFFFF
vs. #ffffff
).
这篇关于将RGB颜色值转换为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文