From 660478aafcbceb32e633285b2a37fa7607de9233 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Sat, 20 Jun 2020 01:37:04 -0400 Subject: [PATCH] Update ColorPicker to 696bb05 --- app/build.gradle | 1 + .../colorpicker/AlphaPatternDrawable.java | 141 +- .../colorpicker/ColorPickerDialog.java | 321 ++-- .../colorpicker/ColorPickerPanelView.java | 210 +-- .../colorpicker/ColorPickerPreference.java | 551 ++++--- .../colorpicker/ColorPickerView.java | 1357 +++++++++-------- .../res/layout-land/dialog_color_picker.xml | 131 +- .../main/res/layout/dialog_color_picker.xml | 137 +- 8 files changed, 1533 insertions(+), 1316 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 18044712..6791fdb0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -78,6 +78,7 @@ android { dependencies { // Android support implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.preference:preference:1.1.1' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' implementation 'com.google.android.material:material:1.1.0' diff --git a/app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java b/app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java index dbd04276..c31dad08 100644 --- a/app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java +++ b/app/src/main/java/net/margaritov/preference/colorpicker/AlphaPatternDrawable.java @@ -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.graphics.Bitmap; @@ -28,101 +30,102 @@ import android.graphics.drawable.Drawable; * This drawable that draws a simple white and gray chessboard pattern. * It's pattern you will often see as a background behind a * partly transparent image in many applications. + * * @author Daniel Nilsson */ public class AlphaPatternDrawable extends Drawable { - private int mRectangleSize = 10; + private int mRectangleSize = 10; - private Paint mPaint = new Paint(); - private Paint mPaintWhite = new Paint(); - private Paint mPaintGray = new Paint(); + private Paint mPaint = new Paint(); + private Paint mPaintWhite = new Paint(); + private Paint mPaintGray = new Paint(); - private int numRectanglesHorizontal; - private int numRectanglesVertical; + private int numRectanglesHorizontal; + private int numRectanglesVertical; - /** - * Bitmap in which the pattern will be cahched. - */ - private Bitmap mBitmap; + /** + * Bitmap in which the pattern will be cahched. + */ + private Bitmap mBitmap; - public AlphaPatternDrawable(int rectangleSize) { - mRectangleSize = rectangleSize; - mPaintWhite.setColor(0xffffffff); - mPaintGray.setColor(0xffcbcbcb); - } + public AlphaPatternDrawable(int rectangleSize) { + mRectangleSize = rectangleSize; + mPaintWhite.setColor(0xffffffff); + mPaintGray.setColor(0xffcbcbcb); + } - @Override - public void draw(Canvas canvas) { - canvas.drawBitmap(mBitmap, null, getBounds(), mPaint); - } + @Override + public void draw(Canvas canvas) { + canvas.drawBitmap(mBitmap, null, getBounds(), mPaint); + } - @Override - public int getOpacity() { - return 0; - } + @Override + public int getOpacity() { + return 0; + } - @Override - public void setAlpha(int alpha) { - throw new UnsupportedOperationException("Alpha is not supported by this drawwable."); - } + @Override + public void setAlpha(int alpha) { + throw new UnsupportedOperationException("Alpha is not supported by this drawwable."); + } - @Override - public void setColorFilter(ColorFilter cf) { - throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable."); - } + @Override + public void setColorFilter(ColorFilter cf) { + throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable."); + } - @Override - protected void onBoundsChange(Rect bounds) { - super.onBoundsChange(bounds); + @Override + protected void onBoundsChange(Rect bounds) { + super.onBoundsChange(bounds); - int height = bounds.height(); - int width = bounds.width(); + int height = bounds.height(); + int width = bounds.width(); - numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize)); - numRectanglesVertical = (int) Math.ceil(height / mRectangleSize); + numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize)); + numRectanglesVertical = (int) Math.ceil(height / mRectangleSize); - generatePatternBitmap(); + generatePatternBitmap(); - } + } - /** - * This will generate a bitmap with the pattern - * as big as the rectangle we were allow to draw on. - * We do this to chache the bitmap so we don't need to - * recreate it each time draw() is called since it - * takes a few milliseconds. - */ - private void generatePatternBitmap() { + /** + * This will generate a bitmap with the pattern + * as big as the rectangle we were allow to draw on. + * We do this to chache the bitmap so we don't need to + * recreate it each time draw() is called since it + * takes a few milliseconds. + */ + private void generatePatternBitmap() { - if (getBounds().width() <= 0 || getBounds().height() <= 0) { - return; - } + if (getBounds().width() <= 0 || getBounds().height() <= 0) { + return; + } - mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888); - Canvas canvas = new Canvas(mBitmap); + mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888); + Canvas canvas = new Canvas(mBitmap); - Rect r = new Rect(); - boolean verticalStartWhite = true; - for (int i = 0; i <= numRectanglesVertical; i++) { + Rect r = new Rect(); + boolean verticalStartWhite = true; + for (int i = 0; i <= numRectanglesVertical; i++) { - boolean isWhite = verticalStartWhite; - for (int j = 0; j <= numRectanglesHorizontal; j++) { + boolean isWhite = verticalStartWhite; + for (int j = 0; j <= numRectanglesHorizontal; j++) { - r.top = i * mRectangleSize; - r.left = j * mRectangleSize; - r.bottom = r.top + mRectangleSize; - r.right = r.left + mRectangleSize; + r.top = i * mRectangleSize; + r.left = j * mRectangleSize; + r.bottom = r.top + mRectangleSize; + r.right = r.left + mRectangleSize; - canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray); + canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray); - isWhite = !isWhite; - } + isWhite = !isWhite; + } - verticalStartWhite = !verticalStartWhite; + verticalStartWhite = !verticalStartWhite; - } + } - } + } -} \ No newline at end of file +} diff --git a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java index fddf6984..aa0e96fb 100644 --- a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java +++ b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerDialog.java @@ -14,123 +14,240 @@ * limitations under the License. */ +// Taken from https://github.com/attenzione/android-ColorPickerPreference/tree/696bb050527d6a7ae14e47b7472a0640a7ff08e6 + package net.margaritov.preference.colorpicker; -import android.app.Dialog; import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Color; import android.graphics.PixelFormat; import android.os.Bundle; +import android.text.InputFilter; +import android.text.InputType; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewTreeObserver; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.TextView; -import org.transdroid.R; - -public class ColorPickerDialog extends Dialog implements ColorPickerView.OnColorChangedListener, View.OnClickListener { - - private ColorPickerView mColorPicker; - - private ColorPickerPanelView mOldColor; - private ColorPickerPanelView mNewColor; - - private OnColorChangedListener mListener; - - public interface OnColorChangedListener { - public void onColorChanged(int color); - } - - public ColorPickerDialog(Context context, int initialColor) { - super(context); - - init(initialColor); - } - - private void init(int color) { - // To fight color banding. - getWindow().setFormat(PixelFormat.RGBA_8888); - - setUp(color); - - } +import androidx.appcompat.app.AppCompatDialog; - private void setUp(int color) { - - LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - View layout = inflater.inflate(R.layout.dialog_color_picker, null); - - setContentView(layout); - - setTitle(R.string.dialog_color_picker); - - mColorPicker = (ColorPickerView) layout.findViewById(R.id.color_picker_view); - mOldColor = (ColorPickerPanelView) layout.findViewById(R.id.old_color_panel); - mNewColor = (ColorPickerPanelView) layout.findViewById(R.id.new_color_panel); - - ((LinearLayout) mOldColor.getParent()) - .setPadding(Math.round(mColorPicker.getDrawingOffset()), 0, Math.round(mColorPicker.getDrawingOffset()), - 0); - - mOldColor.setOnClickListener(this); - mNewColor.setOnClickListener(this); - mColorPicker.setOnColorChangedListener(this); - mOldColor.setColor(color); - mColorPicker.setColor(color, true); - - } - - @Override - public void onColorChanged(int color) { +import org.transdroid.R; - mNewColor.setColor(color); +import java.util.Locale; + +public class ColorPickerDialog + extends + AppCompatDialog + implements + ColorPickerView.OnColorChangedListener, + View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener { + + private ColorPickerView mColorPicker; + + private ColorPickerPanelView mOldColor; + private ColorPickerPanelView mNewColor; + + private EditText mHexVal; + private boolean mHexValueEnabled = false; + private ColorStateList mHexDefaultTextColor; + + private OnColorChangedListener mListener; + private int mOrientation; + private View mLayout; + + private String mTitle; + + @Override + public void onGlobalLayout() { + if (getContext().getResources().getConfiguration().orientation != mOrientation) { + final int oldcolor = mOldColor.getColor(); + final int newcolor = mNewColor.getColor(); + mLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); + setUp(oldcolor); + mNewColor.setColor(newcolor); + mColorPicker.setColor(newcolor); + } + } + + public interface OnColorChangedListener { + public void onColorChanged(int color); + } + + public ColorPickerDialog(Context context, int initialColor, String title) { + super(context); + + mTitle = title; + init(initialColor); + } + + private void init(int color) { + // To fight color banding. + getWindow().setFormat(PixelFormat.RGBA_8888); + + setUp(color); + + } + + private void setUp(int color) { + + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + mLayout = inflater.inflate(R.layout.dialog_color_picker, null); + mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this); + + mOrientation = getContext().getResources().getConfiguration().orientation; + setContentView(mLayout); + + setTitle(mTitle); + + mColorPicker = (ColorPickerView) mLayout.findViewById(R.id.color_picker_view); + mOldColor = (ColorPickerPanelView) mLayout.findViewById(R.id.old_color_panel); + mNewColor = (ColorPickerPanelView) mLayout.findViewById(R.id.new_color_panel); + + mHexVal = (EditText) mLayout.findViewById(R.id.hex_val); + mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); + mHexDefaultTextColor = mHexVal.getTextColors(); + + mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() { + + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == EditorInfo.IME_ACTION_DONE) { + InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(v.getWindowToken(), 0); + String s = mHexVal.getText().toString(); + if (s.length() > 5 || s.length() < 10) { + try { + int c = ColorPickerPreference.convertToColorInt(s.toString()); + mColorPicker.setColor(c, true); + mHexVal.setTextColor(mHexDefaultTextColor); + } catch (IllegalArgumentException e) { + mHexVal.setTextColor(Color.RED); + } + } else { + mHexVal.setTextColor(Color.RED); + } + return true; + } + return false; + } + }); + + ((LinearLayout) mOldColor.getParent()).setPadding( + Math.round(mColorPicker.getDrawingOffset()), + 0, + Math.round(mColorPicker.getDrawingOffset()), + 0 + ); + + mOldColor.setOnClickListener(this); + mNewColor.setOnClickListener(this); + mColorPicker.setOnColorChangedListener(this); + mOldColor.setColor(color); + mColorPicker.setColor(color, true); + + } + + @Override + public void onColorChanged(int color) { + + mNewColor.setColor(color); + + if (mHexValueEnabled) + updateHexValue(color); /* - if (mListener != null) { + if (mListener != null) { mListener.onColorChanged(color); } */ - } - - public void setAlphaSliderVisible(boolean visible) { - mColorPicker.setAlphaSliderVisible(visible); - } - - /** - * Set a OnColorChangedListener to get notified when the color - * selected by the user has changed. - * @param listener - */ - public void setOnColorChangedListener(OnColorChangedListener listener) { - mListener = listener; - } - - public int getColor() { - return mColorPicker.getColor(); - } - - @Override - public void onClick(View v) { - if (v.getId() == R.id.new_color_panel) { - if (mListener != null) { - mListener.onColorChanged(mNewColor.getColor()); - } - } - dismiss(); - } - - @Override - public Bundle onSaveInstanceState() { - Bundle state = super.onSaveInstanceState(); - state.putInt("old_color", mOldColor.getColor()); - state.putInt("new_color", mNewColor.getColor()); - return state; - } - - @Override - public void onRestoreInstanceState(Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - mOldColor.setColor(savedInstanceState.getInt("old_color")); - mColorPicker.setColor(savedInstanceState.getInt("new_color"), true); - } + } + + public void setHexValueEnabled(boolean enable) { + mHexValueEnabled = enable; + if (enable) { + mHexVal.setVisibility(View.VISIBLE); + updateHexLengthFilter(); + updateHexValue(getColor()); + } else + mHexVal.setVisibility(View.GONE); + } + + public boolean getHexValueEnabled() { + return mHexValueEnabled; + } + + private void updateHexLengthFilter() { + if (getAlphaSliderVisible()) + mHexVal.setFilters(new InputFilter[]{new InputFilter.LengthFilter(9)}); + else + mHexVal.setFilters(new InputFilter[]{new InputFilter.LengthFilter(7)}); + } + + private void updateHexValue(int color) { + if (getAlphaSliderVisible()) { + mHexVal.setText(ColorPickerPreference.convertToARGB(color).toUpperCase(Locale.getDefault())); + } else { + mHexVal.setText(ColorPickerPreference.convertToRGB(color).toUpperCase(Locale.getDefault())); + } + mHexVal.setTextColor(mHexDefaultTextColor); + } + + public void setAlphaSliderVisible(boolean visible) { + mColorPicker.setAlphaSliderVisible(visible); + if (mHexValueEnabled) { + updateHexLengthFilter(); + updateHexValue(getColor()); + } + } + + public boolean getAlphaSliderVisible() { + return mColorPicker.getAlphaSliderVisible(); + } + + /** + * Set a OnColorChangedListener to get notified when the color + * selected by the user has changed. + * + * @param listener + */ + public void setOnColorChangedListener(OnColorChangedListener listener) { + mListener = listener; + } + + public int getColor() { + return mColorPicker.getColor(); + } + + @Override + public void onClick(View v) { + if (v.getId() == R.id.new_color_panel) { + if (mListener != null) { + mListener.onColorChanged(mNewColor.getColor()); + } + } + dismiss(); + } + + @Override + public Bundle onSaveInstanceState() { + Bundle state = super.onSaveInstanceState(); + state.putInt("old_color", mOldColor.getColor()); + state.putInt("new_color", mNewColor.getColor()); + return state; + } + + @Override + public void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + mOldColor.setColor(savedInstanceState.getInt("old_color")); + mColorPicker.setColor(savedInstanceState.getInt("new_color"), true); + } } diff --git a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java index 4b305793..60716e6d 100644 --- a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java +++ b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPanelView.java @@ -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; @@ -27,140 +29,148 @@ import android.view.View; * This class draws a panel which which will be filled with a color which can be set. * It can be used to show the currently selected color which you will get from * the {@link ColorPickerView}. + * * @author Daniel Nilsson */ public class ColorPickerPanelView extends View { - /** - * The width in pixels of the border - * surrounding the color panel. - */ - private final static float BORDER_WIDTH_PX = 1; - - private float mDensity = 1f; - - private int mBorderColor = 0xff6E6E6E; - private int mColor = 0xff000000; + /** + * The width in pixels of the border + * surrounding the color panel. + */ + private final static float BORDER_WIDTH_PX = 1; - private Paint mBorderPaint; - private Paint mColorPaint; + private float mDensity = 1f; - private RectF mDrawingRect; - private RectF mColorRect; + private int mBorderColor = 0xff6E6E6E; + private int mColor = 0xff000000; - private AlphaPatternDrawable mAlphaPattern; + private Paint mBorderPaint; + private Paint mColorPaint; + private RectF mDrawingRect; + private RectF mColorRect; - public ColorPickerPanelView(Context context) { - this(context, null); - } + private AlphaPatternDrawable mAlphaPattern; - public ColorPickerPanelView(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } - public ColorPickerPanelView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } + public ColorPickerPanelView(Context context) { + this(context, null); + } - private void init() { - mBorderPaint = new Paint(); - mColorPaint = new Paint(); - mDensity = getContext().getResources().getDisplayMetrics().density; - } + public ColorPickerPanelView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + public ColorPickerPanelView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } - @Override - protected void onDraw(Canvas canvas) { + private void init() { + mBorderPaint = new Paint(); + mColorPaint = new Paint(); + mDensity = getContext().getResources().getDisplayMetrics().density; + } - final RectF rect = mColorRect; - if (BORDER_WIDTH_PX > 0) { - mBorderPaint.setColor(mBorderColor); - canvas.drawRect(mDrawingRect, mBorderPaint); - } + @Override + protected void onDraw(Canvas canvas) { - if (mAlphaPattern != null) { - mAlphaPattern.draw(canvas); - } + final RectF rect = mColorRect; - mColorPaint.setColor(mColor); + if (BORDER_WIDTH_PX > 0) { + mBorderPaint.setColor(mBorderColor); + canvas.drawRect(mDrawingRect, mBorderPaint); + } - canvas.drawRect(rect, mColorPaint); - } + if (mAlphaPattern != null) { + mAlphaPattern.draw(canvas); + } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + mColorPaint.setColor(mColor); - int width = MeasureSpec.getSize(widthMeasureSpec); - int height = MeasureSpec.getSize(heightMeasureSpec); + canvas.drawRect(rect, mColorPaint); + } - setMeasuredDimension(width, height); - } + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); + int width = MeasureSpec.getSize(widthMeasureSpec); + int height = MeasureSpec.getSize(heightMeasureSpec); - mDrawingRect = new RectF(); - mDrawingRect.left = getPaddingLeft(); - mDrawingRect.right = w - getPaddingRight(); - mDrawingRect.top = getPaddingTop(); - mDrawingRect.bottom = h - getPaddingBottom(); + setMeasuredDimension(width, height); + } - setUpColorRect(); + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); - } + mDrawingRect = new RectF(); + mDrawingRect.left = getPaddingLeft(); + mDrawingRect.right = w - getPaddingRight(); + mDrawingRect.top = getPaddingTop(); + mDrawingRect.bottom = h - getPaddingBottom(); - private void setUpColorRect() { - final RectF dRect = mDrawingRect; + setUpColorRect(); - float left = dRect.left + BORDER_WIDTH_PX; - float top = dRect.top + BORDER_WIDTH_PX; - float bottom = dRect.bottom - BORDER_WIDTH_PX; - float right = dRect.right - BORDER_WIDTH_PX; + } - mColorRect = new RectF(left, top, right, bottom); + private void setUpColorRect() { + final RectF dRect = mDrawingRect; - mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity)); + float left = dRect.left + BORDER_WIDTH_PX; + float top = dRect.top + BORDER_WIDTH_PX; + float bottom = dRect.bottom - BORDER_WIDTH_PX; + float right = dRect.right - BORDER_WIDTH_PX; - mAlphaPattern.setBounds(Math.round(mColorRect.left), Math.round(mColorRect.top), Math.round(mColorRect.right), - Math.round(mColorRect.bottom)); + mColorRect = new RectF(left, top, right, bottom); - } + mAlphaPattern = new AlphaPatternDrawable((int) (5 * mDensity)); - /** - * Set the color that should be shown by this view. - * @param color - */ - public void setColor(int color) { - mColor = color; - invalidate(); - } + mAlphaPattern.setBounds( + Math.round(mColorRect.left), + Math.round(mColorRect.top), + Math.round(mColorRect.right), + Math.round(mColorRect.bottom) + ); - /** - * Get the color currently show by this view. - * @return - */ - public int getColor() { - return mColor; - } + } - /** - * Set the color of the border surrounding the panel. - * @param color - */ - public void setBorderColor(int color) { - mBorderColor = color; - invalidate(); - } + /** + * Set the color that should be shown by this view. + * + * @param color + */ + public void setColor(int color) { + mColor = color; + invalidate(); + } - /** - * Get the color of the border surrounding the panel. - */ - public int getBorderColor() { - return mBorderColor; - } + /** + * Get the color currently show by this view. + * + * @return + */ + public int getColor() { + return mColor; + } + + /** + * Set the color of the border surrounding the panel. + * + * @param color + */ + public void setBorderColor(int color) { + mBorderColor = color; + invalidate(); + } + + /** + * Get the color of the border surrounding the panel. + */ + public int getBorderColor() { + return mBorderColor; + } -} \ No newline at end of file +} diff --git a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java index da9a50fe..ca211059 100644 --- a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java +++ b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java @@ -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; @@ -24,259 +26,312 @@ import android.graphics.Color; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; -import android.preference.Preference; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; + /** - * A preference type that allows a user to choose a time + * A preference type that allows a user to choose a color + * * @author Sergey Margaritov */ -public class ColorPickerPreference extends Preference - implements Preference.OnPreferenceClickListener, ColorPickerDialog.OnColorChangedListener { - - View mView; - ColorPickerDialog mDialog; - private int mValue = Color.BLACK; - private float mDensity = 0; - private boolean mAlphaSliderEnabled = false; - - public ColorPickerPreference(Context context) { - super(context); - init(context, null); - } - - public ColorPickerPreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs); - } - - public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(context, attrs); - } - - @Override - protected Object onGetDefaultValue(TypedArray a, int index) { - return a.getColor(index, Color.BLACK); - } - - @Override - protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { - onColorChanged(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue); - } - - private void init(Context context, AttributeSet attrs) { - mDensity = getContext().getResources().getDisplayMetrics().density; - setOnPreferenceClickListener(this); - if (attrs != null) { - mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false); - } - } - - @Override - protected void onBindView(View view) { - super.onBindView(view); - mView = view; - setPreviewColor(); - } - - private void setPreviewColor() { - if (mView == null) { - return; - } - ImageView iView = new ImageView(getContext()); - LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame)); - if (widgetFrameView == null) { - return; - } - widgetFrameView.setVisibility(View.VISIBLE); - widgetFrameView - .setPadding(widgetFrameView.getPaddingLeft(), widgetFrameView.getPaddingTop(), (int) (mDensity * 8), - widgetFrameView.getPaddingBottom()); - // remove already create preview image - int count = widgetFrameView.getChildCount(); - if (count > 0) { - widgetFrameView.removeViews(0, count); - } - widgetFrameView.addView(iView); - widgetFrameView.setMinimumWidth(0); - iView.setBackgroundDrawable(new AlphaPatternDrawable((int) (5 * mDensity))); - iView.setImageBitmap(getPreviewBitmap()); - } - - private Bitmap getPreviewBitmap() { - int d = (int) (mDensity * 31); //30dip - int color = mValue; - Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888); - int w = bm.getWidth(); - int h = bm.getHeight(); - int c = color; - for (int i = 0; i < w; i++) { - for (int j = i; j < h; j++) { - c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : color; - bm.setPixel(i, j, c); - if (i != j) { - bm.setPixel(j, i, c); - } - } - } - - return bm; - } - - @Override - public void onColorChanged(int color) { - if (isPersistent()) { - persistInt(color); - } - mValue = color; - setPreviewColor(); - try { - getOnPreferenceChangeListener().onPreferenceChange(this, color); - } catch (NullPointerException e) { - - } - } - - public boolean onPreferenceClick(Preference preference) { - showDialog(null); - return false; - } - - protected void showDialog(Bundle state) { - mDialog = new ColorPickerDialog(getContext(), mValue); - mDialog.setOnColorChangedListener(this); - if (mAlphaSliderEnabled) { - mDialog.setAlphaSliderVisible(true); - } - if (state != null) { - mDialog.onRestoreInstanceState(state); - } - mDialog.show(); - } - - /** - * Toggle Alpha Slider visibility (by default it's disabled) - * @param enable - */ - public void setAlphaSliderEnabled(boolean enable) { - mAlphaSliderEnabled = enable; - } - - /** - * For custom purposes. Not used by ColorPickerPreferrence - * @param color - * @author Unknown - */ - public static String convertToARGB(int color) { - String alpha = Integer.toHexString(Color.alpha(color)); - String red = Integer.toHexString(Color.red(color)); - String green = Integer.toHexString(Color.green(color)); - String blue = Integer.toHexString(Color.blue(color)); - - if (alpha.length() == 1) { - alpha = "0" + alpha; - } - - if (red.length() == 1) { - red = "0" + red; - } - - if (green.length() == 1) { - green = "0" + green; - } - - if (blue.length() == 1) { - blue = "0" + blue; - } - - return "#" + alpha + red + green + blue; - } - - /** - * For custom purposes. Not used by ColorPickerPreferrence - * @param argb - * @throws NumberFormatException - * @author Unknown - */ - public static int convertToColorInt(String argb) throws NumberFormatException { - - if (argb.startsWith("#")) { - argb = argb.replace("#", ""); - } - - int alpha = -1, red = -1, green = -1, blue = -1; - - if (argb.length() == 8) { - alpha = Integer.parseInt(argb.substring(0, 2), 16); - red = Integer.parseInt(argb.substring(2, 4), 16); - green = Integer.parseInt(argb.substring(4, 6), 16); - blue = Integer.parseInt(argb.substring(6, 8), 16); - } else if (argb.length() == 6) { - alpha = 255; - red = Integer.parseInt(argb.substring(0, 2), 16); - green = Integer.parseInt(argb.substring(2, 4), 16); - blue = Integer.parseInt(argb.substring(4, 6), 16); - } - - return Color.argb(alpha, red, green, blue); - } - - @Override - protected Parcelable onSaveInstanceState() { - final Parcelable superState = super.onSaveInstanceState(); - if (mDialog == null || !mDialog.isShowing()) { - return superState; - } - - final SavedState myState = new SavedState(superState); - myState.dialogBundle = mDialog.onSaveInstanceState(); - return myState; - } - - @Override - protected void onRestoreInstanceState(Parcelable state) { - if (state == null || !(state instanceof SavedState)) { - // Didn't save state for us in onSaveInstanceState - super.onRestoreInstanceState(state); - return; - } - - SavedState myState = (SavedState) state; - super.onRestoreInstanceState(myState.getSuperState()); - showDialog(myState.dialogBundle); - } - - private static class SavedState extends BaseSavedState { - Bundle dialogBundle; - - public SavedState(Parcel source) { - super(source); - dialogBundle = source.readBundle(); - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeBundle(dialogBundle); - } - - public SavedState(Parcelable superState) { - super(superState); - } - - @SuppressWarnings("unused") - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; - } -} \ No newline at end of file +public class ColorPickerPreference + extends + Preference + implements + Preference.OnPreferenceClickListener, + ColorPickerDialog.OnColorChangedListener { + + View mView; + ColorPickerDialog mDialog; + private int mValue = Color.BLACK; + private float mDensity = 0; + private boolean mAlphaSliderEnabled = false; + private boolean mHexValueEnabled = false; + + public ColorPickerPreference(Context context) { + super(context); + init(context, null); + } + + public ColorPickerPreference(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs); + } + + public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(context, attrs); + } + + /**Method edited by + * @author Anna Berkovitch + * added functionality to accept hex string as defaultValue + * and to properly persist resources reference string, such as @color/someColor + * previously persisted 0*/ + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + int colorInt; + String mHexDefaultValue = a.getString(index); + if (mHexDefaultValue != null && mHexDefaultValue.startsWith("#")) { + colorInt = convertToColorInt(mHexDefaultValue); + return colorInt; + + } else { + return a.getColor(index, Color.BLACK); + } + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + onColorChanged(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue); + } + + private void init(Context context, AttributeSet attrs) { + mDensity = getContext().getResources().getDisplayMetrics().density; + setOnPreferenceClickListener(this); + if (attrs != null) { + mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false); + mHexValueEnabled = attrs.getAttributeBooleanValue(null, "hexValue", false); + } + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + mView = holder.itemView; + setPreviewColor(); + } + + private void setPreviewColor() { + if (mView == null) return; + ImageView iView = new ImageView(getContext()); + LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame)); + if (widgetFrameView == null) return; + widgetFrameView.setVisibility(View.VISIBLE); + widgetFrameView.setPadding( + widgetFrameView.getPaddingLeft(), + widgetFrameView.getPaddingTop(), + (int) (mDensity * 8), + widgetFrameView.getPaddingBottom() + ); + // remove already create preview image + int count = widgetFrameView.getChildCount(); + if (count > 0) { + widgetFrameView.removeViews(0, count); + } + widgetFrameView.addView(iView); + widgetFrameView.setMinimumWidth(0); + iView.setBackgroundDrawable(new AlphaPatternDrawable((int) (5 * mDensity))); + iView.setImageBitmap(getPreviewBitmap()); + } + + private Bitmap getPreviewBitmap() { + int d = (int) (mDensity * 31); //30dip + int color = mValue; + Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888); + int w = bm.getWidth(); + int h = bm.getHeight(); + int c = color; + for (int i = 0; i < w; i++) { + for (int j = i; j < h; j++) { + c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : color; + bm.setPixel(i, j, c); + if (i != j) { + bm.setPixel(j, i, c); + } + } + } + + return bm; + } + + @Override + public void onColorChanged(int color) { + if (isPersistent()) { + persistInt(color); + } + mValue = color; + setPreviewColor(); + try { + getOnPreferenceChangeListener().onPreferenceChange(this, color); + } catch (NullPointerException e) { + + } + } + + public boolean onPreferenceClick(Preference preference) { + showDialog(null); + return false; + } + + protected void showDialog(Bundle state) { + mDialog = new ColorPickerDialog(getContext(), mValue, getTitle().toString()); + mDialog.setOnColorChangedListener(this); + if (mAlphaSliderEnabled) { + mDialog.setAlphaSliderVisible(true); + } + if (mHexValueEnabled) { + mDialog.setHexValueEnabled(true); + } + if (state != null) { + mDialog.onRestoreInstanceState(state); + } + mDialog.show(); + } + + /** + * Toggle Alpha Slider visibility (by default it's disabled) + * + * @param enable + */ + public void setAlphaSliderEnabled(boolean enable) { + mAlphaSliderEnabled = enable; + } + + /** + * Toggle Hex Value visibility (by default it's disabled) + * + * @param enable + */ + public void setHexValueEnabled(boolean enable) { + mHexValueEnabled = enable; + } + + /** + * For custom purposes. Not used by ColorPickerPreferrence + * + * @param color + * @author Unknown + */ + public static String convertToARGB(int color) { + String alpha = Integer.toHexString(Color.alpha(color)); + String red = Integer.toHexString(Color.red(color)); + String green = Integer.toHexString(Color.green(color)); + String blue = Integer.toHexString(Color.blue(color)); + + if (alpha.length() == 1) { + alpha = "0" + alpha; + } + + if (red.length() == 1) { + red = "0" + red; + } + + if (green.length() == 1) { + green = "0" + green; + } + + if (blue.length() == 1) { + blue = "0" + blue; + } + + return "#" + alpha + red + green + blue; + } + + /** + * Method currently used by onGetDefaultValue method to + * convert hex string provided in android:defaultValue to color integer. + * + * @param color + * @return A string representing the hex value of color, + * without the alpha value + * @author Charles Rosaaen + */ + public static String convertToRGB(int color) { + String red = Integer.toHexString(Color.red(color)); + String green = Integer.toHexString(Color.green(color)); + String blue = Integer.toHexString(Color.blue(color)); + + if (red.length() == 1) { + red = "0" + red; + } + + if (green.length() == 1) { + green = "0" + green; + } + + if (blue.length() == 1) { + blue = "0" + blue; + } + + return "#" + red + green + blue; + } + + /** + * For custom purposes. Not used by ColorPickerPreferrence + * + * @param argb + * @throws NumberFormatException + * @author Unknown + */ + public static int convertToColorInt(String argb) throws IllegalArgumentException { + + if (!argb.startsWith("#")) { + argb = "#" + argb; + } + + return Color.parseColor(argb); + } + + @Override + protected Parcelable onSaveInstanceState() { + final Parcelable superState = super.onSaveInstanceState(); + if (mDialog == null || !mDialog.isShowing()) { + return superState; + } + + final SavedState myState = new SavedState(superState); + myState.dialogBundle = mDialog.onSaveInstanceState(); + return myState; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state == null || !(state instanceof SavedState)) { + // Didn't save state for us in onSaveInstanceState + super.onRestoreInstanceState(state); + return; + } + + SavedState myState = (SavedState) state; + super.onRestoreInstanceState(myState.getSuperState()); + showDialog(myState.dialogBundle); + } + + private static class SavedState extends BaseSavedState { + Bundle dialogBundle; + + public SavedState(Parcel source) { + super(source); + dialogBundle = source.readBundle(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeBundle(dialogBundle); + } + + public SavedState(Parcelable superState) { + super(superState); + } + + @SuppressWarnings("unused") + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } +} diff --git a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java index 48d18d61..f2824cfc 100644 --- a/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java +++ b/app/src/main/java/net/margaritov/preference/colorpicker/ColorPickerView.java @@ -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,895 +40,908 @@ 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 { - private final static int PANEL_SAT_VAL = 0; - private final static int PANEL_HUE = 1; - private final static int PANEL_ALPHA = 2; - - /** - * The width in pixels of the border - * surrounding all color panels. - */ - private final static float BORDER_WIDTH_PX = 1; - - /** - * The width in dp of the hue panel. - */ - private float HUE_PANEL_WIDTH = 30f; - /** - * The height in dp of the alpha panel - */ - private float ALPHA_PANEL_HEIGHT = 20f; - /** - * The distance in dp between the different - * color panels. - */ - private float PANEL_SPACING = 10f; - /** - * The radius in dp of the color palette tracker circle. - */ - private float PALETTE_CIRCLE_TRACKER_RADIUS = 5f; - /** - * The dp which the tracker of the hue or alpha panel - * will extend outside of its bounds. - */ - private float RECTANGLE_TRACKER_OFFSET = 2f; - - - private float mDensity = 1f; - - private OnColorChangedListener mListener; - - private Paint mSatValPaint; - private Paint mSatValTrackerPaint; - - private Paint mHuePaint; - private Paint mHueTrackerPaint; - - private Paint mAlphaPaint; - private Paint mAlphaTextPaint; - - private Paint mBorderPaint; - - private Shader mValShader; - private Shader mSatShader; - private Shader mHueShader; - private Shader mAlphaShader; - - private int mAlpha = 0xff; - private float mHue = 360f; - private float mSat = 0f; - private float mVal = 0f; - - private String mAlphaSliderText = ""; - private int mSliderTrackerColor = 0xff1c1c1c; - private int mBorderColor = 0xff6E6E6E; - private boolean mShowAlphaPanel = false; - - /* - * To remember which panel that has the "focus" when - * processing hardware button data. - */ - private int mLastTouchedPanel = PANEL_SAT_VAL; - - /** - * Offset from the edge we must have or else - * the finger tracker will get clipped when - * it is drawn outside of the view. - */ - private float mDrawingOffset; - - - /* - * Distance form the edges of the view - * of where we are allowed to draw. - */ - private RectF mDrawingRect; - - private RectF mSatValRect; - private RectF mHueRect; - private RectF mAlphaRect; - - private AlphaPatternDrawable mAlphaPattern; - - private Point mStartTouchPoint = null; + private final static int PANEL_SAT_VAL = 0; + private final static int PANEL_HUE = 1; + private final static int PANEL_ALPHA = 2; + + /** + * The width in pixels of the border + * surrounding all color panels. + */ + private final static float BORDER_WIDTH_PX = 1; + + /** + * The width in dp of the hue panel. + */ + private float HUE_PANEL_WIDTH = 30f; + /** + * The height in dp of the alpha panel + */ + private float ALPHA_PANEL_HEIGHT = 20f; + /** + * The distance in dp between the different + * color panels. + */ + private float PANEL_SPACING = 10f; + /** + * The radius in dp of the color palette tracker circle. + */ + private float PALETTE_CIRCLE_TRACKER_RADIUS = 5f; + /** + * The dp which the tracker of the hue or alpha panel + * will extend outside of its bounds. + */ + private float RECTANGLE_TRACKER_OFFSET = 2f; + + + private float mDensity = 1f; + + private OnColorChangedListener mListener; + + private Paint mSatValPaint; + private Paint mSatValTrackerPaint; + + private Paint mHuePaint; + private Paint mHueTrackerPaint; + + private Paint mAlphaPaint; + private Paint mAlphaTextPaint; + + private Paint mBorderPaint; + + private Shader mValShader; + private Shader mSatShader; + private Shader mHueShader; + private Shader mAlphaShader; + + private int mAlpha = 0xff; + private float mHue = 360f; + private float mSat = 0f; + private float mVal = 0f; + + private String mAlphaSliderText = ""; + private int mSliderTrackerColor = 0xff1c1c1c; + private int mBorderColor = 0xff6E6E6E; + private boolean mShowAlphaPanel = false; + + /* + * To remember which panel that has the "focus" when + * processing hardware button data. + */ + private int mLastTouchedPanel = PANEL_SAT_VAL; + + /** + * Offset from the edge we must have or else + * the finger tracker will get clipped when + * it is drawn outside of the view. + */ + private float mDrawingOffset; - public interface OnColorChangedListener { - public void onColorChanged(int color); - } - public ColorPickerView(Context context) { - this(context, null); - } + /* + * Distance form the edges of the view + * of where we are allowed to draw. + */ + private RectF mDrawingRect; - public ColorPickerView(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } + private RectF mSatValRect; + private RectF mHueRect; + private RectF mAlphaRect; - public ColorPickerView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } + private AlphaPatternDrawable mAlphaPattern; - private void init() { - mDensity = getContext().getResources().getDisplayMetrics().density; - PALETTE_CIRCLE_TRACKER_RADIUS *= mDensity; - RECTANGLE_TRACKER_OFFSET *= mDensity; - HUE_PANEL_WIDTH *= mDensity; - ALPHA_PANEL_HEIGHT *= mDensity; - PANEL_SPACING = PANEL_SPACING * mDensity; + private Point mStartTouchPoint = null; - mDrawingOffset = calculateRequiredOffset(); + public interface OnColorChangedListener { + public void onColorChanged(int color); + } - initPaintTools(); + public ColorPickerView(Context context) { + this(context, null); + } - //Needed for receiving trackball motion events. - setFocusable(true); - setFocusableInTouchMode(true); - } + public ColorPickerView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } - private void initPaintTools() { + public ColorPickerView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } - mSatValPaint = new Paint(); - mSatValTrackerPaint = new Paint(); - mHuePaint = new Paint(); - mHueTrackerPaint = new Paint(); - mAlphaPaint = new Paint(); - mAlphaTextPaint = new Paint(); - mBorderPaint = new Paint(); + private void init() { + mDensity = getContext().getResources().getDisplayMetrics().density; + PALETTE_CIRCLE_TRACKER_RADIUS *= mDensity; + RECTANGLE_TRACKER_OFFSET *= mDensity; + HUE_PANEL_WIDTH *= mDensity; + ALPHA_PANEL_HEIGHT *= mDensity; + PANEL_SPACING = PANEL_SPACING * mDensity; + mDrawingOffset = calculateRequiredOffset(); - mSatValTrackerPaint.setStyle(Style.STROKE); - mSatValTrackerPaint.setStrokeWidth(2f * mDensity); - mSatValTrackerPaint.setAntiAlias(true); + initPaintTools(); - mHueTrackerPaint.setColor(mSliderTrackerColor); - mHueTrackerPaint.setStyle(Style.STROKE); - mHueTrackerPaint.setStrokeWidth(2f * mDensity); - mHueTrackerPaint.setAntiAlias(true); + //Needed for receiving trackball motion events. + setFocusable(true); + setFocusableInTouchMode(true); + } - mAlphaTextPaint.setColor(0xff1c1c1c); - mAlphaTextPaint.setTextSize(14f * mDensity); - mAlphaTextPaint.setAntiAlias(true); - mAlphaTextPaint.setTextAlign(Align.CENTER); - mAlphaTextPaint.setFakeBoldText(true); + private void initPaintTools() { + mSatValPaint = new Paint(); + mSatValTrackerPaint = new Paint(); + mHuePaint = new Paint(); + mHueTrackerPaint = new Paint(); + mAlphaPaint = new Paint(); + mAlphaTextPaint = new Paint(); + mBorderPaint = new Paint(); - } - private float calculateRequiredOffset() { - float offset = Math.max(PALETTE_CIRCLE_TRACKER_RADIUS, RECTANGLE_TRACKER_OFFSET); - offset = Math.max(offset, BORDER_WIDTH_PX * mDensity); + mSatValTrackerPaint.setStyle(Style.STROKE); + mSatValTrackerPaint.setStrokeWidth(2f * mDensity); + mSatValTrackerPaint.setAntiAlias(true); - return offset * 1.5f; - } + mHueTrackerPaint.setColor(mSliderTrackerColor); + mHueTrackerPaint.setStyle(Style.STROKE); + mHueTrackerPaint.setStrokeWidth(2f * mDensity); + mHueTrackerPaint.setAntiAlias(true); - private int[] buildHueColorArray() { + mAlphaTextPaint.setColor(0xff1c1c1c); + mAlphaTextPaint.setTextSize(14f * mDensity); + mAlphaTextPaint.setAntiAlias(true); + mAlphaTextPaint.setTextAlign(Align.CENTER); + mAlphaTextPaint.setFakeBoldText(true); - int[] hue = new int[361]; - int count = 0; - for (int i = hue.length - 1; i >= 0; i--, count++) { - hue[count] = Color.HSVToColor(new float[]{i, 1f, 1f}); - } + } - return hue; - } + private float calculateRequiredOffset() { + float offset = Math.max(PALETTE_CIRCLE_TRACKER_RADIUS, RECTANGLE_TRACKER_OFFSET); + offset = Math.max(offset, BORDER_WIDTH_PX * mDensity); + return offset * 1.5f; + } - @Override - protected void onDraw(Canvas canvas) { + private int[] buildHueColorArray() { - if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) { - return; - } + int[] hue = new int[361]; - drawSatValPanel(canvas); - drawHuePanel(canvas); - drawAlphaPanel(canvas); + int count = 0; + for (int i = hue.length - 1; i >= 0; i--, count++) { + hue[count] = Color.HSVToColor(new float[]{i, 1f, 1f}); + } - } + return hue; + } - private void drawSatValPanel(Canvas canvas) { - final RectF rect = mSatValRect; + @Override + protected void onDraw(Canvas canvas) { - 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); - } + if (mDrawingRect.width() <= 0 || mDrawingRect.height() <= 0) return; - if (mValShader == null) { - mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000, - TileMode.CLAMP); - } + drawSatValPanel(canvas); + drawHuePanel(canvas); + drawAlphaPanel(canvas); - int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f}); + } - 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); + private void drawSatValPanel(Canvas canvas) { - canvas.drawRect(rect, mSatValPaint); + final RectF rect = mSatValRect; - Point p = satValToPoint(mSat, mVal); + 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); + } - mSatValTrackerPaint.setColor(0xff000000); - canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint); + if (mValShader == null) { + mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, + 0xffffffff, 0xff000000, TileMode.CLAMP); + } - mSatValTrackerPaint.setColor(0xffdddddd); - canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint); + int rgb = Color.HSVToColor(new float[]{mHue, 1f, 1f}); - } + 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); - private void drawHuePanel(Canvas canvas) { + canvas.drawRect(rect, mSatValPaint); - final RectF rect = mHueRect; + Point p = satValToPoint(mSat, mVal); - 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); - } + mSatValTrackerPaint.setColor(0xff000000); + canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint); - if (mHueShader == null) { - mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, - TileMode.CLAMP); - mHuePaint.setShader(mHueShader); - } + mSatValTrackerPaint.setColor(0xffdddddd); + canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint); - canvas.drawRect(rect, mHuePaint); + } - float rectHeight = 4 * mDensity / 2; + private void drawHuePanel(Canvas canvas) { - Point p = hueToPoint(mHue); + final RectF rect = mHueRect; - RectF r = new RectF(); - r.left = rect.left - RECTANGLE_TRACKER_OFFSET; - r.right = rect.right + RECTANGLE_TRACKER_OFFSET; - r.top = p.y - rectHeight; - r.bottom = p.y + rectHeight; + 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); + } + if (mHueShader == null) { + mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null, TileMode.CLAMP); + mHuePaint.setShader(mHueShader); + } - canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint); + canvas.drawRect(rect, mHuePaint); - } + float rectHeight = 4 * mDensity / 2; - private void drawAlphaPanel(Canvas canvas) { + Point p = hueToPoint(mHue); - if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) { - return; - } + RectF r = new RectF(); + r.left = rect.left - RECTANGLE_TRACKER_OFFSET; + r.right = rect.right + RECTANGLE_TRACKER_OFFSET; + r.top = p.y - rectHeight; + r.bottom = p.y + rectHeight; - 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.drawRoundRect(r, 2, 2, mHueTrackerPaint); + } - mAlphaPattern.draw(canvas); + private void drawAlphaPanel(Canvas canvas) { - float[] hsv = new float[]{mHue, mSat, mVal}; - int color = Color.HSVToColor(hsv); - int acolor = Color.HSVToColor(0, hsv); + if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) return; - mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP); + 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); + } - mAlphaPaint.setShader(mAlphaShader); - canvas.drawRect(rect, mAlphaPaint); + mAlphaPattern.draw(canvas); - if (mAlphaSliderText != null && mAlphaSliderText != "") { - canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint); - } + float[] hsv = new float[]{mHue, mSat, mVal}; + int color = Color.HSVToColor(hsv); + int acolor = Color.HSVToColor(0, hsv); - float rectWidth = 4 * mDensity / 2; + mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, + color, acolor, TileMode.CLAMP); - Point p = alphaToPoint(mAlpha); - RectF r = new RectF(); - r.left = p.x - rectWidth; - r.right = p.x + rectWidth; - r.top = rect.top - RECTANGLE_TRACKER_OFFSET; - r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET; + mAlphaPaint.setShader(mAlphaShader); - canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint); + canvas.drawRect(rect, mAlphaPaint); - } + if (mAlphaSliderText != null && mAlphaSliderText != "") { + canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint); + } + float rectWidth = 4 * mDensity / 2; - private Point hueToPoint(float hue) { + Point p = alphaToPoint(mAlpha); - final RectF rect = mHueRect; - final float height = rect.height(); + RectF r = new RectF(); + r.left = p.x - rectWidth; + r.right = p.x + rectWidth; + r.top = rect.top - RECTANGLE_TRACKER_OFFSET; + r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET; - Point p = new Point(); + canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint); - p.y = (int) (height - (hue * height / 360f) + rect.top); - p.x = (int) rect.left; + } - return p; - } - private Point satValToPoint(float sat, float val) { + private Point hueToPoint(float hue) { - final RectF rect = mSatValRect; - final float height = rect.height(); - final float width = rect.width(); + final RectF rect = mHueRect; + final float height = rect.height(); - Point p = new Point(); + Point p = new Point(); - p.x = (int) (sat * width + rect.left); - p.y = (int) ((1f - val) * height + rect.top); + p.y = (int) (height - (hue * height / 360f) + rect.top); + p.x = (int) rect.left; - return p; - } + return p; + } - private Point alphaToPoint(int alpha) { + private Point satValToPoint(float sat, float val) { - final RectF rect = mAlphaRect; - final float width = rect.width(); + final RectF rect = mSatValRect; + final float height = rect.height(); + final float width = rect.width(); - Point p = new Point(); + Point p = new Point(); - p.x = (int) (width - (alpha * width / 0xff) + rect.left); - p.y = (int) rect.top; + p.x = (int) (sat * width + rect.left); + p.y = (int) ((1f - val) * height + rect.top); - return p; + return p; + } - } + private Point alphaToPoint(int alpha) { - private float[] pointToSatVal(float x, float y) { + final RectF rect = mAlphaRect; + final float width = rect.width(); - final RectF rect = mSatValRect; - float[] result = new float[2]; + Point p = new Point(); - float width = rect.width(); - float height = rect.height(); + p.x = (int) (width - (alpha * width / 0xff) + rect.left); + p.y = (int) rect.top; - if (x < rect.left) { - x = 0f; - } else if (x > rect.right) { - x = width; - } else { - x = x - rect.left; - } + return p; - if (y < rect.top) { - y = 0f; - } else if (y > rect.bottom) { - y = height; - } else { - y = y - rect.top; - } + } + private float[] pointToSatVal(float x, float y) { - result[0] = 1.f / width * x; - result[1] = 1.f - (1.f / height * y); + final RectF rect = mSatValRect; + float[] result = new float[2]; - return result; - } + float width = rect.width(); + float height = rect.height(); - private float pointToHue(float y) { + if (x < rect.left) { + x = 0f; + } else if (x > rect.right) { + x = width; + } else { + x = x - rect.left; + } - final RectF rect = mHueRect; + if (y < rect.top) { + y = 0f; + } else if (y > rect.bottom) { + y = height; + } else { + y = y - rect.top; + } - float height = rect.height(); - if (y < rect.top) { - y = 0f; - } else if (y > rect.bottom) { - y = height; - } else { - y = y - rect.top; - } + result[0] = 1.f / width * x; + result[1] = 1.f - (1.f / height * y); - return 360f - (y * 360f / height); - } + return result; + } - private int pointToAlpha(int x) { + private float pointToHue(float y) { - final RectF rect = mAlphaRect; - final int width = (int) rect.width(); + final RectF rect = mHueRect; - if (x < rect.left) { - x = 0; - } else if (x > rect.right) { - x = width; - } else { - x = x - (int) rect.left; - } + float height = rect.height(); - return 0xff - (x * 0xff / width); + if (y < rect.top) { + y = 0f; + } else if (y > rect.bottom) { + y = height; + } else { + y = y - rect.top; + } - } + return 360f - (y * 360f / height); + } + private int pointToAlpha(int x) { - @Override - public boolean onTrackballEvent(MotionEvent event) { + final RectF rect = mAlphaRect; + final int width = (int) rect.width(); - float x = event.getX(); - float y = event.getY(); + if (x < rect.left) { + x = 0; + } else if (x > rect.right) { + x = width; + } else { + x = x - (int) rect.left; + } - boolean update = false; + return 0xff - (x * 0xff / width); + } - if (event.getAction() == MotionEvent.ACTION_MOVE) { - switch (mLastTouchedPanel) { + @Override + public boolean onTrackballEvent(MotionEvent event) { - case PANEL_SAT_VAL: + float x = event.getX(); + float y = event.getY(); - float sat, val; + boolean update = false; - sat = mSat + x / 50f; - val = mVal - y / 50f; - if (sat < 0f) { - sat = 0f; - } else if (sat > 1f) { - sat = 1f; - } + if (event.getAction() == MotionEvent.ACTION_MOVE) { - if (val < 0f) { - val = 0f; - } else if (val > 1f) { - val = 1f; - } + switch (mLastTouchedPanel) { - mSat = sat; - mVal = val; + case PANEL_SAT_VAL: - update = true; + float sat, val; - break; + sat = mSat + x / 50f; + val = mVal - y / 50f; - case PANEL_HUE: + if (sat < 0f) { + sat = 0f; + } else if (sat > 1f) { + sat = 1f; + } - float hue = mHue - y * 10f; + if (val < 0f) { + val = 0f; + } else if (val > 1f) { + val = 1f; + } - if (hue < 0f) { - hue = 0f; - } else if (hue > 360f) { - hue = 360f; - } + mSat = sat; + mVal = val; - mHue = hue; + update = true; - update = true; + break; - break; + case PANEL_HUE: - case PANEL_ALPHA: + float hue = mHue - y * 10f; - if (!mShowAlphaPanel || mAlphaRect == null) { - update = false; - } else { + if (hue < 0f) { + hue = 0f; + } else if (hue > 360f) { + hue = 360f; + } - int alpha = (int) (mAlpha - x * 10); + mHue = hue; - if (alpha < 0) { - alpha = 0; - } else if (alpha > 0xff) { - alpha = 0xff; - } + update = true; - mAlpha = alpha; + break; + case PANEL_ALPHA: - update = true; - } + if (!mShowAlphaPanel || mAlphaRect == null) { + update = false; + } else { - break; - } + int alpha = (int) (mAlpha - x * 10); + if (alpha < 0) { + alpha = 0; + } else if (alpha > 0xff) { + alpha = 0xff; + } - } + mAlpha = alpha; - if (update) { + update = true; + } - if (mListener != null) { - mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); - } + break; + } - invalidate(); - return true; - } + } - return super.onTrackballEvent(event); - } - @Override - public boolean onTouchEvent(MotionEvent event) { + if (update) { - boolean update = false; + if (mListener != null) { + mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); + } - switch (event.getAction()) { + invalidate(); + return true; + } - case MotionEvent.ACTION_DOWN: - mStartTouchPoint = new Point((int) event.getX(), (int) event.getY()); + return super.onTrackballEvent(event); + } - update = moveTrackersIfNeeded(event); + @Override + public boolean onTouchEvent(MotionEvent event) { - break; + boolean update = false; - case MotionEvent.ACTION_MOVE: + switch (event.getAction()) { - update = moveTrackersIfNeeded(event); + case MotionEvent.ACTION_DOWN: - break; + mStartTouchPoint = new Point((int) event.getX(), (int) event.getY()); - case MotionEvent.ACTION_UP: + update = moveTrackersIfNeeded(event); - mStartTouchPoint = null; + break; - update = moveTrackersIfNeeded(event); + case MotionEvent.ACTION_MOVE: - break; + update = moveTrackersIfNeeded(event); - } + break; - if (update) { + case MotionEvent.ACTION_UP: - if (mListener != null) { - mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); - } + mStartTouchPoint = null; - invalidate(); - return true; - } + update = moveTrackersIfNeeded(event); + break; - return super.onTouchEvent(event); - } + } - private boolean moveTrackersIfNeeded(MotionEvent event) { + if (update) { - if (mStartTouchPoint == null) { - return false; - } + if (mListener != null) { + mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); + } - boolean update = false; + invalidate(); + return true; + } - int startX = mStartTouchPoint.x; - int startY = mStartTouchPoint.y; + return super.onTouchEvent(event); + } - if (mHueRect.contains(startX, startY)) { - mLastTouchedPanel = PANEL_HUE; + private boolean moveTrackersIfNeeded(MotionEvent event) { - mHue = pointToHue(event.getY()); + if (mStartTouchPoint == null) return false; - update = true; - } else if (mSatValRect.contains(startX, startY)) { + boolean update = false; - mLastTouchedPanel = PANEL_SAT_VAL; + int startX = mStartTouchPoint.x; + int startY = mStartTouchPoint.y; - float[] result = pointToSatVal(event.getX(), event.getY()); - mSat = result[0]; - mVal = result[1]; + if (mHueRect.contains(startX, startY)) { + mLastTouchedPanel = PANEL_HUE; - update = true; - } else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) { + mHue = pointToHue(event.getY()); - mLastTouchedPanel = PANEL_ALPHA; + update = true; + } else if (mSatValRect.contains(startX, startY)) { - mAlpha = pointToAlpha((int) event.getX()); + mLastTouchedPanel = PANEL_SAT_VAL; - update = true; - } + float[] result = pointToSatVal(event.getX(), event.getY()); + mSat = result[0]; + mVal = result[1]; - return update; - } + update = true; + } else if (mAlphaRect != null && mAlphaRect.contains(startX, startY)) { - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + mLastTouchedPanel = PANEL_ALPHA; - int width = 0; - int height = 0; + mAlpha = pointToAlpha((int) event.getX()); - int widthMode = MeasureSpec.getMode(widthMeasureSpec); - int heightMode = MeasureSpec.getMode(heightMeasureSpec); + update = true; + } - int widthAllowed = MeasureSpec.getSize(widthMeasureSpec); - int heightAllowed = MeasureSpec.getSize(heightMeasureSpec); - widthAllowed = chooseWidth(widthMode, widthAllowed); - heightAllowed = chooseHeight(heightMode, heightAllowed); + return update; + } - if (!mShowAlphaPanel) { + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - height = (int) (widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH); + int width = 0; + int height = 0; - //If calculated height (based on the width) is more than the allowed height. - if (height > heightAllowed || getTag().equals("landscape")) { - height = heightAllowed; - width = (int) (height + PANEL_SPACING + HUE_PANEL_WIDTH); - } else { - width = widthAllowed; - } - } else { + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); - width = (int) (heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH); + int widthAllowed = MeasureSpec.getSize(widthMeasureSpec); + int heightAllowed = MeasureSpec.getSize(heightMeasureSpec); - if (width > widthAllowed) { - width = widthAllowed; - height = (int) (widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT); - } else { - height = heightAllowed; - } + widthAllowed = chooseWidth(widthMode, widthAllowed); + heightAllowed = chooseHeight(heightMode, heightAllowed); - } + if (!mShowAlphaPanel) { - setMeasuredDimension(width, height); - } + height = (int) (widthAllowed - PANEL_SPACING - HUE_PANEL_WIDTH); - private int chooseWidth(int mode, int size) { - if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { - return size; - } else { // (mode == MeasureSpec.UNSPECIFIED) - return getPrefferedWidth(); - } - } + //If calculated height (based on the width) is more than the allowed height. + if (height > heightAllowed || getTag().equals("landscape")) { + height = heightAllowed; + width = (int) (height + PANEL_SPACING + HUE_PANEL_WIDTH); + } else { + width = widthAllowed; + } + } else { - private int chooseHeight(int mode, int size) { - if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { - return size; - } else { // (mode == MeasureSpec.UNSPECIFIED) - return getPrefferedHeight(); - } - } + width = (int) (heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH); - private int getPrefferedWidth() { + if (width > widthAllowed) { + width = widthAllowed; + height = (int) (widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT); + } else { + height = heightAllowed; + } - int width = getPrefferedHeight(); + } - if (mShowAlphaPanel) { - width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT); - } + setMeasuredDimension(width, height); + } + private int chooseWidth(int mode, int size) { + if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { + return size; + } else { // (mode == MeasureSpec.UNSPECIFIED) + return getPrefferedWidth(); + } + } - return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING); + private int chooseHeight(int mode, int size) { + if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) { + return size; + } else { // (mode == MeasureSpec.UNSPECIFIED) + return getPrefferedHeight(); + } + } - } + private int getPrefferedWidth() { - private int getPrefferedHeight() { + int width = getPrefferedHeight(); - int height = (int) (200 * mDensity); + if (mShowAlphaPanel) { + width -= (PANEL_SPACING + ALPHA_PANEL_HEIGHT); + } - if (mShowAlphaPanel) { - height += PANEL_SPACING + ALPHA_PANEL_HEIGHT; - } - return height; - } + return (int) (width + HUE_PANEL_WIDTH + PANEL_SPACING); + } - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); + private int getPrefferedHeight() { - mDrawingRect = new RectF(); - mDrawingRect.left = mDrawingOffset + getPaddingLeft(); - mDrawingRect.right = w - mDrawingOffset - getPaddingRight(); - mDrawingRect.top = mDrawingOffset + getPaddingTop(); - mDrawingRect.bottom = h - mDrawingOffset - getPaddingBottom(); + int height = (int) (200 * mDensity); - setUpSatValRect(); - setUpHueRect(); - setUpAlphaRect(); - } + if (mShowAlphaPanel) { + height += PANEL_SPACING + ALPHA_PANEL_HEIGHT; + } - private void setUpSatValRect() { + return height; + } - final RectF dRect = mDrawingRect; - float panelSide = dRect.height() - BORDER_WIDTH_PX * 2; - if (mShowAlphaPanel) { - panelSide -= PANEL_SPACING + ALPHA_PANEL_HEIGHT; - } + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); - float left = dRect.left + BORDER_WIDTH_PX; - float top = dRect.top + BORDER_WIDTH_PX; - float bottom = top + panelSide; - float right = left + panelSide; + mDrawingRect = new RectF(); + mDrawingRect.left = mDrawingOffset + getPaddingLeft(); + mDrawingRect.right = w - mDrawingOffset - getPaddingRight(); + mDrawingRect.top = mDrawingOffset + getPaddingTop(); + mDrawingRect.bottom = h - mDrawingOffset - getPaddingBottom(); - mSatValRect = new RectF(left, top, right, bottom); - } + setUpSatValRect(); + setUpHueRect(); + setUpAlphaRect(); + } - private void setUpHueRect() { - final RectF dRect = mDrawingRect; + private void setUpSatValRect() { - float left = dRect.right - HUE_PANEL_WIDTH + BORDER_WIDTH_PX; - float top = dRect.top + BORDER_WIDTH_PX; - float bottom = dRect.bottom - BORDER_WIDTH_PX - (mShowAlphaPanel ? (PANEL_SPACING + ALPHA_PANEL_HEIGHT) : 0); - float right = dRect.right - BORDER_WIDTH_PX; - - mHueRect = new RectF(left, top, right, bottom); - } - - private void setUpAlphaRect() { - - if (!mShowAlphaPanel) { - return; - } - - final RectF dRect = mDrawingRect; - - float left = dRect.left + BORDER_WIDTH_PX; - float top = dRect.bottom - ALPHA_PANEL_HEIGHT + BORDER_WIDTH_PX; - float bottom = dRect.bottom - BORDER_WIDTH_PX; - float right = dRect.right - BORDER_WIDTH_PX; - - 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)); - - } - - - /** - * Set a OnColorChangedListener to get notified when the color - * selected by the user has changed. - * @param listener - */ - public void setOnColorChangedListener(OnColorChangedListener listener) { - mListener = listener; - } + final RectF dRect = mDrawingRect; + float panelSide = dRect.height() - BORDER_WIDTH_PX * 2; + + if (mShowAlphaPanel) { + panelSide -= PANEL_SPACING + ALPHA_PANEL_HEIGHT; + } + + float left = dRect.left + BORDER_WIDTH_PX; + float top = dRect.top + BORDER_WIDTH_PX; + float bottom = top + panelSide; + float right = left + panelSide; - /** - * Set the color of the border surrounding all panels. - * @param color - */ - public void setBorderColor(int color) { - mBorderColor = color; - invalidate(); - } + mSatValRect = new RectF(left, top, right, bottom); + } - /** - * Get the color of the border surrounding all panels. - */ - public int getBorderColor() { - return mBorderColor; - } - - /** - * Get the current color this view is showing. - * @return the current color. - */ - public int getColor() { - return Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}); - } - - /** - * Set the color the view should show. - * @param color The color that should be selected. - */ - public void setColor(int color) { - setColor(color, false); - } - - /** - * 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. - */ - 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); - - mAlpha = alpha; - mHue = hsv[0]; - mSat = hsv[1]; - mVal = hsv[2]; - - if (callback && mListener != null) { - mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); - } - - invalidate(); - } - - /** - * Get the drawing offset of the color picker view. - * The drawing offset is the distance from the side of - * 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() { - return mDrawingOffset; - } - - /** - * 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) { - - if (mShowAlphaPanel != visible) { - mShowAlphaPanel = visible; + private void setUpHueRect() { + final RectF dRect = mDrawingRect; + + float left = dRect.right - HUE_PANEL_WIDTH + BORDER_WIDTH_PX; + float top = dRect.top + BORDER_WIDTH_PX; + float bottom = dRect.bottom - BORDER_WIDTH_PX - (mShowAlphaPanel ? (PANEL_SPACING + ALPHA_PANEL_HEIGHT) : 0); + float right = dRect.right - BORDER_WIDTH_PX; + + mHueRect = new RectF(left, top, right, bottom); + } + + private void setUpAlphaRect() { + + if (!mShowAlphaPanel) return; + + final RectF dRect = mDrawingRect; + + float left = dRect.left + BORDER_WIDTH_PX; + float top = dRect.bottom - ALPHA_PANEL_HEIGHT + BORDER_WIDTH_PX; + float bottom = dRect.bottom - BORDER_WIDTH_PX; + float right = dRect.right - BORDER_WIDTH_PX; + + 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) + ); + + } + + + /** + * Set a OnColorChangedListener to get notified when the color + * selected by the user has changed. + * + * @param listener + */ + public void setOnColorChangedListener(OnColorChangedListener listener) { + mListener = listener; + } + + /** + * Set the color of the border surrounding all panels. + * + * @param color + */ + public void setBorderColor(int color) { + mBorderColor = color; + invalidate(); + } + + /** + * Get the color of the border surrounding all panels. + */ + public int getBorderColor() { + return mBorderColor; + } + + /** + * Get the current color this view is showing. + * + * @return the current color. + */ + public int getColor() { + return Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}); + } + + /** + * Set the color the view should show. + * + * @param color The color that should be selected. + */ + public void setColor(int color) { + setColor(color, false); + } + + /** + * 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. + */ + public void setColor(int color, boolean callback) { + + int alpha = Color.alpha(color); + + float[] hsv = new float[3]; + + Color.colorToHSV(color, hsv); + + mAlpha = alpha; + mHue = hsv[0]; + mSat = hsv[1]; + mVal = hsv[2]; + + if (callback && mListener != null) { + mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal})); + } + + invalidate(); + } + + /** + * Get the drawing offset of the color picker view. + * The drawing offset is the distance from the side of + * 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() { + return mDrawingOffset; + } + + /** + * 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) { + + if (mShowAlphaPanel != visible) { + mShowAlphaPanel = visible; /* - * Reset all shader to force a recreation. + * Reset all shader to force a recreation. * Otherwise they will not look right after * the size of the view has changed. */ - mValShader = null; - mSatShader = null; - mHueShader = null; - mAlphaShader = null; - ; - - requestLayout(); - } - - } - - public void setSliderTrackerColor(int color) { - mSliderTrackerColor = color; - - mHueTrackerPaint.setColor(mSliderTrackerColor); - - invalidate(); - } - - public int getSliderTrackerColor() { - return mSliderTrackerColor; - } - - /** - * 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) { - String text = getContext().getString(res); - setAlphaSliderText(text); - } - - /** - * 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) { - mAlphaSliderText = text; - invalidate(); - } - - /** - * Get the current value of the text - * that will be shown in the alpha - * slider. - * @return - */ - public String getAlphaSliderText() { - return mAlphaSliderText; - } -} \ No newline at end of file + mValShader = null; + mSatShader = null; + mHueShader = null; + mAlphaShader = null; + + requestLayout(); + } + + } + + public boolean getAlphaSliderVisible() { + return mShowAlphaPanel; + } + + public void setSliderTrackerColor(int color) { + mSliderTrackerColor = color; + + mHueTrackerPaint.setColor(mSliderTrackerColor); + + invalidate(); + } + + public int getSliderTrackerColor() { + return mSliderTrackerColor; + } + + /** + * 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) { + String text = getContext().getString(res); + setAlphaSliderText(text); + } + + /** + * 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) { + mAlphaSliderText = text; + invalidate(); + } + + /** + * Get the current value of the text + * that will be shown in the alpha + * slider. + * + * @return + */ + public String getAlphaSliderText() { + return mAlphaSliderText; + } +} diff --git a/app/src/main/res/layout-land/dialog_color_picker.xml b/app/src/main/res/layout-land/dialog_color_picker.xml index cf567016..f7a2264d 100644 --- a/app/src/main/res/layout-land/dialog_color_picker.xml +++ b/app/src/main/res/layout-land/dialog_color_picker.xml @@ -1,77 +1,82 @@ - - - - - - - - - + android:paddingRight="5dp"> + + + + + + + + + + + + + + + - - - - - - - - - \ No newline at end of file + diff --git a/app/src/main/res/layout/dialog_color_picker.xml b/app/src/main/res/layout/dialog_color_picker.xml index 2ddbca41..a5fbaaf2 100644 --- a/app/src/main/res/layout/dialog_color_picker.xml +++ b/app/src/main/res/layout/dialog_color_picker.xml @@ -1,77 +1,88 @@ - - - - - - - - - + android:paddingRight="5dp"> + + + + + + + + + + + + + + + + + + - - - - - - - - - \ No newline at end of file +