diff --git a/api/current.txt b/api/current.txt index d7fce791085f6..37954fe19a932 100644 --- a/api/current.txt +++ b/api/current.txt @@ -11276,6 +11276,7 @@ package android.graphics { method public static int blue(int); method public static void colorToHSV(int, float[]); method public static int green(int); + method public static float luminance(int); method public static int parseColor(java.lang.String); method public static int red(int); method public static int rgb(int, int, int); diff --git a/api/system-current.txt b/api/system-current.txt index ece53276167eb..d19175993d148 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -11613,6 +11613,7 @@ package android.graphics { method public static int blue(int); method public static void colorToHSV(int, float[]); method public static int green(int); + method public static float luminance(int); method public static int parseColor(java.lang.String); method public static int red(int); method public static int rgb(int, int, int); diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java index 1068b512307dd..cae84732d31f0 100644 --- a/core/java/com/android/internal/policy/PhoneWindow.java +++ b/core/java/com/android/internal/policy/PhoneWindow.java @@ -4021,7 +4021,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { // dark or the light button frame. TypedValue value = new TypedValue(); getContext().getTheme().resolveAttribute(R.attr.colorPrimary, value, true); - if (Color.brightness(value.data) < 0.5) { + if (Color.luminance(value.data) < 0.5) { nonClientDecorView = (NonClientDecorView) mLayoutInflater.inflate( R.layout.non_client_decor_dark, null); } else { diff --git a/graphics/java/android/graphics/Color.java b/graphics/java/android/graphics/Color.java index 23d26eb88108c..80e79f61d236c 100644 --- a/graphics/java/android/graphics/Color.java +++ b/graphics/java/android/graphics/Color.java @@ -117,89 +117,21 @@ public class Color { } /** - * Returns the hue component of a color int. - * - * @return A value between 0.0f and 1.0f - * - * @hide Pending API council - */ - public static float hue(@ColorInt int color) { - int r = (color >> 16) & 0xFF; - int g = (color >> 8) & 0xFF; - int b = color & 0xFF; - - int V = Math.max(b, Math.max(r, g)); - int temp = Math.min(b, Math.min(r, g)); - - float H; - - if (V == temp) { - H = 0; - } else { - final float vtemp = (float) (V - temp); - final float cr = (V - r) / vtemp; - final float cg = (V - g) / vtemp; - final float cb = (V - b) / vtemp; - - if (r == V) { - H = cb - cg; - } else if (g == V) { - H = 2 + cr - cb; - } else { - H = 4 + cg - cr; - } - - H /= 6.f; - if (H < 0) { - H++; - } - } - - return H; - } - - /** - * Returns the saturation component of a color int. - * - * @return A value between 0.0f and 1.0f - * - * @hide Pending API council - */ - public static float saturation(@ColorInt int color) { - int r = (color >> 16) & 0xFF; - int g = (color >> 8) & 0xFF; - int b = color & 0xFF; - - - int V = Math.max(b, Math.max(r, g)); - int temp = Math.min(b, Math.min(r, g)); - - float S; - - if (V == temp) { - S = 0; - } else { - S = (V - temp) / (float) V; - } - - return S; - } - - /** - * Returns the brightness component of a color int. + * Returns the relative luminance of a color. + *

+ * Assumes sRGB encoding. Based on the formula for relative luminance + * defined in WCAG 2.0, W3C Recommendation 11 December 2008. * - * @return A value between 0.0f and 1.0f - * - * @hide Pending API council + * @return a value between 0 (darkest black) and 1 (lightest white) */ - public static float brightness(@ColorInt int color) { - int r = (color >> 16) & 0xFF; - int g = (color >> 8) & 0xFF; - int b = color & 0xFF; - - int V = Math.max(b, Math.max(r, g)); - - return (V / 255.f); + public static float luminance(@ColorInt int color) { + double red = Color.red(color) / 255.0; + red = red < 0.03928 ? red / 12.92 : Math.pow((red + 0.055) / 1.055, 2.4); + double green = Color.green(color) / 255.0; + green = green < 0.03928 ? green / 12.92 : Math.pow((green + 0.055) / 1.055, 2.4); + double blue = Color.blue(color) / 255.0; + blue = blue < 0.03928 ? blue / 12.92 : Math.pow((blue + 0.055) / 1.055, 2.4); + return (float) ((0.2126 * red) + (0.7152 * green) + (0.0722 * blue)); } /**