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.BitmapShader;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.LinearGradient;
25 import android.graphics.Paint;
26 import android.graphics.RadialGradient;
27 import android.graphics.RectF;
28 import android.graphics.Shader;
29 import android.util.AttributeSet;
30 import android.util.DisplayMetrics;
31 import android.view.MotionEvent;
32 import android.view.View;
34 import com.android.gallery3d.R;
36 import java.util.ArrayList;
38 public class ColorHueView extends View implements ColorListener {
42 private Paint mLinePaint1;
43 private Paint mLinePaint2;
44 private Paint mPaint = new Paint();
45 private float mHeight;
46 private Paint mDotPaint;
47 private int mBgcolor = 0;
49 private float mDotRadius;
50 private float mBorder;
52 private float[] mHSVO = {0.f,0.f,0.f,0.f};
53 private int mSliderColor;
54 private float mDotX = mBorder;
55 private float mDotY = mBorder;
57 public final static float DOT_SIZE = 20;
58 public final static float BORDER_SIZE = 20;
59 RectF mRect = new RectF();
61 float[] mTmpHSV = new float[3];
62 private Paint mCheckPaint;
64 public ColorHueView(Context ctx, AttributeSet attrs) {
66 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
67 float mDpToPix = metrics.density;
68 mDotRadius = DOT_SIZE * mDpToPix;
69 mBorder = BORDER_SIZE * mDpToPix;
71 mDotPaint = new Paint();
73 mDotPaint.setStyle(Paint.Style.FILL);
74 mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
75 mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
78 mLinePaint1 = new Paint();
79 mLinePaint1.setColor(Color.GRAY);
80 mLinePaint2 = new Paint();
81 mLinePaint2.setColor(mSliderColor);
82 mLinePaint2.setStrokeWidth(4);
84 mBitmap = Bitmap.createBitmap(256, 2, Bitmap.Config.ARGB_8888);
85 mTmpBuff = new int[mBitmap.getWidth() * mBitmap.getHeight()];
86 mPaint.setAntiAlias(true);
87 mPaint.setFilterBitmap(true);
93 int w = mBitmap.getWidth();
94 int h = mBitmap.getHeight();
96 for (int x = 0; x < w; x++) {
97 float hue = 360 * (x) / (float) w;
102 int color = Color.HSVToColor(mTmpHSV);
104 mTmpBuff[x + w] = color;
108 mBitmap.setPixels(mTmpBuff, 0, w, 0, 0, w, h);
112 public boolean onDown(MotionEvent e) {
117 public boolean onTouchEvent(MotionEvent event) {
121 float x = event.getX();
122 float y = event.getY();
126 if (mDotX < mBorder) {
130 if (mDotX > mWidth - mBorder) {
131 mDotX = mWidth - mBorder;
133 mHSVO[0] = 360 * (mDotX - mBorder) / (mWidth - mBorder * 2);
134 notifyColorListeners(mHSVO);
137 // invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
138 // (int) (oy + mDotRadius));
140 // (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
141 // (int) (mDotY + mDotRadius));
147 private void setupButton() {
148 float pos = mHSVO[0] / 360 * (mWidth - mBorder * 2);
149 mDotX = pos + mBorder;
151 int[] colors3 = new int[]{
152 mSliderColor, mSliderColor, 0x66000000, 0};
153 RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[]{
154 0, .3f, .31f, 1}, Shader.TileMode.CLAMP);
155 mDotPaint.setShader(g);
159 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
168 protected void onDraw(Canvas canvas) {
169 super.onDraw(canvas);
170 canvas.drawColor(mBgcolor);
172 mRect.left = mBorder;
173 mRect.right = mWidth - mBorder;
175 mRect.bottom = mHeight;
176 canvas.drawRect(mRect,mCheckPaint);
177 canvas.drawBitmap(mBitmap, null, mRect, mPaint);
180 canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1);
181 canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2);
182 if (!Float.isNaN(mDotX)) {
183 canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint);
187 private void makeCheckPaint(){
189 int checkdim = block*2;
190 int[] colors = new int[checkdim * checkdim];
191 for (int i = 0; i < colors.length; i++) {
192 int y = i / (checkdim * block);
193 int x = (i / block) % 2;
194 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
196 Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888);
197 BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
198 mCheckPaint = new Paint();
199 mCheckPaint.setShader(bs);
203 public void setColor(float[] hsv) {
204 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
210 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
212 public void notifyColorListeners(float[] hsvo) {
213 for (ColorListener l : mColorListeners) {
218 public void addColorListener(ColorListener l) {
219 mColorListeners.add(l);
222 public void removeColorListener(ColorListener l) {
223 mColorListeners.remove(l);