Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadFlask authored Jan 23, 2019
2 parents e309fea + 69c1ade commit 63feeba
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Xfermode;
import android.graphics.drawable.ColorDrawable;

import com.flask.colorpicker.builder.PaintBuilder;

public class CircleColorDrawable extends ColorDrawable {
public class ColorCircleDrawable extends ColorDrawable {
private float strokeWidth;
private Paint strokePaint = PaintBuilder.newPaint().style(Paint.Style.STROKE).stroke(strokeWidth).color(0xffffffff).build();
private Paint strokePaint = PaintBuilder.newPaint().style(Paint.Style.STROKE).stroke(strokeWidth).color(0xff9e9e9e).build();
private Paint fillPaint = PaintBuilder.newPaint().style(Paint.Style.FILL).color(0).build();
private Paint fillBackPaint = PaintBuilder.newPaint().shader(PaintBuilder.createAlphaPatternShader(16)).build();
private Paint fillBackPaint = PaintBuilder.newPaint().shader(PaintBuilder.createAlphaPatternShader(26)).build();

public CircleColorDrawable() {
super();
}

public CircleColorDrawable(int color) {
public ColorCircleDrawable(int color) {
super(color);
}

Expand All @@ -28,12 +22,12 @@ public void draw(Canvas canvas) {

int width = canvas.getWidth();
float radius = width / 2f;
strokeWidth = radius / 12f;
strokeWidth = radius / 8f;

this.strokePaint.setStrokeWidth(strokeWidth);
this.fillPaint.setColor(getColor());
canvas.drawCircle(radius, radius, radius - strokeWidth * 1.5f, fillBackPaint);
canvas.drawCircle(radius, radius, radius - strokeWidth * 1.5f, fillPaint);
canvas.drawCircle(radius, radius, radius - strokeWidth, fillBackPaint);
canvas.drawCircle(radius, radius, radius - strokeWidth, fillPaint);
canvas.drawCircle(radius, radius, radius - strokeWidth, strokePaint);
}

Expand All @@ -42,4 +36,4 @@ public void setColor(int color) {
super.setColor(color);
invalidateSelf();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.preference.Preference;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

Expand All @@ -21,19 +18,20 @@ public class ColorPickerPreference extends Preference {

protected boolean alphaSlider;
protected boolean lightSlider;
protected boolean border;

protected int selectedColor = 0;

protected ColorPickerView.WHEEL_TYPE wheelType;
protected int density;

private boolean pickerColorEdit;
private String pickerTitle;
private String pickerButtonCancel;
private String pickerButtonOk;

protected ImageView colorIndicator;


public ColorPickerPreference(Context context) {
super(context);
}
Expand All @@ -48,19 +46,20 @@ public ColorPickerPreference(Context context, AttributeSet attrs, int defStyleAt
initWith(context, attrs);
}


private void initWith(Context context, AttributeSet attrs) {
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorPickerPreference);

try {
alphaSlider = typedArray.getBoolean(R.styleable.ColorPickerPreference_alphaSlider, false);
lightSlider = typedArray.getBoolean(R.styleable.ColorPickerPreference_lightnessSlider, false);
border = typedArray.getBoolean(R.styleable.ColorPickerPreference_border, true);

density = typedArray.getInt(R.styleable.ColorPickerPreference_density, 10);
density = typedArray.getInt(R.styleable.ColorPickerPreference_density, 8);
wheelType = ColorPickerView.WHEEL_TYPE.indexOf(typedArray.getInt(R.styleable.ColorPickerPreference_wheelType, 0));

selectedColor = typedArray.getInt(R.styleable.ColorPickerPreference_initialColor, 0xffffffff);

pickerColorEdit = typedArray.getBoolean(R.styleable.ColorPickerPreference_pickerColorEdit, true);
pickerTitle = typedArray.getString(R.styleable.ColorPickerPreference_pickerTitle);
if (pickerTitle==null)
pickerTitle = "Choose color";
Expand All @@ -85,30 +84,19 @@ private void initWith(Context context, AttributeSet attrs) {
protected void onBindView(@NonNull View view) {
super.onBindView(view);

Resources res = view.getContext().getResources();
GradientDrawable colorChoiceDrawable = null;
int tmpColor = isEnabled()
? selectedColor
: darken(selectedColor, .5f);

colorIndicator = (ImageView) view.findViewById(R.id.color_indicator);

ColorCircleDrawable colorChoiceDrawable = null;
Drawable currentDrawable = colorIndicator.getDrawable();
if (currentDrawable!=null && currentDrawable instanceof GradientDrawable)
colorChoiceDrawable = (GradientDrawable) currentDrawable;
if (currentDrawable != null && currentDrawable instanceof ColorCircleDrawable)
colorChoiceDrawable = (ColorCircleDrawable) currentDrawable;

if (colorChoiceDrawable==null) {
colorChoiceDrawable = new GradientDrawable();
colorChoiceDrawable.setShape(GradientDrawable.OVAL);
}

int tmpColor = isEnabled()
? selectedColor
: darken(selectedColor, .5f);

colorChoiceDrawable.setColor(tmpColor);
colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
1,
res.getDisplayMetrics()
), darken(tmpColor, .8f));
if (colorChoiceDrawable == null)
colorChoiceDrawable = new ColorCircleDrawable(tmpColor);

colorIndicator.setImageDrawable(colorChoiceDrawable);
}
Expand All @@ -126,15 +114,16 @@ protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setValue(restoreValue ? getPersistedInt(0) : (Integer) defaultValue);
}


@Override
protected void onClick() {
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
.with(getContext())
.setTitle(pickerTitle)
.initialColor(selectedColor)
.showBorder(border)
.wheelType(wheelType)
.density(density)
.showColorEdit(pickerColorEdit)
.setPositiveButton(pickerButtonOk, new ColorPickerClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedColorFromPicker, Integer[] allColors) {
Expand All @@ -147,7 +136,6 @@ public void onClick(DialogInterface dialog, int selectedColorFromPicker, Integer
else if (!alphaSlider) builder.lightnessSliderOnly();
else if (!lightSlider) builder.alphaSliderOnly();


builder
.build()
.show();
Expand All @@ -164,4 +152,4 @@ public static int darken(int color, float factor) {
Math.max((int)(g * factor), 0),
Math.max((int)(b * factor), 0));
}
}
}
61 changes: 40 additions & 21 deletions library/src/main/java/com/flask/colorpicker/ColorPickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import java.util.ArrayList;

public class ColorPickerView extends View {
private static final float STROKE_RATIO = 2f;
private static final float STROKE_RATIO = 1.5f;

private Bitmap colorWheel;
private Canvas colorWheelCanvas;
private int density = 10;
private Bitmap currentColor;
private Canvas currentColorCanvas;
private boolean showBorder;
private int density = 8;

private float lightness = 1;
private float alpha = 1;
Expand All @@ -40,10 +43,9 @@ public class ColorPickerView extends View {
private Integer initialColors[] = new Integer[]{null, null, null, null, null};
private int colorSelection = 0;
private Integer initialColor;
private Integer pickerTextColor;
private Integer pickerColorEditTextColor;
private Paint colorWheelFill = PaintBuilder.newPaint().color(0).build();
private Paint selectorStroke1 = PaintBuilder.newPaint().color(0xffffffff).build();
private Paint selectorStroke2 = PaintBuilder.newPaint().color(0xff000000).build();
private Paint selectorStroke = PaintBuilder.newPaint().color(0).build();
private Paint alphaPatternPaint = PaintBuilder.newPaint().build();
private ColorCircle currentColorCircle;

Expand All @@ -66,7 +68,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
// set the color without changing the edit text preventing stack overflow
setColor(color, false);
} catch (Exception e) {

e.printStackTrace();
}
}

Expand Down Expand Up @@ -107,7 +109,7 @@ private void initWith(Context context, AttributeSet attrs) {
density = typedArray.getInt(R.styleable.ColorPickerPreference_density, 10);
initialColor = typedArray.getInt(R.styleable.ColorPickerPreference_initialColor, 0xffffffff);

pickerTextColor = typedArray.getInt(R.styleable.ColorPickerPreference_pickerColorEditTextColor, 0xffffffff);
pickerColorEditTextColor = typedArray.getInt(R.styleable.ColorPickerPreference_pickerColorEditTextColor, 0xffffffff);

WHEEL_TYPE wheelType = WHEEL_TYPE.indexOf(typedArray.getInt(R.styleable.ColorPickerPreference_wheelType, 0));
ColorWheelRenderer renderer = ColorWheelRendererBuilder.getRenderer(wheelType);
Expand Down Expand Up @@ -159,14 +161,19 @@ private void updateColorWheel() {
if (colorWheel == null) {
colorWheel = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
colorWheelCanvas = new Canvas(colorWheel);
alphaPatternPaint.setShader(PaintBuilder.createAlphaPatternShader(8));
alphaPatternPaint.setShader(PaintBuilder.createAlphaPatternShader(26));
}
if (currentColor == null) {
currentColor = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
currentColorCanvas = new Canvas(currentColor);
}
drawColorWheel();
invalidate();
}

private void drawColorWheel() {
colorWheelCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
currentColorCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

if (renderer == null) return;

Expand Down Expand Up @@ -227,6 +234,7 @@ public boolean onTouchEvent(MotionEvent event) {

initialColor = selectedColor;
setColorToSliders(selectedColor);
updateColorWheel();
invalidate();
break;
}
Expand Down Expand Up @@ -267,18 +275,25 @@ protected void callOnColorChangedListeners(int oldColor, int newColor) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(backgroundColor);
if (colorWheel != null)
canvas.drawBitmap(colorWheel, 0, 0, null);
if (currentColorCircle != null) {
float maxRadius = canvas.getWidth() / 2f - STROKE_RATIO * (1f + ColorWheelRenderer.GAP_PERCENTAGE);
float size = maxRadius / density / 2;

float maxRadius = canvas.getWidth() / (1f + ColorWheelRenderer.GAP_PERCENTAGE);
float size = maxRadius / density / 2;
if (colorWheel != null && currentColorCircle != null) {
colorWheelFill.setColor(Color.HSVToColor(currentColorCircle.getHsvWithLightness(this.lightness)));
colorWheelFill.setAlpha((int) (alpha * 0xff));
canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size * STROKE_RATIO, selectorStroke1);
canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size * (1 + (STROKE_RATIO - 1) / 2), selectorStroke2);

canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size, alphaPatternPaint);
canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size, colorWheelFill);
// a separate canvas is used to erase an issue with the alpha pattern around the edges
// draw circle slightly larger than it needs to be, then erase edges to proper dimensions
currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + 4, alphaPatternPaint);
currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + 4, colorWheelFill);

selectorStroke = PaintBuilder.newPaint().color(0xffffffff).style(Paint.Style.STROKE).stroke(size * (STROKE_RATIO - 1)).xPerMode(PorterDuff.Mode.CLEAR).build();

if (showBorder) colorWheelCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + (selectorStroke.getStrokeWidth() / 2f), selectorStroke);
canvas.drawBitmap(colorWheel, 0, 0, null);

currentColorCanvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size + (selectorStroke.getStrokeWidth() / 2f), selectorStroke);
canvas.drawBitmap(currentColor, 0, 0, null);
}
}

