1915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford/*
2915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * Copyright (C) 2013 The Android Open Source Project
3915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford *
4915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * you may not use this file except in compliance with the License.
6915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * You may obtain a copy of the License at
7915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford *
8915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford *
10915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * Unless required by applicable law or agreed to in writing, software
11915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * See the License for the specific language governing permissions and
14915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford * limitations under the License.
15915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford */
16915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
17915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordpackage com.android.gallery3d.filtershow.colorpicker;
18915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
19915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.content.Context;
20915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Canvas;
21915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Color;
22915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.LinearGradient;
23915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Paint;
24915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.RadialGradient;
25915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Shader;
26915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.util.AttributeSet;
27915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.util.DisplayMetrics;
28915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.view.MotionEvent;
29915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.view.View;
30915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
31915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport com.android.gallery3d.R;
32915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
33915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport java.util.ArrayList;
34915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
35915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordpublic class ColorValueView extends View implements ColorListener {
36915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
37915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mRadius;
38915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mWidth;
39915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private Paint mBarPaint1;
40915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private Paint mLinePaint1;
41915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private Paint mLinePaint2;
42915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mHeight;
43915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private int mBgcolor = 0;
44915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private Paint mDotPaint;
45915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float dotRadus;
46915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mBorder;
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
48915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float[] mHSVO = new float[4];
49915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private int mSliderColor;
50915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mDotX;
51915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private float mDotY = mBorder;
52915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private final static float DOT_SIZE = ColorRectView.DOT_SIZE;
53915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private final static float BORDER_SIZE = ColorRectView.DOT_SIZE;
54915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
55f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
56f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
57915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public ColorValueView(Context ctx, AttributeSet attrs) {
58915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        super(ctx, attrs);
59915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
60915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float mDpToPix = metrics.density;
61915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        dotRadus = DOT_SIZE * mDpToPix;
62915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mBorder = BORDER_SIZE * mDpToPix;
63915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
64915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mBarPaint1 = new Paint();
65915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
66915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotPaint = new Paint();
67915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
68915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotPaint.setStyle(Paint.Style.FILL);
69915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
70915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
71915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mBarPaint1.setStyle(Paint.Style.FILL);
72915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
73915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mLinePaint1 = new Paint();
74915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mLinePaint1.setColor(Color.GRAY);
75915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mLinePaint2 = new Paint();
76915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
77915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mLinePaint2.setColor(mSliderColor);
78915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mLinePaint2.setStrokeWidth(4);
79915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
80915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
81915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public boolean onDown(MotionEvent e) {
82915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        return true;
83915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
84915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
85915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public boolean onTouchEvent(MotionEvent event) {
86915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float ox = mDotX;
87915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float oy = mDotY;
88915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
89915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float x = event.getX();
90915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float y = event.getY();
91915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
92915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotY = y;
93915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
94915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        if (mDotY < mBorder) {
95915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mDotY = mBorder;
96915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
97915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
98915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        if (mDotY > mHeight - mBorder) {
99915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mDotY = mHeight - mBorder;
100915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
101915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mHSVO[2] = (mDotY - mBorder) / (mHeight - mBorder * 2);
102915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        notifyColorListeners(mHSVO);
103915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setupButton();
104915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        invalidate((int) (ox - dotRadus), (int) (oy - dotRadus), (int) (ox + dotRadus),
105915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                (int) (oy + dotRadus));
106915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        invalidate((int) (mDotX - dotRadus), (int) (mDotY - dotRadus), (int) (mDotX + dotRadus),
107915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                (int) (mDotY + dotRadus));
108915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
109915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        return true;
110915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
111915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
112915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private void setupButton() {
113915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float pos = mHSVO[2] * (mHeight - mBorder * 2);
114915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotY = pos + mBorder;
115915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
116915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int[] colors3 = new int[] {
117915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                mSliderColor, mSliderColor, 0x66000000, 0 };
118915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        RadialGradient g = new RadialGradient(mDotX, mDotY, dotRadus, colors3, new float[] {
119915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        0, .3f, .31f, 1 }, Shader.TileMode.CLAMP);
120915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotPaint.setShader(g);
121915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
122915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
123915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    @Override
124915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
125915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mWidth = w;
126915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mHeight = h;
127915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mDotX = mWidth / 2;
128915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        updatePaint();
129915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setupButton();
130915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
131915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
132915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private void updatePaint() {
133915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float[] hsv = new float[] {
134915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                mHSVO[0], mHSVO[1], 0f };
135915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int color1 = Color.HSVToColor(hsv);
136915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        hsv[2] = 1;
137915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int color2 = Color.HSVToColor(hsv);
138915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
139915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        Shader sg = new LinearGradient(mBorder, mBorder, mBorder, mHeight - mBorder, color1, color2,
140915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                Shader.TileMode.CLAMP);
141915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mBarPaint1.setShader(sg);
142915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
143915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
144915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    @Override
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    protected void onDraw(Canvas canvas) {
146915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        super.onDraw(canvas);
147915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        canvas.drawColor(mBgcolor);
148915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1);
149915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        canvas.drawLine(mDotX, mDotY, mDotX, mHeight - mBorder, mLinePaint2);
150915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        canvas.drawLine(mDotX, mBorder, mDotX, mDotY, mLinePaint1);
151a900c0a7b4f3fd4797290428a97c48066a2689ebIan Rogers        if (!Float.isNaN(mDotX)) {
152915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            canvas.drawCircle(mDotX, mDotY, dotRadus, mDotPaint);
153915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
154915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
155915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
156915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    @Override
157915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void setColor(float[] hsvo) {
158915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length);
159915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
160915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float oy = mDotY;
161915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        updatePaint();
162915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setupButton();
163915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        invalidate();
164915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
165915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
166915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
167915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void notifyColorListeners(float[] hsv) {
168915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        for (ColorListener l : mColorListeners) {
169915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            l.setColor(hsv);
170915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
171915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
172915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
173915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void addColorListener(ColorListener l) {
174915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mColorListeners.add(l);
175915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
176915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
177915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void removeColorListener(ColorListener l) {
178915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mColorListeners.remove(l);
179915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
180915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford}
181