2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.filtershow.colorpicker;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.LinearGradient;
24 import android.graphics.Paint;
25 import android.graphics.RadialGradient;
26 import android.graphics.Rect;
27 import android.graphics.RectF;
28 import android.graphics.Shader;
29 import android.graphics.SweepGradient;
30 import android.util.AttributeSet;
31 import android.util.DisplayMetrics;
32 import android.view.MotionEvent;
33 import android.view.View;
35 import com.android.gallery3d.R;
37 import java.util.ArrayList;
39 public class ColorSVRectView extends View implements ColorListener {
40 private float mDpToPix;
42 private float mCtrY = 100;
43 private Paint mPaint1;
45 private float mCtrX = 100;
46 private Paint mDotPaint = new Paint();
47 private float mDotRadus;
48 private float mBorder;
50 private float mDotX = Float.NaN;
52 private int mSliderColor = 0xFF33B5E5;
53 private float[] mHSVO = new float[]{0, 1, 1, 1};
54 RectF mRect = new RectF();
58 public final static float DOT_SIZE = 20;
59 public final static float BORDER_SIZE = 20;
62 public ColorSVRectView(Context ctx, AttributeSet attrs) {
65 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
66 mDpToPix = metrics.density;
67 mDotRadus = DOT_SIZE * mDpToPix;
68 mBorder = BORDER_SIZE * mDpToPix;
70 mPaint1 = new Paint();
73 mDotPaint.setStyle(Paint.Style.FILL);
75 mDotPaint.setColor(0x646464);
76 mSliderColor = 0x888888;
78 mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
79 mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
81 mPaint1.setStyle(Paint.Style.FILL);
82 mPaint1.setAntiAlias(true);
83 mPaint1.setFilterBitmap(true);
85 mBitmap = Bitmap.createBitmap(64, 46, Bitmap.Config.ARGB_8888);
90 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
91 super.onMeasure(widthMeasureSpec, widthMeasureSpec);
95 int w = mBitmap.getWidth();
96 int h = mBitmap.getHeight();
97 int[] buff = new int[w * h];
98 float[] hsv = new float[3];
100 for (int i = 0; i < w * h; i++) {
101 float sat = (i % w) / (float) w;
102 float val = (w - i / w) / (float) w;
105 buff[i] = Color.HSVToColor(hsv);
107 mBitmap.setPixels(buff, 0, w, 0, 0, w, h);
110 private void setUpColorPanel() {
119 protected void onDraw(Canvas canvas) {
120 super.onDraw(canvas);
121 Rect r = canvas.getClipBounds();
123 mRect.top += mBorder;
124 mRect.bottom -= mBorder;
125 mRect.left += mBorder;
126 mRect.right -= mBorder;
127 canvas.drawBitmap(mBitmap, null, mRect, mPaint1);
129 if (mDotX != Float.NaN) {
131 canvas.drawCircle(mDotX, mDotY, mDotRadus, mDotPaint);
136 public boolean onDown(MotionEvent e) {
141 public boolean onTouchEvent(MotionEvent event) {
143 invalidate((int) (mDotX - mDotRadus), (int) (mDotY - mDotRadus), (int) (mDotX + mDotRadus),
144 (int) (mDotY + mDotRadus));
145 float x = event.getX();
146 float y = event.getY();
148 x = Math.max(Math.min(x, mWidth - mBorder), mBorder);
149 y = Math.max(Math.min(y, mHeight - mBorder), mBorder);
152 float sat = 1 - (mDotY - mBorder) / (mHeight - 2 * mBorder);
157 float value = (mDotX - mBorder) / (mHeight - 2 * mBorder);
160 notifyColorListeners(mHSVO);
162 invalidate((int) (mDotX - mDotRadus), (int) (mDotY - mDotRadus), (int) (mDotX + mDotRadus),
163 (int) (mDotY + mDotRadus));
169 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
179 private void updateDot() {
181 double hue = mHSVO[0];
182 double sat = mHSVO[1];
183 double val = mHSVO[2];
184 double opc = mHSVO[3];
186 mDotX = (float) (mBorder + (mHeight - 2 * mBorder) * sat);
187 mDotY = (float) ((1 - val) * (mHeight - 2 * mBorder) + mBorder);
191 private void updateDotPaint() {
192 int[] colors3 = new int[]{
193 mSliderColor, mSliderColor, 0x66000000, 0};
194 RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadus, colors3, new float[]{
195 0, .3f, .31f, 1}, Shader.TileMode.CLAMP);
196 mDotPaint.setShader(g);
201 public void setColor(float[] hsvo) {
202 if (hsvo[0] == mHSVO[0]
203 && hsvo[1] == mHSVO[1]
204 && hsvo[2] == mHSVO[2]) {
205 mHSVO[3] = hsvo[3]; // we don't update if color stays the same
208 System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length);
218 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
220 public void notifyColorListeners(float[] hsv) {
221 for (ColorListener l : mColorListeners) {
226 public void addColorListener(ColorListener l) {
227 mColorListeners.add(l);
230 public void removeColorListener(ColorListener l) {
231 mColorListeners.remove(l);