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