diff options
Diffstat (limited to 'src/com/android/gallery3d/filtershow/colorpicker')
7 files changed, 867 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorGridDialog.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorGridDialog.java new file mode 100644 index 000000000..dd4df7dc8 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorGridDialog.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +import android.app.Dialog; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.GradientDrawable; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import com.android.gallery3d.R; + +import java.util.ArrayList; + +public class ColorGridDialog extends Dialog { + RGBListener mCallback; + private static final String LOGTAG = "ColorGridDialog"; + + public ColorGridDialog(Context context, final RGBListener cl) { + super(context); + mCallback = cl; + setTitle(R.string.color_pick_title); + setContentView(R.layout.filtershow_color_gird); + Button sel = (Button) findViewById(R.id.filtershow_cp_custom); + ArrayList<Button> b = getButtons((ViewGroup) getWindow().getDecorView()); + int k = 0; + float[] hsv = new float[3]; + + for (Button button : b) { + if (!button.equals(sel)){ + hsv[0] = (k % 5) * 360 / 5; + hsv[1] = (k / 5) / 3.0f; + hsv[2] = (k < 5) ? (k / 4f) : 1; + final int c = (Color.HSVToColor(hsv) & 0x00FFFFFF) | 0xAA000000; + GradientDrawable sd = ((GradientDrawable) button.getBackground()); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mCallback.setColor(c); + dismiss(); + } + }); + sd.setColor(c); + k++; + } + + } + sel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + showColorPicker(); + ColorGridDialog.this.dismiss(); + } + }); + } + + private ArrayList<Button> getButtons(ViewGroup vg) { + ArrayList<Button> list = new ArrayList<Button>(); + for (int i = 0; i < vg.getChildCount(); i++) { + View v = vg.getChildAt(i); + if (v instanceof Button) { + list.add((Button) v); + } else if (v instanceof ViewGroup) { + list.addAll(getButtons((ViewGroup) v)); + } + } + return list; + } + + public void showColorPicker() { + ColorListener cl = new ColorListener() { + @Override + public void setColor(float[] hsvo) { + int c = Color.HSVToColor(hsvo) & 0xFFFFFF; + int alpha = (int) (hsvo[3] * 255); + c |= alpha << 24; + mCallback.setColor(c); + } + }; + ColorPickerDialog cpd = new ColorPickerDialog(this.getContext(), cl); + cpd.show(); + } + +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorListener.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorListener.java new file mode 100644 index 000000000..5127dad26 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorListener.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +public interface ColorListener { + void setColor(float[] hsvo); +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorOpacityView.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorOpacityView.java new file mode 100644 index 000000000..2bff501f7 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorOpacityView.java @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapShader; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.RadialGradient; +import android.graphics.Shader; +import android.util.AttributeSet; +import android.util.DisplayMetrics; +import android.view.MotionEvent; +import android.view.View; + +import com.android.gallery3d.R; + +import java.util.ArrayList; + +public class ColorOpacityView extends View implements ColorListener { + + private float mRadius; + private float mWidth; + private Paint mBarPaint1; + private Paint mLinePaint1; + private Paint mLinePaint2; + private Paint mCheckPaint; + + private float mHeight; + private Paint mDotPaint; + private int mBgcolor = 0; + + private float mDotRadius; + private float mBorder; + + private float[] mHSVO = new float[4]; + private int mSliderColor; + private float mDotX = mBorder; + private float mDotY = mBorder; + private final static float DOT_SIZE = ColorRectView.DOT_SIZE; + public final static float BORDER_SIZE = 20;; + + public ColorOpacityView(Context ctx, AttributeSet attrs) { + super(ctx, attrs); + DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); + float mDpToPix = metrics.density; + mDotRadius = DOT_SIZE * mDpToPix; + mBorder = BORDER_SIZE * mDpToPix; + mBarPaint1 = new Paint(); + + mDotPaint = new Paint(); + + mDotPaint.setStyle(Paint.Style.FILL); + mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color)); + mSliderColor = ctx.getResources().getColor(R.color.slider_line_color); + + mBarPaint1.setStyle(Paint.Style.FILL); + + mLinePaint1 = new Paint(); + mLinePaint1.setColor(Color.GRAY); + mLinePaint2 = new Paint(); + mLinePaint2.setColor(mSliderColor); + mLinePaint2.setStrokeWidth(4); + + int[] colors = new int[16 * 16]; + for (int i = 0; i < colors.length; i++) { + int y = i / (16 * 8); + int x = (i / 8) % 2; + colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444; + } + Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888); + BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); + mCheckPaint = new Paint(); + mCheckPaint.setShader(bs); + } + + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + float ox = mDotX; + float oy = mDotY; + + float x = event.getX(); + float y = event.getY(); + + mDotX = x; + + if (mDotX < mBorder) { + mDotX = mBorder; + } + + if (mDotX > mWidth - mBorder) { + mDotX = mWidth - mBorder; + } + mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2); + notifyColorListeners(mHSVO); + setupButton(); + invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius), + (int) (oy + mDotRadius)); + invalidate( + (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius), + (int) (mDotY + mDotRadius)); + + return true; + } + + private void setupButton() { + float pos = mHSVO[3] * (mWidth - mBorder * 2); + mDotX = pos + mBorder; + + int[] colors3 = new int[] { + mSliderColor, mSliderColor, 0x66000000, 0 }; + RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[] { + 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP); + mDotPaint.setShader(g); + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + mHeight = h; + mDotY = mHeight / 2; + updatePaint(); + setupButton(); + } + + private void updatePaint() { + + int color2 = Color.HSVToColor(mHSVO); + int color1 = color2 & 0xFFFFFF; + + Shader sg = new LinearGradient( + mBorder, mBorder, mWidth - mBorder, mBorder, color1, color2, Shader.TileMode.CLAMP); + mBarPaint1.setShader(sg); + + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + canvas.drawColor(mBgcolor); + canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mCheckPaint); + canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1); + canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1); + canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2); + if (mDotX != Float.NaN) { + canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint); + } + } + + @Override + public void setColor(float[] hsv) { + System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length); + + float oy = mDotY; + + updatePaint(); + setupButton(); + invalidate(); + } + + ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); + + public void notifyColorListeners(float[] hsvo) { + for (ColorListener l : mColorListeners) { + l.setColor(hsvo); + } + } + + public void addColorListener(ColorListener l) { + mColorListeners.add(l); + } + + public void removeColorListener(ColorListener l) { + mColorListeners.remove(l); + } +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorPickerDialog.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorPickerDialog.java new file mode 100644 index 000000000..73a5c907c --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorPickerDialog.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +import android.app.Dialog; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.GradientDrawable; +import android.view.View; +import android.widget.Button; +import android.widget.ToggleButton; + +import com.android.gallery3d.R; + +public class ColorPickerDialog extends Dialog implements ColorListener { + ToggleButton mSelectedButton; + GradientDrawable mSelectRect; + + float[] mHSVO = new float[4]; + + public ColorPickerDialog(Context context, final ColorListener cl) { + super(context); + + setContentView(R.layout.filtershow_color_picker); + ColorValueView csv = (ColorValueView) findViewById(R.id.colorValueView); + ColorRectView cwv = (ColorRectView) findViewById(R.id.colorRectView); + ColorOpacityView cvv = (ColorOpacityView) findViewById(R.id.colorOpacityView); + float[] hsvo = new float[] { + 123, .9f, 1, 1 }; + + mSelectRect = (GradientDrawable) getContext() + .getResources().getDrawable(R.drawable.filtershow_color_picker_roundrect); + Button selButton = (Button) findViewById(R.id.btnSelect); + selButton.setCompoundDrawablesWithIntrinsicBounds(null, null, mSelectRect, null); + Button sel = (Button) findViewById(R.id.btnSelect); + + sel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ColorPickerDialog.this.dismiss(); + if (cl != null) { + cl.setColor(mHSVO); + } + } + }); + + cwv.setColor(hsvo); + cvv.setColor(hsvo); + csv.setColor(hsvo); + csv.addColorListener(cwv); + cwv.addColorListener(csv); + csv.addColorListener(cvv); + cwv.addColorListener(cvv); + cvv.addColorListener(cwv); + cvv.addColorListener(csv); + cvv.addColorListener(this); + csv.addColorListener(this); + cwv.addColorListener(this); + + } + + void toggleClick(ToggleButton v, int[] buttons, boolean isChecked) { + int id = v.getId(); + if (!isChecked) { + mSelectedButton = null; + return; + } + for (int i = 0; i < buttons.length; i++) { + if (id != buttons[i]) { + ToggleButton b = (ToggleButton) findViewById(buttons[i]); + b.setChecked(false); + } + } + mSelectedButton = v; + + float[] hsv = (float[]) v.getTag(); + + ColorValueView csv = (ColorValueView) findViewById(R.id.colorValueView); + ColorRectView cwv = (ColorRectView) findViewById(R.id.colorRectView); + ColorOpacityView cvv = (ColorOpacityView) findViewById(R.id.colorOpacityView); + cwv.setColor(hsv); + cvv.setColor(hsv); + csv.setColor(hsv); + } + + @Override + public void setColor(float[] hsvo) { + System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length); + int color = Color.HSVToColor(hsvo); + mSelectRect.setColor(color); + setButtonColor(mSelectedButton, hsvo); + } + + private void setButtonColor(ToggleButton button, float[] hsv) { + if (button == null) { + return; + } + int color = Color.HSVToColor(hsv); + button.setBackgroundColor(color); + float[] fg = new float[] { + (hsv[0] + 180) % 360, + hsv[1], + (hsv[2] > .5f) ? .1f : .9f + }; + button.setTextColor(Color.HSVToColor(fg)); + button.setTag(hsv); + } + +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorRectView.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorRectView.java new file mode 100644 index 000000000..07d7c7126 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorRectView.java @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.RadialGradient; +import android.graphics.RectF; +import android.graphics.Shader; +import android.graphics.SweepGradient; +import android.util.AttributeSet; +import android.util.DisplayMetrics; +import android.view.MotionEvent; +import android.view.View; + +import com.android.gallery3d.R; + +import java.util.ArrayList; + +public class ColorRectView extends View implements ColorListener { + private float mDpToPix; + private float mRadius = 80; + private float mCtrY = 100; + private Paint mWheelPaint1; + private Paint mWheelPaint2; + private Paint mWheelPaint3; + private float mCtrX = 100; + private Paint mDotPaint; + private float mDotRadus; + private float mBorder; + private int mBgcolor = 0; + private float mDotX = Float.NaN; + private float mDotY; + private int mSliderColor = 0xFF33B5E5; + private float[] mHSVO = new float[4]; + private int[] mColors = new int[] { + 0xFFFF0000,// red + 0xFFFFFF00,// yellow + 0xFF00FF00,// green + 0xFF00FFFF,// cyan + 0xFF0000FF,// blue + 0xFFFF00FF,// magenta + 0xFFFF0000,// red + }; + private int mWidth; + private int mHeight; + public final static float DOT_SIZE = 20; + public final static float BORDER_SIZE = 10; + + public ColorRectView(Context ctx, AttributeSet attrs) { + super(ctx, attrs); + + DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); + mDpToPix = metrics.density; + mDotRadus = DOT_SIZE * mDpToPix; + mBorder = BORDER_SIZE * mDpToPix; + + mWheelPaint1 = new Paint(); + mWheelPaint2 = new Paint(); + mWheelPaint3 = new Paint(); + mDotPaint = new Paint(); + + mDotPaint.setStyle(Paint.Style.FILL); + mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color)); + mSliderColor = ctx.getResources().getColor(R.color.slider_line_color); + mWheelPaint1.setStyle(Paint.Style.FILL); + mWheelPaint2.setStyle(Paint.Style.FILL); + mWheelPaint3.setStyle(Paint.Style.FILL); + } + + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + + invalidate((int) (mDotX - mDotRadus), (int) (mDotY - mDotRadus), (int) (mDotX + mDotRadus), + (int) (mDotY + mDotRadus)); + float x = event.getX(); + float y = event.getY(); + + x = Math.max(Math.min(x, mWidth - mBorder), mBorder); + y = Math.max(Math.min(y, mHeight - mBorder), mBorder); + mDotX = x; + mDotY = y; + float sat = 1 - (mDotY - mBorder) / (mHeight - 2 * mBorder); + if (sat > 1) { + sat = 1; + } + + double hue = Math.PI * 2 * (mDotX - mBorder) / (mHeight - 2 * mBorder); + mHSVO[0] = ((float) Math.toDegrees(hue) + 360) % 360; + mHSVO[1] = sat; + notifyColorListeners(mHSVO); + updateDotPaint(); + invalidate((int) (mDotX - mDotRadus), (int) (mDotY - mDotRadus), (int) (mDotX + mDotRadus), + (int) (mDotY + mDotRadus)); + + return true; + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + mHeight = h; + mCtrY = h / 2f; + mCtrX = w / 2f; + mRadius = Math.min(mCtrY, mCtrX) - 2 * mBorder; + setUpColorPanel(); + } + + private void setUpColorPanel() { + float val = mHSVO[2]; + int v = 0xFF000000 | 0x10101 * (int) (val * 0xFF); + int[] colors = new int[] { + 0x0000000, v }; + int[] colors2 = new int[] { + 0x0000000, 0xFF000000 }; + int[] wheelColor = new int[mColors.length]; + float[] hsv = new float[3]; + for (int i = 0; i < wheelColor.length; i++) { + Color.colorToHSV(mColors[i], hsv); + hsv[2] = mHSVO[2]; + wheelColor[i] = Color.HSVToColor(hsv); + } + updateDot(); + updateDotPaint(); + SweepGradient sg = new SweepGradient(mCtrX, mCtrY, wheelColor, null); + LinearGradient lg = new LinearGradient( + mBorder, 0, mWidth - mBorder, 0, wheelColor, null, Shader.TileMode.CLAMP); + + mWheelPaint1.setShader(lg); + LinearGradient rg = new LinearGradient( + 0, mBorder, 0, mHeight - mBorder, colors, null, Shader.TileMode.CLAMP); + mWheelPaint2.setShader(rg); + LinearGradient rg2 = new LinearGradient( + 0, mBorder, 0, mHeight - mBorder, colors2, null, Shader.TileMode.CLAMP); + mWheelPaint3.setShader(rg2); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + canvas.drawColor(mBgcolor); + RectF rect = new RectF(); + rect.left = mBorder; + rect.right = mWidth - mBorder; + rect.top = mBorder; + rect.bottom = mHeight - mBorder; + + canvas.drawRect(rect, mWheelPaint1); + canvas.drawRect(rect, mWheelPaint3); + canvas.drawRect(rect, mWheelPaint2); + + if (mDotX != Float.NaN) { + + canvas.drawCircle(mDotX, mDotY, mDotRadus, mDotPaint); + } + } + + private void updateDot() { + + double hue = mHSVO[0]; + double sat = mHSVO[1]; + + mDotX = (float) (mBorder + (mHeight - 2 * mBorder) * Math.toRadians(hue) / (Math.PI * 2)); + mDotY = (float) ((1 - sat) * (mHeight - 2 * mBorder) + mBorder); + + } + + private void updateDotPaint() { + int[] colors3 = new int[] { + mSliderColor, mSliderColor, 0x66000000, 0 }; + RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadus, colors3, new float[] { + 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP); + mDotPaint.setShader(g); + + } + + @Override + public void setColor(float[] hsvo) { + System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length); + + setUpColorPanel(); + invalidate(); + + updateDot(); + updateDotPaint(); + + } + + ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); + + public void notifyColorListeners(float[] hsv) { + for (ColorListener l : mColorListeners) { + l.setColor(hsv); + } + } + + public void addColorListener(ColorListener l) { + mColorListeners.add(l); + } + + public void removeColorListener(ColorListener l) { + mColorListeners.remove(l); + } +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/ColorValueView.java b/src/com/android/gallery3d/filtershow/colorpicker/ColorValueView.java new file mode 100644 index 000000000..13cb44bad --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/ColorValueView.java @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.RadialGradient; +import android.graphics.Shader; +import android.util.AttributeSet; +import android.util.DisplayMetrics; +import android.view.MotionEvent; +import android.view.View; + +import com.android.gallery3d.R; + +import java.util.ArrayList; + +public class ColorValueView extends View implements ColorListener { + + private float mRadius; + private float mWidth; + private Paint mBarPaint1; + private Paint mLinePaint1; + private Paint mLinePaint2; + private float mHeight; + private int mBgcolor = 0; + private Paint mDotPaint; + private float dotRadus; + private float mBorder; + + private float[] mHSVO = new float[4]; + private int mSliderColor; + private float mDotX; + private float mDotY = mBorder; + private final static float DOT_SIZE = ColorRectView.DOT_SIZE; + private final static float BORDER_SIZE = ColorRectView.DOT_SIZE; + + public ColorValueView(Context ctx, AttributeSet attrs) { + super(ctx, attrs); + DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); + float mDpToPix = metrics.density; + dotRadus = DOT_SIZE * mDpToPix; + mBorder = BORDER_SIZE * mDpToPix; + + mBarPaint1 = new Paint(); + + mDotPaint = new Paint(); + + mDotPaint.setStyle(Paint.Style.FILL); + mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color)); + + mBarPaint1.setStyle(Paint.Style.FILL); + + mLinePaint1 = new Paint(); + mLinePaint1.setColor(Color.GRAY); + mLinePaint2 = new Paint(); + mSliderColor = ctx.getResources().getColor(R.color.slider_line_color); + mLinePaint2.setColor(mSliderColor); + mLinePaint2.setStrokeWidth(4); + } + + public boolean onDown(MotionEvent e) { + return true; + } + + public boolean onTouchEvent(MotionEvent event) { + float ox = mDotX; + float oy = mDotY; + + float x = event.getX(); + float y = event.getY(); + + mDotY = y; + + if (mDotY < mBorder) { + mDotY = mBorder; + } + + if (mDotY > mHeight - mBorder) { + mDotY = mHeight - mBorder; + } + mHSVO[2] = (mDotY - mBorder) / (mHeight - mBorder * 2); + notifyColorListeners(mHSVO); + setupButton(); + invalidate((int) (ox - dotRadus), (int) (oy - dotRadus), (int) (ox + dotRadus), + (int) (oy + dotRadus)); + invalidate((int) (mDotX - dotRadus), (int) (mDotY - dotRadus), (int) (mDotX + dotRadus), + (int) (mDotY + dotRadus)); + + return true; + } + + private void setupButton() { + float pos = mHSVO[2] * (mHeight - mBorder * 2); + mDotY = pos + mBorder; + + int[] colors3 = new int[] { + mSliderColor, mSliderColor, 0x66000000, 0 }; + RadialGradient g = new RadialGradient(mDotX, mDotY, dotRadus, colors3, new float[] { + 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP); + mDotPaint.setShader(g); + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + mHeight = h; + mDotX = mWidth / 2; + updatePaint(); + setupButton(); + } + + private void updatePaint() { + float[] hsv = new float[] { + mHSVO[0], mHSVO[1], 0f }; + int color1 = Color.HSVToColor(hsv); + hsv[2] = 1; + int color2 = Color.HSVToColor(hsv); + + Shader sg = new LinearGradient(mBorder, mBorder, mBorder, mHeight - mBorder, color1, color2, + Shader.TileMode.CLAMP); + mBarPaint1.setShader(sg); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + canvas.drawColor(mBgcolor); + canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1); + canvas.drawLine(mDotX, mDotY, mDotX, mHeight - mBorder, mLinePaint2); + canvas.drawLine(mDotX, mBorder, mDotX, mDotY, mLinePaint1); + if (mDotX != Float.NaN) { + canvas.drawCircle(mDotX, mDotY, dotRadus, mDotPaint); + } + } + + @Override + public void setColor(float[] hsvo) { + System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length); + + float oy = mDotY; + updatePaint(); + setupButton(); + invalidate(); + + } + + ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); + + public void notifyColorListeners(float[] hsv) { + for (ColorListener l : mColorListeners) { + l.setColor(hsv); + } + } + + public void addColorListener(ColorListener l) { + mColorListeners.add(l); + } + + public void removeColorListener(ColorListener l) { + mColorListeners.remove(l); + } +} diff --git a/src/com/android/gallery3d/filtershow/colorpicker/RGBListener.java b/src/com/android/gallery3d/filtershow/colorpicker/RGBListener.java new file mode 100644 index 000000000..147fb91a4 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/colorpicker/RGBListener.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.colorpicker; + +public interface RGBListener { + void setColor(int hsv); +} |