Android的颜色变深 [英] Android Color Darker
问题描述
好了,所以我在我的应用程序中的整型变量。这是一个色彩的价值,在我的preferences被设置由一个颜色选择器。现在,我需要同时使用的颜色和任何颜色它可能是一个黑暗的版本。
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,做变黑那里,并转换回:
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]。你也可以尝试使用其他美白配方,因为只需乘不会淡化黑眼圈。
(This is untested.) To lighten, multiply the value component by something > 1.0. You'll have to clamp the result to the range [0.0, 1.0]. You might also experiment with other lightening formulas, since simply multiplying isn't going to lighten black.
编辑:其实,减仓,比一个更好的策略上面可能会减少1.0值部件的区别:
Actually, to lighten, a better strategy than the above might be to reduce the difference from 1.0 of the value component:
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 can 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 slightly concerned that the result might exceed 1.0f (by perhaps one bit).
这篇关于Android的颜色变深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!