mirror of
https://github.com/moparisthebest/k-9
synced 2025-01-30 23:00:09 -05:00
Update HoloColorPicker
This commit is contained in:
parent
24c10f0fd3
commit
8084964c23
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user