0

I've got a method that sets the text and images tints of a parent to some color. Now if the background of the parent and the foreground (the tint I'm settings) are close in contrast the text won't be readable.

How can I check for the difference between those 2 colors and change one (make it lighter or darker) up to a point where they become readable?

Here's what I've got till now:

public static void invokeContrastSafety(ViewGroup parent, int tint, boolean shouldPreserveForeground) {
    Drawable background = parent.getBackground();
    if (background instanceof ColorDrawable) {
        if (isColorDark(((ColorDrawable) background).getColor())) {
            // Parent background is dark

            if (isColorDark(tint)) {
                // Tint (foreground) color is also dark.
                if (shouldPreserveForeground) {
                    // We can't modify tint color, changing background to make things readable.


                } else {
                    // Altering foreground to make things readable

                }

                invokeInternal(parent, tint);
            } else {
                // Everything is readable. Just pass it on.
                invokeInternal(parent, tint);
            }

        } else {
            // Parent background is light

            if (!isColorDark(tint)) {
                if (shouldPreserveForeground) {

                } else {

                }
            } else {
                invokeInternal(parent, tint);
            }
        }
    }
}

private static boolean isColorDark(int color){
    double darkness = 1-(0.299* Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255;
    return darkness >= 0.2;
}
Alex Newman
  • 1,369
  • 12
  • 34

1 Answers1

0

Try this one it works for me. This function returns that the color is dark or light.

public static boolean isBrightColor(int color) {
    if (android.R.color.transparent == color)
        return true;

    boolean rtnValue = false;

    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);

    // Color is Light
    if (brightness >= 200) {
        rtnValue = true;
    }
    return rtnValue;
}

You can change your custom logic in this function if you need.

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • My bad for not adding the `isColorDark()` function in the snippet. I already know how to check for that. – Alex Newman Jul 15 '17 at 12:31
  • Then what is the issue? – Andy Developer Jul 15 '17 at 12:32
  • I left blank spaces for the logic to make the color darker or lighter up to the point when it will be easily readable on the given background color. – Alex Newman Jul 15 '17 at 12:34
  • If your background color is dark then use light color and if your background is light then use dark color. You need to choose particular for light background and dark background. If you don't want to do that then set the light transparency with same color if you use dark background and set the dark transparency with same color if you use the light background. – Andy Developer Jul 15 '17 at 12:40