【问题标题】:Convert RGB Color to HEX color将 RGB 颜色转换为 HEX 颜色
【发布时间】:2019-02-08 10:25:39
【问题描述】:

我正在使用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();

【问题讨论】:

  • redValue+greenValue+blueValue 没有意义
  • 我该如何解决?
  • String.format("#%06X", pixel) ?

标签: android graphics hex rgb


【解决方案1】:

解决方案:

只需使用:

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

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

希望对你有帮助。

【讨论】:

  • @Avinash 很乐意帮助兄弟 ;)
【解决方案2】:

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

例子:

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

【讨论】:

  • 颜色 = 0xff334433;如何获得这个值,我的 rgb 值是 r:21 b:16 g:228 而当前的十六进制颜色是 15e410
  • 不,它只是举例,可以是任何值。也可以是Color.rgb(21, 228, 16)方法返回的值。
  • 是r,g,b的组合吗?
  • 是的。顺便说一下,第一个ff对应的是alpha通道。对于 RGB 颜色,它始终为 255,即十六进制的 FF。
【解决方案3】:

您将错误的参数发送到函数 String.format 以获取 hexColor。

试试这个:

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

【讨论】:

猜你喜欢
  • 2018-06-16
  • 2012-02-06
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2021-04-18
  • 2011-04-17
  • 2022-11-25
  • 2012-03-05
相关资源
最近更新 更多