在android中增加Color的亮度

时间:2012-08-31 03:46:49

标签: java android eclipse

我正在使用此代码随机更改textView的颜色。我有黑色背景,所以有时这些颜色难以阅读。

 int r = (rand.nextInt(176) * 80 ) / 256;
        int g = (rand.nextInt(176) * 80 ) / 256;
        int b = (rand.nextInt(176) * 80 ) / 256;

        TextView pos = (TextView) view.findViewById(R.id.position);
        pos.setText(((Integer)(position + 1)).toString());
        pos.setTextSize(30);
        pos.setTextColor(Color.argb(255,r,g,b));

        TextView data = (TextView) view.findViewById(R.id.textOfTip);
        data.setText(tipsList[position].toString());
        data.setTextSize(24);
        data.setTextColor(Color.argb(255,r,g,b));

我的问题是,如何才能增加文字颜色的亮度或发光效果,以便于阅读。

最好的问候

2 个答案:

答案 0 :(得分:2)

首先,以0xAARRGGBB格式获取颜色(例如,实心红色为0xFFFF0000)。然后,将其推送到方法colorToHSV。接下来,更改L/V/B值(vary slightly,但我认为它将足够接近你正在做的事情)。最后,请致电HSVToColor以再次以0xAARRGGBB格式获取新颜色。然后有several ways将其转换为R,G,B值,这通常涉及字节移位。

这样的事情:

int color = 0xFFFF0000;
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] = 0.2f;
color = Color.HSVToColor(hsv);
int[] rgb = new int[3];
MyColor.colorToRGB(color, rgb); // Your custom method
// The rgb array now contains your RGB colors.

注意:还有RGBToHSV,可能会派上用场。

答案 1 :(得分:0)

我认为有一些公式可以达到亮度和发光效果。本文为您提供了很好的理解。

Formula to determine brightness of RGB color