Expand Down Expand Up @@ -422,12 +437,12 @@ public void setColorEdit(EditText colorEdit) {
if (this.colorEdit != null) {
this.colorEdit.setVisibility(View.VISIBLE);
this.colorEdit.addTextChangedListener(colorTextChange);
setColorEditTextColor(pickerTextColor);
setColorEditTextColor(pickerColorEditTextColor);
}
}

public void setColorEditTextColor(int argb) {
this.pickerTextColor = argb;
this.pickerColorEditTextColor = argb;
if (colorEdit != null)
colorEdit.setTextColor(argb);
}
Expand Down Expand Up @@ -488,6 +503,10 @@ public void setSelectedColor(int previewNumber) {
setColor(color, true);
}

public void setShowBorder(boolean showBorder) {
this.showBorder = showBorder;
}

private void setHighlightedColor(int previewNumber) {
int children = colorPreview.getChildCount();
if (children == 0 || colorPreview.getVisibility() != View.VISIBLE)
Expand Down Expand Up @@ -519,7 +538,7 @@ private void setColorPreviewColor(int newColor) {
return;
LinearLayout childLayout = (LinearLayout) childView;
ImageView childImage = (ImageView) childLayout.findViewById(R.id.image_preview);
childImage.setImageDrawable(new CircleColorDrawable(newColor));
childImage.setImageDrawable(new ColorCircleDrawable(newColor));
}

private void setColorText(int argb) {
Expand Down Expand Up @@ -548,4 +567,4 @@ public static WHEEL_TYPE indexOf(int index) {
return FLOWER;
}
}
}
}
Loading

0 comments on commit 63feeba

Please sign in to comment.