我在我的应用程序中引入了“标记”功能,我允许显示标记的方法之一是将文本设置为用户为每个选择的颜色。我的应用程序有三个主题,背景为白色,黑色和类似记事本的棕色(这些可能会在未来发生变化/增长)。如果标签容易与背景形成对比,我希望能够以原生颜色显示标签,否则只需使用每个主题的默认文本颜色。
我写了一个辅助函数来帮助我确定文本是否会被屏蔽,但它不是100%正确(我希望它确定是否会根据所有三个hsv组件屏蔽颜色,现在饱和度比较无效)。代码如下。
public static boolean colorWillBeMasked(int color, Application app){
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
//note 0, black 1, white 2
int theme = app.api.getThemeView();
System.out.println("h=" +hsv[0]+ ", s=" +hsv[1]+ ", v=" +hsv[2]+", theme="+theme);
if(android.R.color.transparent == color) return true;
// color is dark
if(hsv[2] <= .2){
if(theme == 1) return true;
}
// color is light
else if(hsv[2] >= .8) {
if(theme == 2) return true;
}
return false;
}
当用蓝色,红色,透明,黑色,黄色和绿色调用此功能时,输出如下(分别):
我的问题是:基于色调,饱和度和值,如何确定以某种方式着色的文本是否会显示在白色背景上与黑色背景上,还是会被屏蔽?请使用我的算法并对其进行改进或帮助我创建一个新算法。
提前致谢。
答案 0 :(得分:4)
我提出的解决方案:
我最终使用找到on this blog的算法来重新定义我的函数,如下所示;然后我调整了两端的亮度截止。希望这有助于某人。
public static boolean colorWillBeMasked(int color, Application app){
if(android.R.color.transparent == color) return true;
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);
System.out.println("COLOR: " + color + ", BRIGHT: " + brightness);
//note 0,black 1,classic 2
int theme = app.api.getThemeView();
// color is dark
if(brightness <= 40){
if(theme == 1) return true;
}
// color is light
else if (brightness >= 215){
if(theme == 2) return true;
}
return false;
}