将 RGB 颜色转换为 HEX 颜色

Convert RGB Color to HEX color

我正在使用 OnTouchListenerImageView 中提取颜色。

红、绿、蓝色码可以成功获取,但无法将RGB转HEX..

示例:我的 rgb 值为
r:21

b:16

g: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.format 发送了错误的参数以获取 hexColor。

试试这个:

String hexColor = String.format("#%06X", redValued, greenValue, blueValue);

使用 Integer.toHexString(color); 将整数转换为十六进制字符串。

示例:

int color = 0xff334433;
String hex = Integer.toHexString(color);
System.out.println(hex); // this prints - ff334433

解法:

只需使用:

String hex = String.format("#%02x%02x%02x", redValue, greenValue, blueValue);

这会将所有红色、绿色和蓝色值转换为十六进制字符串。

希望对您有所帮助。