package mobi.porquenao.recents;

import android.graphics.Color;
import android.os.Build;

public class RecentsHelper {

    private RecentsHelper() {}

    public static boolean isAvailable() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    public static boolean isColorLight(int color) {
        return computeContrastBetweenColors(color, Color.WHITE) <= 3f;
    }

    /**
     * Extracted from release android-5.1.1_r2
     * https://android.googlesource.com/platform/frameworks/base/+/android-5.1.1_r2/packages/SystemUI/src/com/android/systemui/recents/misc/Utilities.java
     *
     * Calculates the constrast between two colors, using the algorithm provided by the WCAG v2.
     */
    private static float computeContrastBetweenColors(int bg, int fg) {
        float bgR = Color.red(bg) / 255f;
        float bgG = Color.green(bg) / 255f;
        float bgB = Color.blue(bg) / 255f;
        bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
        bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
        bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
        float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;

        float fgR = Color.red(fg) / 255f;
        float fgG = Color.green(fg) / 255f;
        float fgB = Color.blue(fg) / 255f;
        fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
        fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
        fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
        float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
        return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
    }

}

@@ -440,24 +440,25 @@
 android-
-5.1.1
+7.0.0
 _r2
+1
 %0A     * 
@@ -529,16 +529,17 @@
 oid-
-5.1.1
+7.0.0
 _r2
+1
 /pac
@@ -638,17 +638,16 @@
  the con
-s
 trast be
@@ -1764,16 +1764,17 @@
  * fgB;%0A
+%0A
         

Update Android's color contrast calculator