Android Color Darker [英] Android Color Darker

查看:332
本文介绍了Android Color Darker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我的应用程序中有一个整数变量。它是颜色的值,由我的首选项中的颜色选择器设置。现在,我需要使用这种颜色和任何颜色的更黑的版本可能是。

Okay, so I have an integer variable in my application. It's the value of a color, being set by a color picker in my preferences. Now, I need to use both that color and a darker version of any color it might be.

现在我知道在标准Java有一个Color.darker()方法,但在Android中似乎没有一个等价物。有人知道等效或任何解决方法吗?谢谢。

Now I know in standard Java there is a Color.darker() method, but there doesn't seem to be an equivalent in Android. Does anyone know of an equivalent or any workarounds? Thanks.

推荐答案

我认为最简单的就是转换为HSV,在那里变暗, / p>

The easiest, I think, would be to convert to HSV, do the darkening there, and convert back:

float[] hsv = new float[3];
int color = getColor();
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
color = Color.HSVToColor(hsv);

要减轻,一个简单的方法可能是将值组件乘以> 1.0。然而,你必须将结果钳制到范围[0.0,1.0]。

To lighten, a simple approach may be to multiply the value component by something > 1.0. However, you'll have to clamp the result to the range [0.0, 1.0]. Also, simply multiplying isn't going to lighten black.

因此,更好的解决方案是:将值组件的1.0减少到lighten:

Therefore a better solution is: Reduce the difference from 1.0 of the value component to lighten:

hsv[2] = 1.0f - 0.8f * (1.0f - hsv[2]);

这完全平行于变暗的方法,只是使用1作为原点而不是0。工作以减轻任何颜色(甚至黑色),不需要任何夹紧。它可以简化为:

This is entirely parallel to the approach for darkening, just using 1 as the origin instead of 0. It works to lighten any color (even black) and doesn't need any clamping. It could be simplified to:

hsv[2] = 0.2f + 0.8f * hsv[2];

然而,由于浮点运算的四舍五入效应,我担心结果可能超过1.0f(可能一位)。更好地坚持稍复杂的公式。

However, because of possible rounding effects of floating point arithmetic, I'd be concerned that the result might exceed 1.0f (by perhaps one bit). Better to stick to the slightly more complicated formula.

这篇关于Android Color Darker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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