private void showColorPickerDialog( Context context, int preColor, final ColorPicker.OnColorChangedListener listener) { AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_DARK); LinearLayout linearLayout = new LinearLayout(context); linearLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(params); final ColorPicker picker = new ColorPicker(context); picker.setLayoutParams(params); OpacityBar opacityBar = new OpacityBar(context); opacityBar.setLayoutParams(params); SaturationBar saturationBar = new SaturationBar(context); saturationBar.setLayoutParams(params); ValueBar valueBar = new ValueBar(context); valueBar.setLayoutParams(params); linearLayout.addView(picker); linearLayout.addView(opacityBar); linearLayout.addView(saturationBar); linearLayout.addView(valueBar); picker.addOpacityBar(opacityBar); picker.addSaturationBar(saturationBar); picker.addValueBar(valueBar); picker.setOldCenterColor(preColor); picker.setColor(preColor); builder.setView(linearLayout); builder.setPositiveButton( "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { setColor(picker.getColor()); listener.onColorChanged(picker.getColor()); } }); builder.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alertDialog = builder.create(); alertDialog.setCanceledOnTouchOutside(false); alertDialog.show(); }
@Override protected View onCreateDialogView() { View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_colorpicker, null); mColorPickerView = (ColorPicker) view.findViewById(R.id.pk_colorPicker); OpacityBar mOpacityBar = (OpacityBar) view.findViewById(R.id.pk_opacityBar); SVBar svBar = (SVBar) view.findViewById(R.id.pk_svBar); mOpacityBar.setColorPicker(mColorPickerView); svBar.setColorPicker(mColorPickerView); mColorPickerView.addOpacityBar(mOpacityBar); mColorPickerView.addSVBar(svBar); mOpacityBar.setVisibility(mAlphaAllowed ? View.VISIBLE : View.GONE); svBar.setVisibility(mSVAllowed ? View.VISIBLE : View.GONE); return view; }
/** * 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); }
/** * Used to change the color of the {@code OpacityBar} used by the {@code SVBar} if there is an * change in color. * * @param color int of the color used to change the opacity bar color. */ public void changeOpacityBarColor(int color) { if (mOpacityBar != null) { mOpacityBar.setColor(color); } }
/** * Add a Opacity bar to the color wheel. * * @param bar The instance of the Opacity bar. */ public void addOpacityBar(OpacityBar bar) { mOpacityBar = bar; // Give an instance of the color picker to the Opacity bar. mOpacityBar.setColorPicker(this); mOpacityBar.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; }