1 package com.android.gallery3d.filtershow.controller;
3 import android.app.ActionBar.LayoutParams;
4 import android.content.Context;
5 import android.content.res.Resources;
6 import android.graphics.Color;
7 import android.graphics.drawable.GradientDrawable;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12 import android.widget.LinearLayout;
14 import com.android.gallery3d.R;
15 import com.android.gallery3d.filtershow.colorpicker.ColorListener;
16 import com.android.gallery3d.filtershow.colorpicker.ColorPickerDialog;
17 import com.android.gallery3d.filtershow.editors.Editor;
19 import java.util.Arrays;
20 import java.util.Vector;
22 public class ColorChooser implements Control {
23 private final String LOGTAG = "StyleChooser";
24 protected ParameterColor mParameter;
25 protected LinearLayout mLinearLayout;
26 protected Editor mEditor;
27 private View mTopView;
28 private Vector<Button> mIconButton = new Vector<Button>();
29 protected int mLayoutID = R.layout.filtershow_control_color_chooser;
31 private int mTransparent;
32 private int mSelected;
33 private static final int OPACITY_OFFSET = 3;
34 private int[] mButtonsID = {
35 R.id.draw_color_button01,
36 R.id.draw_color_button02,
37 R.id.draw_color_button03,
38 R.id.draw_color_button04,
39 R.id.draw_color_button05,
41 private Button[] mButton = new Button[mButtonsID.length];
43 int mSelectedButton = 0;
46 public void setUp(ViewGroup container, Parameter parameter, Editor editor) {
47 container.removeAllViews();
48 Resources res = container.getContext().getResources();
49 mTransparent = res.getColor(R.color.color_chooser_unslected_border);
50 mSelected = res.getColor(R.color.color_chooser_slected_border);
52 mContext = container.getContext();
53 int iconDim = res.getDimensionPixelSize(R.dimen.draw_style_icon_dim);
54 mParameter = (ParameterColor) parameter;
55 LayoutInflater inflater =
56 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
57 mTopView = inflater.inflate(mLayoutID, container, true);
58 mLinearLayout = (LinearLayout) mTopView.findViewById(R.id.listStyles);
59 mTopView.setVisibility(View.VISIBLE);
62 LayoutParams lp = new LayoutParams(iconDim, iconDim);
63 int [] palette = mParameter.getColorPalette();
64 for (int i = 0; i < mButtonsID.length; i++) {
65 final Button button = (Button) mTopView.findViewById(mButtonsID[i]);
67 float[] hsvo = new float[4];
68 Color.colorToHSV(palette[i], hsvo);
69 hsvo[OPACITY_OFFSET] = (0xFF & (palette[i] >> 24)) / (float) 255;
71 GradientDrawable sd = ((GradientDrawable) button.getBackground());
72 sd.setColor(palette[i]);
73 sd.setStroke(3, (mSelectedButton == i) ? mSelected : mTransparent);
75 final int buttonNo = i;
76 button.setOnClickListener(new View.OnClickListener() {
78 public void onClick(View arg0) {
79 selectColor(arg0, buttonNo);
83 Button button = (Button) mTopView.findViewById(R.id.draw_color_popupbutton);
85 button.setOnClickListener(new View.OnClickListener() {
87 public void onClick(View arg0) {
94 public void setColorSet(int[] basColors) {
95 int []palette = mParameter.getColorPalette();
96 for (int i = 0; i < palette.length; i++) {
97 palette[i] = basColors[i];
98 float[] hsvo = new float[4];
99 Color.colorToHSV(palette[i], hsvo);
100 hsvo[OPACITY_OFFSET] = (0xFF & (palette[i] >> 24)) / (float) 255;
101 mButton[i].setTag(hsvo);
102 GradientDrawable sd = ((GradientDrawable) mButton[i].getBackground());
103 sd.setColor(palette[i]);
108 public int[] getColorSet() {
109 return mParameter.getColorPalette();
112 private void resetBorders() {
113 int []palette = mParameter.getColorPalette();
114 for (int i = 0; i < mButtonsID.length; i++) {
115 final Button button = mButton[i];
116 GradientDrawable sd = ((GradientDrawable) button.getBackground());
117 sd.setColor(palette[i]);
118 sd.setStroke(3, (mSelectedButton == i) ? mSelected : mTransparent);
122 public void selectColor(View button, int buttonNo) {
123 mSelectedButton = buttonNo;
124 float[] hsvo = (float[]) button.getTag();
125 mParameter.setValue(Color.HSVToColor((int) (hsvo[OPACITY_OFFSET] * 255), hsvo));
127 mEditor.commitLocalRepresentation();
131 public View getTopView() {
136 public void setPrameter(Parameter parameter) {
137 mParameter = (ParameterColor) parameter;
142 public void updateUI() {
143 if (mParameter == null) {
148 public void changeSelectedColor(float[] hsvo) {
149 int []palette = mParameter.getColorPalette();
150 int c = Color.HSVToColor((int) (hsvo[3] * 255), hsvo);
151 final Button button = mButton[mSelectedButton];
152 GradientDrawable sd = ((GradientDrawable) button.getBackground());
154 palette[mSelectedButton] = c;
155 mParameter.setValue(Color.HSVToColor((int) (hsvo[OPACITY_OFFSET] * 255), hsvo));
157 mEditor.commitLocalRepresentation();
161 public void showColorPicker() {
162 ColorListener cl = new ColorListener() {
164 public void setColor(float[] hsvo) {
165 changeSelectedColor(hsvo);
168 public void addColorListener(ColorListener l) {
171 ColorPickerDialog cpd = new ColorPickerDialog(mContext, cl);
172 float[] c = (float[]) mButton[mSelectedButton].getTag();
173 cpd.setColor(Arrays.copyOf(c, 4));
174 cpd.setOrigColor(Arrays.copyOf(c, 4));