在Android中使用color和color.darker? [英] Using color and color.darker in Android?

查看:286
本文介绍了在Android中使用color和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?

推荐答案

我认为最简单的就是转换为HSV,在那里变暗,并转换回来:

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.

因此,更好的解决方案是:将价值组成部分的差异减少到减轻:

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和color.darker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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