如何在android中检查亮度?
我有一个整数值的颜色.我想检查这个颜色是深色或浅色基于整数值的颜色.
if (checkColor == Color.RED || checkColor == Color.BLACK) {
//set fore color is white
} else {
//set fore color is black
}
Run Code Online (Sandbox Code Playgroud)
而不是上面的代码,我想改变
if (!isBrightColor(checkColor)) {
//set fore color is white
} else {
//set fore color is black
}
private boolean isBrightColor(int checkColor){
boolean rtnValue;
//How to check this color is bright or dark
return rtnValue;
}
Run Code Online (Sandbox Code Playgroud)
你应该试试这个....
public static boolean isBrightColor(int color) {
if (android.R.color.transparent == color)
return true;
boolean rtnValue = false;
int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) };
int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1]
* rgb[1] * .691 + rgb[2] * rgb[2] * .068);
// color is light
if (brightness >= 200) {
rtnValue = true;
}
return rtnValue;
}
Run Code Online (Sandbox Code Playgroud)
参考: Android/Java:确定文本颜色是否会与背景混合?
| 归档时间: |
|
| 查看次数: |
3023 次 |
| 最近记录: |