summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/colorpicker/ColorPickerDialog.java
blob: 73a5c907cf14772ae7a072a78dd96fbf6f30aa23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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);
    }

}