|
|
|
@ -14,6 +14,8 @@
@@ -14,6 +14,8 @@
|
|
|
|
|
* limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6
|
|
|
|
|
|
|
|
|
|
package net.margaritov.preference.colorpicker; |
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
@ -38,6 +40,7 @@ import android.view.View;
@@ -38,6 +40,7 @@ import android.view.View;
|
|
|
|
|
* to select a color. A slider for the alpha channel is |
|
|
|
|
* also available. Enable it by setting |
|
|
|
|
* setAlphaSliderVisible(boolean) to true. |
|
|
|
|
* |
|
|
|
|
* @author Daniel Nilsson |
|
|
|
|
*/ |
|
|
|
|
public class ColorPickerView extends View { |
|
|
|
@ -220,9 +223,7 @@ public class ColorPickerView extends View {
@@ -220,9 +223,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
@Override |
|
|
|
|
protected void onDraw(Canvas canvas) { |
|
|
|
|
|
|
|
|
|
if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) return; |
|
|
|
|
|
|
|
|
|
drawSatValPanel(canvas); |
|
|
|
|
drawHuePanel(canvas); |
|
|
|
@ -236,18 +237,18 @@ public class ColorPickerView extends View {
@@ -236,18 +237,18 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
if (BORDER_WIDTH_PX > 0) { |
|
|
|
|
mBorderPaint.setColor(mBorderColor); |
|
|
|
|
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, |
|
|
|
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint); |
|
|
|
|
canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mValShader == null) { |
|
|
|
|
mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000, |
|
|
|
|
TileMode.CLAMP); |
|
|
|
|
mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, |
|
|
|
|
0xffffffff, 0xff000000, TileMode.CLAMP); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f}); |
|
|
|
|
|
|
|
|
|
mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP); |
|
|
|
|
mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, |
|
|
|
|
0xffffffff, rgb, TileMode.CLAMP); |
|
|
|
|
ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY); |
|
|
|
|
mSatValPaint.setShader(mShader); |
|
|
|
|
|
|
|
|
@ -269,13 +270,15 @@ public class ColorPickerView extends View {
@@ -269,13 +270,15 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
if (BORDER_WIDTH_PX > 0) { |
|
|
|
|
mBorderPaint.setColor(mBorderColor); |
|
|
|
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, |
|
|
|
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint); |
|
|
|
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, |
|
|
|
|
rect.top - BORDER_WIDTH_PX, |
|
|
|
|
rect.right + BORDER_WIDTH_PX, |
|
|
|
|
rect.bottom + BORDER_WIDTH_PX, |
|
|
|
|
mBorderPaint); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mHueShader == null) { |
|
|
|
|
mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, |
|
|
|
|
TileMode.CLAMP); |
|
|
|
|
mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, TileMode.CLAMP); |
|
|
|
|
mHuePaint.setShader(mHueShader); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -298,16 +301,17 @@ public class ColorPickerView extends View {
@@ -298,16 +301,17 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
private void drawAlphaPanel(Canvas canvas) { |
|
|
|
|
|
|
|
|
|
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) return; |
|
|
|
|
|
|
|
|
|
final RectF rect = mAlphaRect; |
|
|
|
|
|
|
|
|
|
if (BORDER_WIDTH_PX > 0) { |
|
|
|
|
mBorderPaint.setColor(mBorderColor); |
|
|
|
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, |
|
|
|
|
rect.bottom + BORDER_WIDTH_PX, mBorderPaint); |
|
|
|
|
canvas.drawRect(rect.left - BORDER_WIDTH_PX, |
|
|
|
|
rect.top - BORDER_WIDTH_PX, |
|
|
|
|
rect.right + BORDER_WIDTH_PX, |
|
|
|
|
rect.bottom + BORDER_WIDTH_PX, |
|
|
|
|
mBorderPaint); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -317,7 +321,8 @@ public class ColorPickerView extends View {
@@ -317,7 +321,8 @@ public class ColorPickerView extends View {
|
|
|
|
|
int color = Color.HSVToColor(hsv); |
|
|
|
|
int acolor = Color.HSVToColor(0, hsv); |
|
|
|
|
|
|
|
|
|
mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP); |
|
|
|
|
mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, |
|
|
|
|
color, acolor, TileMode.CLAMP); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mAlphaPaint.setShader(mAlphaShader); |
|
|
|
@ -593,9 +598,7 @@ public class ColorPickerView extends View {
@@ -593,9 +598,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
private boolean moveTrackersIfNeeded(MotionEvent event) { |
|
|
|
|
|
|
|
|
|
if (mStartTouchPoint == null) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (mStartTouchPoint == null) return false; |
|
|
|
|
|
|
|
|
|
boolean update = false; |
|
|
|
|
|
|
|
|
@ -760,9 +763,7 @@ public class ColorPickerView extends View {
@@ -760,9 +763,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
private void setUpAlphaRect() { |
|
|
|
|
|
|
|
|
|
if (!mShowAlphaPanel) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (!mShowAlphaPanel) return; |
|
|
|
|
|
|
|
|
|
final RectF dRect = mDrawingRect; |
|
|
|
|
|
|
|
|
@ -774,8 +775,12 @@ public class ColorPickerView extends View {
@@ -774,8 +775,12 @@ public class ColorPickerView extends View {
|
|
|
|
|
mAlphaRect = new RectF(left, top, right, bottom); |
|
|
|
|
|
|
|
|
|
mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity)); |
|
|
|
|
mAlphaPattern.setBounds(Math.round(mAlphaRect.left), Math.round(mAlphaRect.top), Math.round(mAlphaRect.right), |
|
|
|
|
Math.round(mAlphaRect.bottom)); |
|
|
|
|
mAlphaPattern.setBounds( |
|
|
|
|
Math.round(mAlphaRect.left), |
|
|
|
|
Math.round(mAlphaRect.top), |
|
|
|
|
Math.round(mAlphaRect.right), |
|
|
|
|
Math.round(mAlphaRect.bottom) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -783,6 +788,7 @@ public class ColorPickerView extends View {
@@ -783,6 +788,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
/** |
|
|
|
|
* Set a OnColorChangedListener to get notified when the color |
|
|
|
|
* selected by the user has changed. |
|
|
|
|
* |
|
|
|
|
* @param listener |
|
|
|
|
*/ |
|
|
|
|
public void setOnColorChangedListener(OnColorChangedListener listener) { |
|
|
|
@ -791,6 +797,7 @@ public class ColorPickerView extends View {
@@ -791,6 +797,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set the color of the border surrounding all panels. |
|
|
|
|
* |
|
|
|
|
* @param color |
|
|
|
|
*/ |
|
|
|
|
public void setBorderColor(int color) { |
|
|
|
@ -807,6 +814,7 @@ public class ColorPickerView extends View {
@@ -807,6 +814,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the current color this view is showing. |
|
|
|
|
* |
|
|
|
|
* @return the current color. |
|
|
|
|
*/ |
|
|
|
|
public int getColor() { |
|
|
|
@ -815,6 +823,7 @@ public class ColorPickerView extends View {
@@ -815,6 +823,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set the color the view should show. |
|
|
|
|
* |
|
|
|
|
* @param color The color that should be selected. |
|
|
|
|
*/ |
|
|
|
|
public void setColor(int color) { |
|
|
|
@ -823,6 +832,7 @@ public class ColorPickerView extends View {
@@ -823,6 +832,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set the color this view should show. |
|
|
|
|
* |
|
|
|
|
* @param color The color that should be selected. |
|
|
|
|
* @param callback If you want to get a callback to |
|
|
|
|
* your OnColorChangedListener. |
|
|
|
@ -830,13 +840,10 @@ public class ColorPickerView extends View {
@@ -830,13 +840,10 @@ public class ColorPickerView extends View {
|
|
|
|
|
public void setColor(int color, boolean callback) { |
|
|
|
|
|
|
|
|
|
int alpha = Color.alpha(color); |
|
|
|
|
int red = Color.red(color); |
|
|
|
|
int blue = Color.blue(color); |
|
|
|
|
int green = Color.green(color); |
|
|
|
|
|
|
|
|
|
float[] hsv = new float[3]; |
|
|
|
|
|
|
|
|
|
Color.RGBToHSV(red, green, blue, hsv); |
|
|
|
|
Color.colorToHSV(color, hsv); |
|
|
|
|
|
|
|
|
|
mAlpha = alpha; |
|
|
|
|
mHue = hsv[0]; |
|
|
|
@ -856,6 +863,7 @@ public class ColorPickerView extends View {
@@ -856,6 +863,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
* a panel to the side of the view minus the padding. |
|
|
|
|
* Useful if you want to have your own panel below showing |
|
|
|
|
* the currently selected color and want to align it perfectly. |
|
|
|
|
* |
|
|
|
|
* @return The offset in pixels. |
|
|
|
|
*/ |
|
|
|
|
public float getDrawingOffset() { |
|
|
|
@ -865,6 +873,7 @@ public class ColorPickerView extends View {
@@ -865,6 +873,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
/** |
|
|
|
|
* Set if the user is allowed to adjust the alpha panel. Default is false. |
|
|
|
|
* If it is set to false no alpha will be set. |
|
|
|
|
* |
|
|
|
|
* @param visible |
|
|
|
|
*/ |
|
|
|
|
public void setAlphaSliderVisible(boolean visible) { |
|
|
|
@ -881,13 +890,16 @@ public class ColorPickerView extends View {
@@ -881,13 +890,16 @@ public class ColorPickerView extends View {
|
|
|
|
|
mSatShader = null; |
|
|
|
|
mHueShader = null; |
|
|
|
|
mAlphaShader = null; |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
requestLayout(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean getAlphaSliderVisible() { |
|
|
|
|
return mShowAlphaPanel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setSliderTrackerColor(int color) { |
|
|
|
|
mSliderTrackerColor = color; |
|
|
|
|
|
|
|
|
@ -903,6 +915,7 @@ public class ColorPickerView extends View {
@@ -903,6 +915,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
/** |
|
|
|
|
* Set the text that should be shown in the |
|
|
|
|
* alpha slider. Set to null to disable text. |
|
|
|
|
* |
|
|
|
|
* @param res string resource id. |
|
|
|
|
*/ |
|
|
|
|
public void setAlphaSliderText(int res) { |
|
|
|
@ -913,6 +926,7 @@ public class ColorPickerView extends View {
@@ -913,6 +926,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
/** |
|
|
|
|
* Set the text that should be shown in the |
|
|
|
|
* alpha slider. Set to null to disable text. |
|
|
|
|
* |
|
|
|
|
* @param text Text that should be shown. |
|
|
|
|
*/ |
|
|
|
|
public void setAlphaSliderText(String text) { |
|
|
|
@ -924,6 +938,7 @@ public class ColorPickerView extends View {
@@ -924,6 +938,7 @@ public class ColorPickerView extends View {
|
|
|
|
|
* Get the current value of the text |
|
|
|
|
* that will be shown in the alpha |
|
|
|
|
* slider. |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public String getAlphaSliderText() { |
|
|
|
|