【问题标题】:Android/Java: Determining if text color will blend in with the background?Android/Java:确定文本颜色是否会与背景融为一体?
【发布时间】:2011-12-08 18:55:09
【问题描述】:

我在我的应用程序中引入了“标记”功能,我允许显示标记的方法之一是将文本设置为用户为每个标记选择的颜色。我的应用程序有三个主题,背景分别是白色、黑色和类似记事本的棕色(这些将来可能会改变/增长)。如果标签容易与背景形成对比,我希望能够以其本机颜色显示标签,否则只需为每个主题使用默认文本颜色。

我编写了一个辅助函数来帮助我确定文本是否会被屏蔽,但它不是 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;
   }

当使用蓝色、红色、透明、黑色、黄色和绿色调用此函数时,输出如下(分别):

  • h=0.0,s=1.0,v=1.0,主题=1
  • h=229.41177,s=1.0,v=1.0,主题=1
  • h=267.6923, s=1.0, v=0.050980393, 主题=1
  • h=0.0,s=0.0,v=0.0,主题=1
  • h=59.52941,s=1.0,v=1.0,主题=1
  • h=111.29411, s=1.0, v=1.0, 主题=1

我的问题是:根据色调、饱和度和值,您如何确定以某种方式着色的文本是否会显示在白色背景和黑色背景上,或者是否会被遮盖?请采用我的算法并改进它或帮助我创建一个新算法。

提前致谢。

【问题讨论】:

  • 感谢什么?你甚至没有问一个问题,但是:不客气。
  • 更新了文字,抱歉我没说清楚。

标签: java android colors textview hsv


【解决方案1】:

我想出的解决方案:

我最终使用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;
}

【讨论】:

  • 请注意,源的亮度系数来自another source,同时它本身已更改以指定其作者“自从被告知......更准确”的不同系数:.299, .587, .114.
猜你喜欢
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
相关资源
最近更新 更多