将RGB颜色转换为十六进制颜色 [英] Convert RGB Color to HEX color

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

问题描述

我正在使用 OnTouchListener ImageView 中获取颜色.

I am taking a color from an ImageView using OnTouchListener.

可以成功获得红色,绿色,蓝色颜色代码,但是我无法将RGB转换为十六进制..

Red, Green, Blue color code can be successfully obtained, but i cant convert RGB to HEX ..

示例:我的rgb值是
r:21

example : my rgb values are
r:21

b:16

g:228

当前对应的十六进制颜色为#15e410.

and curresponding hex color is #15e410.

我想得到#15e410.从r:21,b:16,g:228

i want get #15e410. from 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);

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

This will convert all the Red, Green and Blue values to Hex String.

希望有帮助.

这篇关于将RGB颜色转换为十六进制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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