简体   繁体   English

如何检查背景颜色的亮度以决定写在上面的文本颜色

[英]How to check brightness of a background color to decide text color written on it

I have a simple question, but I wasn't able to find an answer to this.我有一个简单的问题,但我无法找到答案。 Note, that I'm almost a complete beginner.请注意,我几乎是一个完整的初学者。

So I have an app (it's not mine, but I'm contributing to it), and in there is writing on a color background, which can be changed by the user.所以我有一个应用程序(它不是我的,但我正在为它做出贡献),并且在彩色背景上书写,可以由用户更改。 The writing should appear black if the background is bright enough but stay white if it isn't.如果背景足够亮,文字应该显示为黑色,如果不是,则保持白色。

The application is a school diary app for elementary and high-school students that connects to the state-wide school diary service in Hungary.该应用程序是一款面向中小学生的学校日记应用程序,可连接到匈牙利的全州学校日记服务。 Here, the best note is 5, the worst is 1. The user can set the colors of each grade in the settings.这里,最好的音符是5,最差的是1。用户可以在设置中设置每个等级的颜色。 Right now, only the code for the note "4" is hard-coded to have black text (because the background is yellow by default on "4" notes), all others have white.现在,只有音符“4”的代码被硬编码为具有黑色文本(因为“4”音符的背景默认为黄色),所有其他文本均为白色。 This is what I want to automate.这就是我想要自动化的。

Example of white text白色文本示例

Example of black text黑色文本示例

This is the main screen of the app for reference这是应用程序的主屏幕以供参考

Page where user can change color for a kind of note用户可以为某种笔记更改颜色的页面

Code right now:现在的代码:

switch (evaluation.NumberValue) {
    case 1:
      bColor = globals.color1;
      fColor = Colors.white;
      break;
    case 2:
      bColor = globals.color2;
      fColor = Colors.white;
      break;
    case 3:
      bColor = globals.color3;
      fColor = Colors.white;
      break;
    case 4:
      bColor = globals.color4;
      fColor = Colors.black; //This should be white if color4 is dark enough. Same applies to all of them.
      break;
    case 5: //I'm looking for something like this:
      bColor = globals.color5;
      fColor = (lightLevel(globals.color5) > 50) ? Colors.black : Colors.white;
      break;
    default:
      bColor = Colors.black;
      fColor = Colors.white;
      break;
  }

I'm looking for something like this:我正在寻找这样的东西:

 case 5: //I'm looking for something like this:
  bColor = globals.color5;
  fColor = (lightLevel(globals.color5) > 50) ? Colors.black : Colors.white;
  break;

Thank you for any help!感谢您的任何帮助!

I can suggest 2 options:我可以建议 2 个选项:

  1. Brightness.estimateBrightnessForColor method eg Brightness.estimateBrightnessForColor方法例如
Color calculateTextColor(Color background) {
  return Brightness.estimateBrightnessForColor(background) == Brightness.light ? Colors.black : Colors.white;
}

  1. Color.computeLuminance method eg Color.computeLuminance方法例如
Color calculateTextColor(Color background) {
  return background.computeLuminance() >= 0.5 ? Colors.black : Colors.white;
}

Although the latter one is described as computationally expensive to calculate .尽管后者被描述为computationally expensive to calculate

Please let me know if this helped.如果这有帮助,请告诉我。

Brightness.estimateBrightnessForColor method eg Color calculateTextColor(Color background) { return Brightness.estimateBrightnessForColor(background) == Brightness.light ? Colors.black : Colors.white; }

I found that the method estimateBrightnessForColor is now part of the class ThemeData, so need to be called like this now:我发现方法estimateBrightnessForColor现在是类ThemeData的一部分,所以现在需要像这样调用:

ThemeData.estimateBrightnessForColor(background)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM