From 8084964c23de61db455afdd663f203c22ef3700b Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 18 Jan 2013 14:15:56 +0100 Subject: [PATCH] Update HoloColorPicker --- .../larswerkman/colorpicker/ColorPicker.java | 96 ++++++++++--------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/plugins/HoloColorPicker/src/com/larswerkman/colorpicker/ColorPicker.java b/plugins/HoloColorPicker/src/com/larswerkman/colorpicker/ColorPicker.java index bca59e8b8..47ddf3d76 100644 --- a/plugins/HoloColorPicker/src/com/larswerkman/colorpicker/ColorPicker.java +++ b/plugins/HoloColorPicker/src/com/larswerkman/colorpicker/ColorPicker.java @@ -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); } /**