将RGB颜色值转换为十六进制字符串 [英] Convert a RGB Color Value to a Hexadecimal String

查看:333
本文介绍了将RGB颜色值转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序中,我能够用红色,绿色和蓝色获得JButtonColor.我已将这些值存储在三个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 ints.

如何将这些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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