android 如何在安卓系统中检查颜色亮度?

iqih9akk  于 2024-01-04  发布在  Android
关注(0)|答案(3)|浏览(187)

如何在安卓系统中检查亮度?
我有一个颜色整数值。2我想根据颜色的整数值来检查这个颜色是深色还是浅色。

  1. if (checkColor == Color.RED || checkColor == Color.BLACK) {
  2. //set fore color is white
  3. } else {
  4. //set fore color is black
  5. }

字符串
而不是上面的代码,我想改变

  1. if (!isBrightColor(checkColor)) {
  2. //set fore color is white
  3. } else {
  4. //set fore color is black
  5. }
  6. private boolean isBrightColor(int checkColor){
  7. boolean rtnValue;
  8. //How to check this color is bright or dark
  9. return rtnValue;
  10. }

oiopk7p5

oiopk7p51#

你应该试试这个……

  1. public static boolean isBrightColor(int color) {
  2. if (android.R.color.transparent == color)
  3. return true;
  4. boolean rtnValue = false;
  5. int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) };
  6. int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1]
  7. * rgb[1] * .691 + rgb[2] * rgb[2] * .068);
  8. // color is light
  9. if (brightness >= 200) {
  10. rtnValue = true;
  11. }
  12. return rtnValue;
  13. }

字符串
参考:Android/Java: Determining if text color will blend in with the background?

展开查看全部
cidc1ykv

cidc1ykv2#

我知道这是一个老问题,但现在有一个Color.luminance(int)已经做了选择的答案建议(API 24+)

  1. /**
  2. * Returns the relative luminance of a color.
  3. * <p>
  4. * Assumes sRGB encoding. Based on the formula for relative luminance
  5. * defined in WCAG 2.0, W3C Recommendation 11 December 2008.
  6. *
  7. * @return a value between 0 (darkest black) and 1 (lightest white)
  8. */
  9. public static float luminance(@ColorInt int color) {
  10. ColorSpace.Rgb cs = (ColorSpace.Rgb) ColorSpace.get(ColorSpace.Named.SRGB);
  11. DoubleUnaryOperator eotf = cs.getEotf();
  12. double r = eotf.applyAsDouble(red(color) / 255.0);
  13. double g = eotf.applyAsDouble(green(color) / 255.0);
  14. double b = eotf.applyAsDouble(blue(color) / 255.0);
  15. return (float) ((0.2126 * r) + (0.7152 * g) + (0.0722 * b));
  16. }

字符串
亮度和亮度之间也有区别:
根据维基百科:
一种更感知相关的替代方案是使用亮度Y′作为亮度维度(图12 d)。亮度是伽马校正的R、G和B的加权平均值,基于它们对感知亮度的贡献,长期用作彩色电视广播中的单色维度。对于sRGB,Rec.709原色产生Y′709,根据Rec.601,数字滤波器使用Y′601,其他一些原色也在使用中,这导致不同的系数。

  1. Y 601 = 0.2989 * R + 0.5870 * G + 0.1140 * B (SDTV)
  2. Y 240 = 0.212 * R + 0.701 * G + 0.087 * B (Adobe)
  3. Y 709 = 0.2126 * R + 0.7152 * G + 0.0722 * B (HDTV)
  4. Y 2020 = 0.2627 * R + 0.6780 * G + 0.0593 * B (UHDTV, HDR)


因此,您首先需要了解您想要的实际上是亮度(RGB上R,G和B之和的平均值)还是亮度,这是人眼感知的结果。
为了完整起见,如果你想要实际的亮度,那将是颜色的HSV表示上的V值,所以你可以使用:用途:

  1. val hsv = FloatArray(3)
  2. Color.colorToHSV(color, hsv)
  3. val brightness = hsv[2]

展开查看全部
pdkcd3nj

pdkcd3nj3#

我知道这已经太晚了,但这可能会帮助别人在未来,因为它是更容易和值得信赖的答案:
你可以直接使用java中已经存在的库:
ColorUtils.calculateLuminance()
示例:-

  1. Random rnd = new Random();
  2. int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  3. if(ColorUtils.calculateLuminance(color) < 0.5) {
  4. //darker or brighter color
  5. // hence use white foreground
  6. }
  7. else{
  8. //lighter color
  9. //hence use black as foreground
  10. }

字符串

展开查看全部

相关问题