Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import android.view.MotionEvent;
import android.view.View;

import com.larswerkman.holocolorpicker.R;

/**
* Displays a holo-themed color picker.
*
Expand All @@ -52,7 +50,7 @@ public class ColorPicker extends View {
private static final String STATE_SHOW_OLD_COLOR = "showColor";

/**
* Colors to construct the color wheel using {@link android.graphics.SweepGradient}.
* Colors to construct the color wheel using {@link SweepGradient}.
*/
private static final int[] COLORS = new int[] { 0xFFFF0000, 0xFFFF00FF,
0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
Expand Down Expand Up @@ -118,8 +116,8 @@ public class ColorPicker extends View {
/**
* {@code true} if the user clicked on the pointer to start the move mode. <br>
* {@code false} once the user stops touching the screen.
*
* @see #onTouchEvent(android.view.MotionEvent)
*
* @see #onTouchEvent(MotionEvent)
*/
private boolean mUserIsMovingPointer = false;

Expand All @@ -132,7 +130,7 @@ public class ColorPicker extends View {
* The ARGB value of the center with the old selected color.
*/
private int mCenterOldColor;

/**
* Whether to show the old color in the center or not.
*/
Expand All @@ -145,19 +143,19 @@ public class ColorPicker extends View {

/**
* Number of pixels the origin of this view is moved in X- and Y-direction.
*
*
* <p>
* We use the center of this (quadratic) View as origin of our internal
* coordinate system. Android uses the upper left corner as origin for the
* View-specific coordinate system. So this is the value we use to translate
* from one coordinate system to the other.
* </p>
*
*
* <p>
* Note: (Re)calculated in {@link #onMeasure(int, int)}.
* </p>
*
* @see #onDraw(android.graphics.Canvas)
*
* @see #onDraw(Canvas)
*/
private float mTranslationOffset;

Expand Down Expand Up @@ -235,7 +233,7 @@ public class ColorPicker extends View {
/**
* {@code onColorSelectedListener} instance of the onColorSelectedListener
*/
private OnColorSelectedListener onColorSelectedListener;
protected OnColorSelectedListener onColorSelectedListener;

public ColorPicker(Context context) {
super(context);
Expand Down Expand Up @@ -316,7 +314,7 @@ public OnColorSelectedListener getOnColorSelectedListener() {
/**
* Color of the latest entry of the onColorSelectedListener.
*/
private int oldSelectedListenerColor;
protected int oldSelectedListenerColor;

private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(attrs,
Expand Down Expand Up @@ -659,9 +657,9 @@ else if (Math.sqrt(x*x + y*y) <= mColorWheelRadius + mColorPointerHaloRadius
mCenterHaloPaint.setAlpha(0x00);

if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) {
onColorSelectedListener.onColorSelected(mCenterNewColor);
oldSelectedListenerColor = mCenterNewColor;
}
onColorSelectedListener.onColorSelected(mCenterNewColor);
oldSelectedListenerColor = mCenterNewColor;
}

invalidate();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import android.view.MotionEvent;
import android.view.View;

import com.larswerkman.holocolorpicker.R;

public class OpacityBar extends View {

/*
Expand Down Expand Up @@ -109,7 +107,7 @@ public class OpacityBar extends View {
* {@code true} if the user clicked on the pointer to start the move mode. <br>
* {@code false} once the user stops touching the screen.
*
* @see #onTouchEvent(android.view.MotionEvent)
* @see #onTouchEvent(MotionEvent)
*/
private boolean mIsMovingPointer;

Expand Down Expand Up @@ -139,7 +137,7 @@ public class OpacityBar extends View {
* to the host activity/fragment
*/
private OnOpacityChangedListener onOpacityChangedListener;

/**
* Opacity of the latest entry of the onOpacityChangedListener.
*/
Expand Down Expand Up @@ -262,7 +260,7 @@ else if (lengthMode == MeasureSpec.AT_MOST) {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

// Fill the rectangle instance based on orientation
int x1, y1;
if (mOrientation == ORIENTATION_HORIZONTAL) {
Expand Down Expand Up @@ -297,14 +295,14 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
0x0081ff00, 0xff81ff00 }, null, Shader.TileMode.CLAMP);
Color.colorToHSV(0xff81ff00, mHSVColor);
}

mBarPaint.setShader(shader);
mPosToOpacFactor = 0xFF / ((float) mBarLength);
mOpacToPosFactor = ((float) mBarLength) / 0xFF;

float[] hsvColor = new float[3];
Color.colorToHSV(mColor, hsvColor);

if (!isInEditMode()){
mBarPointerPosition = Math.round((mOpacToPosFactor * Color.alpha(mColor))
+ mBarPointerHaloRadius);
Expand All @@ -328,7 +326,7 @@ protected void onDraw(Canvas canvas) {
cX = mBarPointerHaloRadius;
cY = mBarPointerPosition;
}

// Draw the pointer halo.
canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint);
// Draw the pointer.
Expand Down Expand Up @@ -395,8 +393,19 @@ public boolean onTouchEvent(MotionEvent event) {
oldChangedListenerOpacity = getOpacity();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_UP:
mIsMovingPointer = false;
if(mPicker.onColorSelectedListener!= null){
mPicker.onColorSelectedListener.onColorSelected(mColor);
mPicker.oldSelectedListenerColor = mColor;
}
break;
case MotionEvent.ACTION_CANCEL:
mIsMovingPointer = false;
if(mPicker.onColorSelectedListener!= null){
mPicker.onColorSelectedListener.onColorSelected(mColor);
mPicker.oldSelectedListenerColor = mColor;
}
break;
}
return true;
Expand All @@ -406,7 +415,7 @@ public boolean onTouchEvent(MotionEvent event) {
* Set the bar color. <br>
* <br>
* Its discouraged to use this method.
*
*
* @param color
*/
public void setColor(int color) {
Expand All @@ -419,7 +428,7 @@ public void setColor(int color) {
x1 = mBarThickness;
y1 = (mBarLength + mBarPointerHaloRadius);
}

Color.colorToHSV(color, mHSVColor);
shader = new LinearGradient(mBarPointerHaloRadius, 0,
x1, y1, new int[] {
Expand All @@ -436,7 +445,7 @@ public void setColor(int color) {

/**
* Set the pointer on the bar. With the opacity value.
*
*
* @param opacity float between 0 and 255
*/
public void setOpacity(int opacity) {
Expand All @@ -452,7 +461,7 @@ public void setOpacity(int opacity) {

/**
* Get the currently selected opacity.
*
*
* @return The int value of the currently selected opacity.
*/
public int getOpacity() {
Expand All @@ -469,7 +478,7 @@ public int getOpacity() {

/**
* Calculate the color selected by the pointer on the bar.
*
*
* @param coord Coordinate of the pointer.
*/
private void calculateColor(int coord) {
Expand All @@ -492,7 +501,7 @@ private void calculateColor(int coord) {

/**
* Get the currently selected color.
*
*
* @return The ARGB value of the currently selected color.
*/
public int getColor() {
Expand All @@ -504,7 +513,7 @@ public int getColor() {
* <br>
* WARNING: Don't change the color picker. it is done already when the bar
* is added to the ColorPicker
*
*
* @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(SVBar)
* @param picker
*/
Expand Down
33 changes: 21 additions & 12 deletions libary/src/main/java/com/larswerkman/holocolorpicker/SVBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import android.view.MotionEvent;
import android.view.View;

import com.larswerkman.holocolorpicker.R;

public class SVBar extends View {

/*
Expand Down Expand Up @@ -110,7 +108,7 @@ public class SVBar extends View {
* {@code true} if the user clicked on the pointer to start the move mode. <br>
* {@code false} once the user stops touching the screen.
*
* @see #onTouchEvent(android.view.MotionEvent)
* @see #onTouchEvent(MotionEvent)
*/
private boolean mIsMovingPointer;

Expand Down Expand Up @@ -275,7 +273,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Shader.TileMode.CLAMP);
Color.colorToHSV(0xff81ff00, mHSVColor);
}

mBarPaint.setShader(shader);
mPosToSVFactor = 1 / ((float) mBarLength / 2);
mSVToPosFactor = ((float) mBarLength / 2) / 1;
Expand Down Expand Up @@ -309,7 +307,7 @@ protected void onDraw(Canvas canvas) {
cX = mBarPointerHaloRadius;
cY = mBarPointerPosition;
}

// Draw the pointer halo.
canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint);
// Draw the pointer.
Expand Down Expand Up @@ -377,14 +375,25 @@ public boolean onTouchEvent(MotionEvent event) {
break;
case MotionEvent.ACTION_UP:
mIsMovingPointer = false;
if(mPicker.onColorSelectedListener!= null){
mPicker.onColorSelectedListener.onColorSelected(mColor);
mPicker.oldSelectedListenerColor = mColor;
}
break;
case MotionEvent.ACTION_CANCEL:
mIsMovingPointer = false;
if(mPicker.onColorSelectedListener!= null){
mPicker.onColorSelectedListener.onColorSelected(mColor);
mPicker.oldSelectedListenerColor = mColor;
}
break;
}
return true;
}

/**
* Set the pointer on the bar. With the saturation value.
*
*
* @param saturation float between 0 and 1
*/
public void setSaturation(float saturation) {
Expand All @@ -403,7 +412,7 @@ public void setSaturation(float saturation) {

/**
* Set the pointer on the bar. With the Value value.
*
*
* @param value float between 0 and 1
*/
public void setValue(float value) {
Expand All @@ -424,7 +433,7 @@ public void setValue(float value) {
* Set the bar color. <br>
* <br>
* Its discouraged to use this method.
*
*
* @param color
*/
public void setColor(int color) {
Expand All @@ -436,7 +445,7 @@ public void setColor(int color) {
x1 = mBarThickness;
y1 = (mBarLength + mBarPointerHaloRadius);
}

Color.colorToHSV(color, mHSVColor);
shader = new LinearGradient(mBarPointerHaloRadius, 0,
x1, y1, new int[] {Color.WHITE, color, Color.BLACK}, null,
Expand All @@ -454,7 +463,7 @@ public void setColor(int color) {

/**
* Calculate the color selected by the pointer on the bar.
*
*
* @param coord Coordinate of the pointer.
*/
private void calculateColor(int coord) {
Expand All @@ -481,7 +490,7 @@ private void calculateColor(int coord) {

/**
* Get the currently selected color.
*
*
* @return The ARGB value of the currently selected color.
*/
public int getColor() {
Expand All @@ -493,7 +502,7 @@ public int getColor() {
* <br>
* WARNING: Don't change the color picker. it is done already when the bar
* is added to the ColorPicker
*
*
* @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(SVBar)
* @param picker
*/
Expand Down
Loading