Update HoloColorPicker

This commit is contained in:
cketti 2013-01-18 14:15:56 +01:00
parent 24c10f0fd3
commit 8084964c23
1 changed files with 50 additions and 46 deletions

View File

@ -56,6 +56,55 @@ public class ColorPicker extends View {
0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
/**
* Get a random color.
*
* @return The ARGB value of a randomly selected color.
*/
public static int getRandomColor() {
return calculateColor((float) (Math.random() * 2 * Math.PI));
}
private static int ave(int s, int d, float p) {
return s + java.lang.Math.round(p * (d - s));
}
/**
* Calculate the color using the supplied angle.
*
* @param angle
* The selected color's position expressed as angle (in rad).
*
* @return The ARGB value of the color on the color wheel at the specified angle.
*/
private static int calculateColor(float angle) {
float unit = (float) (angle / (2 * Math.PI));
if (unit < 0) {
unit += 1;
}
if (unit <= 0) {
return COLORS[0];
}
if (unit >= 1) {
return COLORS[COLORS.length - 1];
}
float p = unit * (COLORS.length - 1);
int i = (int) p;
p -= i;
int c0 = COLORS[i];
int c1 = COLORS[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
/**
* {@code Paint} instance used to draw the color wheel.
*/
@ -94,11 +143,6 @@ public class ColorPicker extends View {
*/
private boolean mUserIsMovingPointer = false;
/**
* The ARGB value of the currently selected color.
*/
private int mColor;
/**
* Number of pixels the origin of this view is moved in X- and Y-direction.
*
@ -204,53 +248,13 @@ public class ColorPicker extends View {
mColorWheelRadius);
}
private int ave(int s, int d, float p) {
return s + java.lang.Math.round(p * (d - s));
}
/**
* Calculate the color using the supplied angle.
*
* @param angle
* The selected color's position expressed as angle (in rad).
*
* @return The ARGB value of the color on the color wheel at the specified angle.
*/
private int calculateColor(float angle) {
float unit = (float) (angle / (2 * Math.PI));
if (unit < 0) {
unit += 1;
}
if (unit <= 0) {
return COLORS[0];
}
if (unit >= 1) {
return COLORS[COLORS.length - 1];
}
float p = unit * (COLORS.length - 1);
int i = (int) p;
p -= i;
int c0 = COLORS[i];
int c1 = COLORS[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
mColor = Color.argb(a, r, g, b);
return Color.argb(a, r, g, b);
}
/**
* Get the currently selected color.
*
* @return The ARGB value of the currently selected color.
*/
public int getColor() {
return mColor;
return calculateColor(mAngle);
}
/**