Java tutorial
//package com.java2s; // Use of this source code is governed by a BSD-style license that can be import android.graphics.Color; public class Main { private static final float CONTRAST_LIGHT_ITEM_THRESHOLD = 3f; /** * Check whether lighter or darker foreground elements (i.e. text, drawables etc.) * should be used depending on the given background color. * @param backgroundColor The background color value which is being queried. * @return Whether light colored elements should be used. */ public static boolean shoudUseLightForegroundOnBackground(int backgroundColor) { return getContrastForColor(backgroundColor) >= CONTRAST_LIGHT_ITEM_THRESHOLD; } /** Calculates the contrast between the given color and white, using the algorithm provided by * the WCAG v2 in http://www.w3.org/TR/WCAG20/#contrast-ratiodef. */ private static float getContrastForColor(int color) { float bgR = Color.red(color) / 255f; float bgG = Color.green(color) / 255f; float bgB = Color.blue(color) / 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; return Math.abs((1.05f) / (bgL + 0.05f)); } }