问题描述
我正在尝试修改第三方软件。我想使用由一些方法(我不能modifiy)作为一个整数返回的颜色。但是,我想使用RGB格式,如#FF00FF。如何进行转换?
I am trying to modify a third party software. I want to use a color which is returned by some methods (which I cant modifiy) as an integer. However, I would like to use RGB format, like #FF00FF. How can I make a conversion?
这里是一个HTML示例
我想在Android上归档同样的东西。
Here is an HTML example http://www.shodor.org/stella2java/rgbint.htmlI would like to archive same thing in Java, on Android.
推荐答案
使用此
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
我们知道HEX中的颜色值的长度为6。 %06X与来自(0xFFFFFF& intColor)的结果匹配,如果长度小于6,则通过在结果的左侧附加ZERO使结果为6。你看到#,所以这个#字符附加到结果,最后你得到一个HEX颜色值。
We know lenght of color value in HEX is 6. So you see 6 here. %06X matches the result coming from (0xFFFFFF & intColor) and if length is less than 6, it makes result with 6 by appending ZERO to left side of result. And you see #, so this # char gets appended to result and finally you get a HEX COLOR value.
这篇关于将整数颜色值转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!