Home | History | Annotate | Download | only in colorpicker
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.filtershow.colorpicker;
     18 
     19 import android.app.Dialog;
     20 import android.content.Context;
     21 import android.graphics.Color;
     22 import android.graphics.drawable.GradientDrawable;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.Button;
     26 
     27 import com.android.gallery3d.R;
     28 
     29 import java.util.ArrayList;
     30 
     31 public class ColorGridDialog extends Dialog {
     32     RGBListener mCallback;
     33     private static final String LOGTAG = "ColorGridDialog";
     34 
     35     public ColorGridDialog(Context context, final RGBListener cl) {
     36         super(context);
     37         mCallback = cl;
     38         setTitle(R.string.color_pick_title);
     39         setContentView(R.layout.filtershow_color_gird);
     40         Button sel = (Button) findViewById(R.id.filtershow_cp_custom);
     41         ArrayList<Button> b = getButtons((ViewGroup) getWindow().getDecorView());
     42         int k = 0;
     43         float[] hsv = new float[3];
     44 
     45         for (Button button : b) {
     46             if (!button.equals(sel)){
     47                 hsv[0] = (k % 5) * 360 / 5;
     48                 hsv[1] = (k / 5) / 3.0f;
     49                 hsv[2] = (k < 5) ? (k / 4f) : 1;
     50                 final int c = (Color.HSVToColor(hsv) & 0x00FFFFFF) | 0xAA000000;
     51                 GradientDrawable sd = ((GradientDrawable) button.getBackground());
     52                 button.setOnClickListener(new View.OnClickListener() {
     53                         @Override
     54                     public void onClick(View v) {
     55                         mCallback.setColor(c);
     56                         dismiss();
     57                     }
     58                 });
     59                 sd.setColor(c);
     60                 k++;
     61             }
     62 
     63         }
     64         sel.setOnClickListener(new View.OnClickListener() {
     65                 @Override
     66             public void onClick(View v) {
     67                 showColorPicker();
     68                 ColorGridDialog.this.dismiss();
     69             }
     70         });
     71     }
     72 
     73     private ArrayList<Button> getButtons(ViewGroup vg) {
     74         ArrayList<Button> list = new ArrayList<Button>();
     75         for (int i = 0; i < vg.getChildCount(); i++) {
     76             View v = vg.getChildAt(i);
     77             if (v instanceof Button) {
     78                 list.add((Button) v);
     79             } else if (v instanceof ViewGroup) {
     80                 list.addAll(getButtons((ViewGroup) v));
     81             }
     82         }
     83         return list;
     84     }
     85 
     86     public void showColorPicker() {
     87         ColorListener cl = new ColorListener() {
     88                 @Override
     89             public void setColor(float[] hsvo) {
     90                 int c = Color.HSVToColor(hsvo) & 0xFFFFFF;
     91                 int alpha = (int) (hsvo[3] * 255);
     92                 c |= alpha << 24;
     93                 mCallback.setColor(c);
     94             }
     95             @Override
     96             public void addColorListener(ColorListener l) {
     97             }
     98         };
     99         ColorPickerDialog cpd = new ColorPickerDialog(this.getContext(), cl);
    100         cpd.show();
    101     }
    102 
    103 }
    104