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.app.Dialog;
20915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.content.Context;
21915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Color;
22915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.drawable.GradientDrawable;
23915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.view.View;
24915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.view.ViewGroup;
25915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.widget.Button;
26915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
27915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport com.android.gallery3d.R;
28915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
29915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport java.util.ArrayList;
30915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
31915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordpublic class ColorGridDialog extends Dialog {
32915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    RGBListener mCallback;
33915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private static final String LOGTAG = "ColorGridDialog";
34915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
35915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public ColorGridDialog(Context context, final RGBListener cl) {
36915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        super(context);
37915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mCallback = cl;
38915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setTitle(R.string.color_pick_title);
39915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setContentView(R.layout.filtershow_color_gird);
40915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        Button sel = (Button) findViewById(R.id.filtershow_cp_custom);
41915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        ArrayList<Button> b = getButtons((ViewGroup) getWindow().getDecorView());
42915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int k = 0;
43915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float[] hsv = new float[3];
44915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
45915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        for (Button button : b) {
46915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            if (!button.equals(sel)){
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                hsv[0] = (k % 5) * 360 / 5;
48915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                hsv[1] = (k / 5) / 3.0f;
49915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                hsv[2] = (k < 5) ? (k / 4f) : 1;
50915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                final int c = (Color.HSVToColor(hsv) & 0x00FFFFFF) | 0xAA000000;
51915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                GradientDrawable sd = ((GradientDrawable) button.getBackground());
52915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                button.setOnClickListener(new View.OnClickListener() {
53915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                        @Override
54915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                    public void onClick(View v) {
55915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                        mCallback.setColor(c);
56915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                        dismiss();
57915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                    }
58915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                });
59915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                sd.setColor(c);
60915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                k++;
61915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
62915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
63915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
64915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        sel.setOnClickListener(new View.OnClickListener() {
65915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                @Override
66915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            public void onClick(View v) {
67915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                showColorPicker();
68915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                ColorGridDialog.this.dismiss();
69915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
70915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        });
71915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
72915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
73915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private ArrayList<Button> getButtons(ViewGroup vg) {
74915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        ArrayList<Button> list = new ArrayList<Button>();
75915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        for (int i = 0; i < vg.getChildCount(); i++) {
76915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            View v = vg.getChildAt(i);
77915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            if (v instanceof Button) {
78915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                list.add((Button) v);
79915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            } else if (v instanceof ViewGroup) {
80915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                list.addAll(getButtons((ViewGroup) v));
81915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
82915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
83915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        return list;
84915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
85915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
86915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void showColorPicker() {
87915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        ColorListener cl = new ColorListener() {
88915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                @Override
89915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            public void setColor(float[] hsvo) {
90915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                int c = Color.HSVToColor(hsvo) & 0xFFFFFF;
91915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                int alpha = (int) (hsvo[3] * 255);
92915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                c |= alpha << 24;
93915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                mCallback.setColor(c);
94915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
956125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            @Override
966125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            public void addColorListener(ColorListener l) {
976125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            }
98915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        };
99915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        ColorPickerDialog cpd = new ColorPickerDialog(this.getContext(), cl);
100915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        cpd.show();
101915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
102915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
103915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford}
104