@Override protected void onRestoreInstanceState(Parcelable state) { Bundle savedState = (Bundle) state; Parcelable superState = savedState.getParcelable(STATE_PARENT); super.onRestoreInstanceState(superState); setColor(Color.HSVToColor(savedState.getFloatArray(STATE_COLOR))); if (savedState.containsKey(STATE_SATURATION)) { setSaturation(savedState.getFloat(STATE_SATURATION)); } else { setValue(savedState.getFloat(STATE_VALUE)); } mOrientation = savedState.getBoolean(STATE_ORIENTATION, ORIENTATION_DEFAULT); }
/** * Set the color to be highlighted by the pointer. If the instances {@code SVBar} and the {@code * OpacityBar} aren't null the color will also be set to them * * @param color The RGB value of the color to highlight. If this is not a color displayed on the * color wheel a very simple algorithm is used to map it to the color wheel. The resulting * color often won't look close to the original color. This is especially true for shades of * grey. You have been warned! */ public void setColor(int color) { mAngle = colorToAngle(color); mPointerColor.setColor(calculateColor(mAngle)); // check of the instance isn't null if (mOpacityBar != null) { // set the value of the opacity mOpacityBar.setColor(mColor); mOpacityBar.setOpacity(Color.alpha(color)); } // check if the instance isn't null if (mSVbar != null) { // the array mHSV will be filled with the HSV values of the color. Color.colorToHSV(color, mHSV); mSVbar.setColor(mColor); // because of the design of the Saturation/Value bar, // we can only use Saturation or Value every time. // Here will be checked which we shall use. if (mHSV[1] < mHSV[2]) { mSVbar.setSaturation(mHSV[1]); } else if (mHSV[1] > mHSV[2]) { mSVbar.setValue(mHSV[2]); } } if (mSaturationBar != null) { Color.colorToHSV(color, mHSV); mSaturationBar.setColor(mColor); mSaturationBar.setSaturation(mHSV[1]); } if (mValueBar != null && mSaturationBar == null) { Color.colorToHSV(color, mHSV); mValueBar.setColor(mColor); mValueBar.setValue(mHSV[2]); } else if (mValueBar != null) { Color.colorToHSV(color, mHSV); mValueBar.setValue(mHSV[2]); } setNewCenterColor(color); }
/** * Add a Saturation/Value bar to the color wheel. * * @param bar The instance of the Saturation/Value bar. */ public void addSVBar(SVBar bar) { mSVbar = bar; // Give an instance of the color picker to the Saturation/Value bar. mSVbar.setColorPicker(this); mSVbar.setColor(mColor); }
@Override public boolean onTouchEvent(MotionEvent event) { getParent().requestDisallowInterceptTouchEvent(true); // Convert coordinates to our internal coordinate system float x = event.getX() - mTranslationOffset; float y = event.getY() - mTranslationOffset; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Check whether the user pressed on the pointer. float[] pointerPosition = calculatePointerPosition(mAngle); if (x >= (pointerPosition[0] - mColorPointerHaloRadius) && x <= (pointerPosition[0] + mColorPointerHaloRadius) && y >= (pointerPosition[1] - mColorPointerHaloRadius) && y <= (pointerPosition[1] + mColorPointerHaloRadius)) { mSlopX = x - pointerPosition[0]; mSlopY = y - pointerPosition[1]; mUserIsMovingPointer = true; invalidate(); } // Check whether the user pressed on the center. else if (x >= -mColorCenterRadius && x <= mColorCenterRadius && y >= -mColorCenterRadius && y <= mColorCenterRadius && mShowCenterOldColor) { mCenterHaloPaint.setAlpha(0x50); setColor(getOldCenterColor()); invalidate(); } // Check whether the user pressed anywhere on the wheel. else if (Math.sqrt(x * x + y * y) <= mColorWheelRadius + mColorPointerHaloRadius && Math.sqrt(x * x + y * y) >= mColorWheelRadius - mColorPointerHaloRadius && mTouchAnywhereOnColorWheelEnabled) { mUserIsMovingPointer = true; invalidate(); } // If user did not press pointer or center, report event not handled else { getParent().requestDisallowInterceptTouchEvent(false); return false; } break; case MotionEvent.ACTION_MOVE: if (mUserIsMovingPointer) { mAngle = (float) Math.atan2(y - mSlopY, x - mSlopX); mPointerColor.setColor(calculateColor(mAngle)); setNewCenterColor(mCenterNewColor = calculateColor(mAngle)); if (mOpacityBar != null) { mOpacityBar.setColor(mColor); } if (mValueBar != null) { mValueBar.setColor(mColor); } if (mSaturationBar != null) { mSaturationBar.setColor(mColor); } if (mSVbar != null) { mSVbar.setColor(mColor); } invalidate(); } // If user did not press pointer or center, report event not handled else { getParent().requestDisallowInterceptTouchEvent(false); return false; } break; case MotionEvent.ACTION_UP: mUserIsMovingPointer = false; mCenterHaloPaint.setAlpha(0x00); if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) { onColorSelectedListener.onColorSelected(mCenterNewColor); oldSelectedListenerColor = mCenterNewColor; } invalidate(); break; case MotionEvent.ACTION_CANCEL: if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) { onColorSelectedListener.onColorSelected(mCenterNewColor); oldSelectedListenerColor = mCenterNewColor; } break; } return true; }