Remove framework-private HSV APIs, replace with luminance

Luminance is the measure used by W3C to compute contrast ratios, so
we should be preferring that for "brightness" comparisons.

Bug: 22815971
Change-Id: I84a971d3cc6b12acebe8e455c0c0440c1c8bce06
This commit is contained in:
Alan Viverette
2015-08-12 14:01:17 -04:00
parent c91edfafac
commit 81590a48a4
4 changed files with 16 additions and 82 deletions

View File

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

View File

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

View File

@@ -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 {

View File

@@ -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.
* <p>
* 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));
}
/**