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 LIGHTNESS_OPAQUE_BOX_THRESHOLD = 0.82f; private static final float LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA = 0.2f; /** * @return The base color for the textbox given a toolbar background color. */ public static int getTextBoxColorForToolbarBackground(int color) { if (shouldUseOpaqueTextboxBackground(color)) return Color.WHITE; return getColorWithOverlay(Color.WHITE, color, LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA); } /** * Check which version of the textbox background should be used depending on the given * color. * @param color The color value we are querying for. * @return Whether the transparent version of the background should be used. */ public static boolean shouldUseOpaqueTextboxBackground(int color) { return getLightnessForColor(color) > LIGHTNESS_OPAQUE_BOX_THRESHOLD; } private static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) { return Color.rgb( (int) (overlayAlpha * Color.red(baseColor) + (1f - overlayAlpha) * Color.red(overlayColor)), (int) (overlayAlpha * Color.green(baseColor) + (1f - overlayAlpha) * Color.green(overlayColor)), (int) (overlayAlpha * Color.blue(baseColor) + (1f - overlayAlpha) * Color.blue(overlayColor))); } /** * Computes the lightness value in HSL standard for the given color. */ private static float getLightnessForColor(int color) { int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); int largest = Math.max(red, Math.max(green, blue)); int smallest = Math.min(red, Math.min(green, blue)); int average = (largest + smallest) / 2; return average / 255.0f; } }