1f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford/*
2f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * Copyright (C) 2013 The Android Open Source Project
3f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford *
4f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * you may not use this file except in compliance with the License.
6f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * You may obtain a copy of the License at
7f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford *
8f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford *
10f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * Unless required by applicable law or agreed to in writing, software
11f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * See the License for the specific language governing permissions and
14f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford * limitations under the License.
15f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford */
16f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
17f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordpackage com.android.gallery3d.filtershow.colorpicker;
18f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
19f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.content.Context;
20f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Bitmap;
21f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.BitmapShader;
22f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Canvas;
23f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Color;
24f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.LinearGradient;
25f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Paint;
26f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.RadialGradient;
27a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport android.graphics.RectF;
28f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Shader;
29f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.util.AttributeSet;
30f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.util.DisplayMetrics;
31f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.view.MotionEvent;
32f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.view.View;
33f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
34f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.R;
35f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
36f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport java.util.ArrayList;
37f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
38f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordpublic class ColorHueView extends View implements ColorListener {
39f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
40f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mWidth;
41a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
42f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private Paint mLinePaint1;
43f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private Paint mLinePaint2;
44a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    private Paint mPaint = new Paint();
45f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mHeight;
46f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private Paint mDotPaint;
47f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private int mBgcolor = 0;
48a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    Bitmap mBitmap;
49f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mDotRadius;
50f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mBorder;
51f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
52a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    private float[] mHSVO = {0.f,0.f,0.f,0.f};
53f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private int mSliderColor;
54f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mDotX = mBorder;
55f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private float mDotY = mBorder;
56f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
57a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    public final static float DOT_SIZE = 20;
58a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    public final static float BORDER_SIZE = 20;
59a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    RectF mRect = new RectF();
60a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    int[] mTmpBuff;
61a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    float[] mTmpHSV = new float[3];
62a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    private Paint mCheckPaint;
63f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
64f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public ColorHueView(Context ctx, AttributeSet attrs) {
65f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        super(ctx, attrs);
66f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
67f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float mDpToPix = metrics.density;
68f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotRadius = DOT_SIZE * mDpToPix;
69f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mBorder = BORDER_SIZE * mDpToPix;
70f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
71f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotPaint = new Paint();
72f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
73f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotPaint.setStyle(Paint.Style.FILL);
74f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
75f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
76f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
77f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
78f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mLinePaint1 = new Paint();
79f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mLinePaint1.setColor(Color.GRAY);
80f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mLinePaint2 = new Paint();
81f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mLinePaint2.setColor(mSliderColor);
82f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mLinePaint2.setStrokeWidth(4);
83f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
846125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        mBitmap = Bitmap.createBitmap(256, 2, Bitmap.Config.ARGB_8888);
856125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        mTmpBuff = new int[mBitmap.getWidth() * mBitmap.getHeight()];
86a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mPaint.setAntiAlias(true);
87a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mPaint.setFilterBitmap(true);
88a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        fillBitmap();
89a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        makeCheckPaint();
90a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    }
91a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
92a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    void fillBitmap() {
93a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int w = mBitmap.getWidth();
94a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int h = mBitmap.getHeight();
95a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
96a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        for (int x = 0; x < w; x++) {
97a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            float hue = 360 * (x) / (float) w;
98a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
99a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mTmpHSV[0] = hue;
100a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mTmpHSV[1] = 1;
101a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mTmpHSV[2] = 1;
1026125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            int color = Color.HSVToColor(mTmpHSV);
103a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mTmpBuff[x] = color;
104a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mTmpBuff[x + w] = color;
1056125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford
106f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
107a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
108a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mBitmap.setPixels(mTmpBuff, 0, w, 0, 0, w, h);
109f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
110f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
111a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
112f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public boolean onDown(MotionEvent e) {
113f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        return true;
114f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
115f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
116f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    @Override
117f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public boolean onTouchEvent(MotionEvent event) {
118f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float ox = mDotX;
119f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float oy = mDotY;
120f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
121f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float x = event.getX();
122f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float y = event.getY();
123f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
124f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotX = x;
125f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
126f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        if (mDotX < mBorder) {
127f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            mDotX = mBorder;
128f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
129f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
130f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        if (mDotX > mWidth - mBorder) {
131f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            mDotX = mWidth - mBorder;
132f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
133a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mHSVO[0] = 360 * (mDotX - mBorder) / (mWidth - mBorder * 2);
134f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        notifyColorListeners(mHSVO);
135f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        setupButton();
136a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        fillBitmap();
137a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford//        invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
138a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford//                (int) (oy + mDotRadius));
139a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford//        invalidate(
140a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford//                (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
141a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford//                (int) (mDotY + mDotRadius));
142a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        invalidate();
143f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
144f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        return true;
145f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
146f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
147f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private void setupButton() {
148a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        float pos = mHSVO[0] / 360 * (mWidth - mBorder * 2);
149f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotX = pos + mBorder;
150f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
151a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int[] colors3 = new int[]{
152a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                mSliderColor, mSliderColor, 0x66000000, 0};
153a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[]{
154a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                0, .3f, .31f, 1}, Shader.TileMode.CLAMP);
155f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotPaint.setShader(g);
156f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
157f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
158f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    @Override
159f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
160f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mWidth = w;
161f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mHeight = h;
162f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mDotY = mHeight / 2;
163f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        setupButton();
164f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
165f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
166f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
167f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    @Override
168f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    protected void onDraw(Canvas canvas) {
169f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        super.onDraw(canvas);
170f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        canvas.drawColor(mBgcolor);
171a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
172a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mRect.left = mBorder;
173a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mRect.right = mWidth - mBorder;
17497d76abecd25656bcd622c56d9cf4e2c9b236ea3John Hoford        mRect.top = 0;
17597d76abecd25656bcd622c56d9cf4e2c9b236ea3John Hoford        mRect.bottom = mHeight;
176a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        canvas.drawRect(mRect,mCheckPaint);
177a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        canvas.drawBitmap(mBitmap, null, mRect, mPaint);
178a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
179a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
180f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1);
181f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2);
182a900c0a7b4f3fd4797290428a97c48066a2689ebIan Rogers        if (!Float.isNaN(mDotX)) {
183f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint);
184f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
185f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
186f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
187a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    private void makeCheckPaint(){
1886125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        int block = 16;
1896125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        int checkdim = block*2;
1906125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        int[] colors = new int[checkdim * checkdim];
191a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        for (int i = 0; i < colors.length; i++) {
1926125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            int y = i / (checkdim * block);
1936125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            int x = (i / block) % 2;
194a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
195a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        }
196a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888);
197a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
198a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mCheckPaint = new Paint();
199a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mCheckPaint.setShader(bs);
200a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    }
201a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
202f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    @Override
203f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void setColor(float[] hsv) {
204f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
205a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        fillBitmap();
206f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        setupButton();
207f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        invalidate();
208f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
209f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
210a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
211a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
212f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void notifyColorListeners(float[] hsvo) {
213f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        for (ColorListener l : mColorListeners) {
214f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            l.setColor(hsvo);
215f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
216f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
217f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
218f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void addColorListener(ColorListener l) {
219f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mColorListeners.add(l);
220f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
221f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
222f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void removeColorListener(ColorListener l) {
223f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mColorListeners.remove(l);
224f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
225f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford}
226