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;
216125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.content.res.Configuration;
22915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.Color;
23915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.graphics.drawable.GradientDrawable;
24a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport android.util.DisplayMetrics;
25915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.view.View;
266125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.view.ViewGroup;
274196c7da24f0393887a9eb4f3bbfebfbc122d3b0John Hofordimport android.view.Window;
28a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport android.view.WindowManager;
29915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.widget.Button;
306125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.widget.ImageButton;
31915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport android.widget.ToggleButton;
32915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
33915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hofordimport com.android.gallery3d.R;
346125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport com.android.gallery3d.filtershow.FilterShowActivity;
356125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport com.android.photos.views.GalleryThumbnailView;
36915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
37a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordpublic class ColorPickerDialog extends Dialog   {
38915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    ToggleButton mSelectedButton;
39a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    ColorHueView mColorHueView;
40a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    ColorSVRectView mColorSVRectView;
41a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    ColorOpacityView mColorOpacityView;
426125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford    ColorCompareView mColorCompareView;
436125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford
44a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    float[] mHSVO = new float[4]; // hue=0..360, sat & val opacity = 0...1
45915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
46915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public ColorPickerDialog(Context context, final ColorListener cl) {
47915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        super(context);
48a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        DisplayMetrics metrics = new DisplayMetrics();
49a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        WindowManager wm =  (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
50a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        wm.getDefaultDisplay().getMetrics(metrics);
51a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int height = metrics.heightPixels*8/10;
52a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int width = metrics.widthPixels*8/10;
53a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        getWindow().setLayout(width, height);
544196c7da24f0393887a9eb4f3bbfebfbc122d3b0John Hoford        requestWindowFeature(Window.FEATURE_NO_TITLE);
55915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        setContentView(R.layout.filtershow_color_picker);
56a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorHueView = (ColorHueView) findViewById(R.id.ColorHueView);
57a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorSVRectView = (ColorSVRectView) findViewById(R.id.colorRectView);
58a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorOpacityView = (ColorOpacityView) findViewById(R.id.colorOpacityView);
596125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        mColorCompareView = (ColorCompareView) findViewById(R.id.btnSelect);
606125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford
61915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float[] hsvo = new float[] {
62915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                123, .9f, 1, 1 };
63915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
646125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        ImageButton apply = (ImageButton) findViewById(R.id.applyColorPick);
656125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        ImageButton cancel = (ImageButton) findViewById(R.id.cancelColorPick);
66915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
676125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        apply.setOnClickListener(new View.OnClickListener() {
68a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            @Override
696125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            public void onClick(View view) {
706125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford                cl.setColor(mHSVO);
716125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford                ColorPickerDialog.this.dismiss();
726125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            }
736125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        });
746125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        cancel.setOnClickListener(new View.OnClickListener() {
756125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            @Override
766125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            public void onClick(View view) {
77915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                ColorPickerDialog.this.dismiss();
78915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
79915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        });
806125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        ColorListener [] c = {mColorCompareView,mColorSVRectView,mColorOpacityView,mColorHueView};
816125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        for (int i = 0; i < c.length; i++) {
826125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            c[i].setColor(hsvo);
836125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            for (int j = 0; j < c.length; j++) {
846125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford                if (i==j) {
856125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford                     continue;
866125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford                }
876125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford               c[i].addColorListener(c[j]);
886125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            }
896125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        }
90915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
91a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        ColorListener colorListener = new ColorListener(){
92a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            @Override
93a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            public void setColor(float[] hsvo) {
94a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length);
95a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                int color = Color.HSVToColor(hsvo);
96a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                setButtonColor(mSelectedButton, hsvo);
97a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            }
986125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford
996125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            @Override
1006125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            public void addColorListener(ColorListener l) {
1016125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            }
102a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        };
103915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
1046125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        for (int i = 0; i < c.length; i++) {
1056125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford            c[i].addColorListener(colorListener);
1066125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        }
1076125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        setOnShowListener((FilterShowActivity) context);
1086125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        setOnDismissListener((FilterShowActivity) context);
109915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
110915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
111915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    void toggleClick(ToggleButton v, int[] buttons, boolean isChecked) {
112915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int id = v.getId();
113915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        if (!isChecked) {
114915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            mSelectedButton = null;
115915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            return;
116915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
117915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        for (int i = 0; i < buttons.length; i++) {
118915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            if (id != buttons[i]) {
119915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                ToggleButton b = (ToggleButton) findViewById(buttons[i]);
120915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                b.setChecked(false);
121915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            }
122915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
123915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        mSelectedButton = v;
124915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
125915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float[] hsv = (float[]) v.getTag();
126915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
127a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        ColorHueView csv = (ColorHueView) findViewById(R.id.ColorHueView);
128a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        ColorSVRectView cwv = (ColorSVRectView) findViewById(R.id.colorRectView);
129915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        ColorOpacityView cvv = (ColorOpacityView) findViewById(R.id.colorOpacityView);
130915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        cwv.setColor(hsv);
131915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        cvv.setColor(hsv);
132915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        csv.setColor(hsv);
133915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
134915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
1356125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford    public void setOrigColor(float[] hsvo) {
1366125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        mColorCompareView.setOrigColor(hsvo);
1376125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford    }
138a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
139915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    public void setColor(float[] hsvo) {
140a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorOpacityView.setColor(hsvo);
141a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorHueView.setColor(hsvo);
142a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        mColorSVRectView.setColor(hsvo);
1436125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford        mColorCompareView.setColor(hsvo);
144915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
145915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
146915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    private void setButtonColor(ToggleButton button, float[] hsv) {
147915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        if (button == null) {
148915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford            return;
149915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        }
150915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        int color = Color.HSVToColor(hsv);
151915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        button.setBackgroundColor(color);
152915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        float[] fg = new float[] {
153915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                (hsv[0] + 180) % 360,
154915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                hsv[1],
155915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford                        (hsv[2] > .5f) ? .1f : .9f
156915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        };
157915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        button.setTextColor(Color.HSVToColor(fg));
158915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford        button.setTag(hsv);
159915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford    }
160915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford
161915d60a378b0ca7bbe3af6312ccd2f4b94eb2da1John Hoford}
162