ViewUtils { public static boolean isDarkBackground(Activity activity) { int color = activity.getResources().getColor(R.color.bt_white); try { Drawable background = activity.getWindow().getDecorView().getRootView().getBackground(); if (background instanceof ColorDrawable) { color = ((ColorDrawable) background).getColor(); } } catch (Exception ignored) {} double luminance = (0.2126 * Color.red(color)) + (0.7152 * Color.green(color)) + (0.0722 * Color.blue(color)); return luminance < 128; }  static int dp2px(Context context, float dp); static boolean isDarkBackground(Activity activity);  }
@Test public void isDarkBackground_detectsBlackBackgroundAndReturnsTrue() { Activity activity = setupActivity(R.color.bt_black); assertTrue(ViewUtils.isDarkBackground(activity)); }
@Test public void isDarkBackground_detectsWhiteBackgroundAndReturnsFalse() { Activity activity = setupActivity(R.color.bt_white); assertFalse(ViewUtils.isDarkBackground(activity)); }