[英]Convert RGB Color to HEX color
我正在使用OnTouchListener
从ImageView
中获取颜色。
Red, Green, Blue 颜色代码可以成功获取,但我无法将 RGB 转换为 HEX..
示例:我的 rgb 值是
r:21
乙:16
克:228
当前的十六进制颜色是#15e410。
我想要#15e410。 来自 r:21,b:16,g:228
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
int hexa= Color.rgb(redValue, greenValue, blueValue);
Toast.makeText(getApplicationContext(),"hexa ::"+hexa ,Toast.LENGTH_LONG).show();
解:
只需使用:
String hex = String.format("#%02x%02x%02x", redValue, greenValue, blueValue);
这会将所有红色,绿色和蓝色值转换为十六进制字符串。
希望能帮助到你。
使用Integer.toHexString(color);
将整数转换为十六进制字符串。
例:
int color = 0xff334433;
String hex = Integer.toHexString(color);
System.out.println(hex); // this prints - ff334433
我的解决方案:
Function RGBToHex(red As Integer, green As Integer, blue As Integer) As String
Return String.Format("#{0:X2}{1:X2}{2:X2}", red, green, blue)
End Function
这个 function 以三个整数作为输入(分别代表颜色的红、绿、蓝值),并返回格式为“#RRGGBB”的字符串,其中 RR、GG 和 BB 是红、绿、蓝的十六进制值颜色的蓝色成分。
您可以通过将颜色的红色、绿色和蓝色值传递为 arguments 来调用此 function,如下所示:
Dim hexColor As String = RGBToHex(255, 0, 0)
您将错误的参数发送给函数String.format以获取hexColor。
试试这个:
String hexColor = String.format("#%06X", redValued, greenValue, blueValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.