如何使给定颜色变暗(int) [英] How to darken a given color (int)
本文介绍了如何使给定颜色变暗(int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个采用给定颜色的函数,我希望它使颜色变暗(将其亮度降低20%左右).仅给出颜色(int),我不知道如何执行此操作.正确的方法是什么?
I have a function that takes a given color and I would like it to darken the color (reduce its brightness by 20% or so). I can't figure out how to do this given just a color (int). What is the proper approach?
public static int returnDarkerColor(int color){
int darkerColor = ....
return darkerColor;
}
推荐答案
更多的Android方法:
A more Android way of doing it:
public static int manipulateColor(int color, float factor) {
int a = Color.alpha(color);
int r = Math.round(Color.red(color) * factor);
int g = Math.round(Color.green(color) * factor);
int b = Math.round(Color.blue(color) * factor);
return Color.argb(a,
Math.min(r,255),
Math.min(g,255),
Math.min(b,255));
}
您将希望使用小于 1.0f
的因数来变暗.尝试 0.8f
.
You will want to use a factor less than 1.0f
to darken. try 0.8f
.
这篇关于如何使给定颜色变暗(int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文