get Best Contrast Color - Android Graphics

Android examples for Graphics:Contrast Color

Description

get Best Contrast Color

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    /**//  w  ww . j  a  v  a2  s. c  o m
     * @see {http://stackoverflow.com/questions/946544/good-text-foreground-color-for-a-given-background-color} 
     * @param color
     * @return white or black depending on the brightness of the color
     */
    public static int getBestContrast(int color) {
        return isDark(color) ? Color.WHITE : Color.BLACK;
    }

    public static boolean isDark(int color) {
        int valuedColor = Color.red(color) * 299 + Color.green(color) * 587
                + Color.blue(color) * 114;

        return valuedColor < 186000;
    }
}

Related Tutorials