summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Kung <kingkung@google.com>2013-02-15 11:48:55 -0800
committerJames Kung <kingkung@google.com>2013-02-15 17:15:01 -0800
commit2cfe25aefb32ed215f1c661d9670baf276fb7776 (patch)
tree5d7187a9fab24cc3ab606ebb9f1e6c750f2856b2 /src
parent4ae643f42d929a968658775a34e833c8d7c39a7a (diff)
downloadandroid_frameworks_opt_colorpicker-2cfe25aefb32ed215f1c661d9670baf276fb7776.tar.gz
android_frameworks_opt_colorpicker-2cfe25aefb32ed215f1c661d9670baf276fb7776.tar.bz2
android_frameworks_opt_colorpicker-2cfe25aefb32ed215f1c661d9670baf276fb7776.zip
Creating colorpicker shared library project.
Change-Id: I858c259e2e3f62a90e2fcbc0d2286f8cf381e4dd
Diffstat (limited to 'src')
-rw-r--r--src/com/android/colorpicker/ColorPickerDialog.java182
-rw-r--r--src/com/android/colorpicker/ColorPickerPalette.java143
-rw-r--r--src/com/android/colorpicker/ColorPickerSwatch.java89
-rw-r--r--src/com/android/colorpicker/HsvColorComparator.java61
4 files changed, 475 insertions, 0 deletions
diff --git a/src/com/android/colorpicker/ColorPickerDialog.java b/src/com/android/colorpicker/ColorPickerDialog.java
new file mode 100644
index 0000000..c55a001
--- /dev/null
+++ b/src/com/android/colorpicker/ColorPickerDialog.java
@@ -0,0 +1,182 @@
+/*
+ * 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.colorpicker;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.ProgressBar;
+
+import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
+
+/**
+ * A dialog which takes in as input an array of colors and creates a palette allowing the user to
+ * select a specific color swatch, which invokes a listener.
+ */
+public class ColorPickerDialog extends DialogFragment implements OnColorSelectedListener {
+
+ public static final int SIZE_LARGE = 1;
+ public static final int SIZE_SMALL = 2;
+
+ protected AlertDialog mAlertDialog;
+
+ private static final String KEY_COLORS = "colors";
+ private static final String KEY_CURRENT_COLOR = "current_color";
+ private static final String KEY_COLUMNS = "columns";
+ private static final String KEY_SIZE = "size";
+
+ protected String mTitle;
+ protected int mTitleResId;
+ protected int[] mColors;
+ protected int mSelectedColor;
+ protected int mColumns;
+ protected int mSize;
+ private ColorPickerPalette mPalette;
+ private ProgressBar mProgress;
+
+ protected OnColorSelectedListener mListener;
+
+ public ColorPickerDialog() {
+ // Empty constructor required for dialog fragments.
+ }
+
+ public ColorPickerDialog(int titleResId, int[] colors, int selectedColor,
+ int columns, int size) {
+ mTitleResId = titleResId;
+ mColors = colors;
+ mSelectedColor = selectedColor;
+ mColumns = columns;
+ mSize = size;
+ }
+
+ public void setOnColorSelectedListener(OnColorSelectedListener listener) {
+ mListener = listener;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setRetainInstance(true);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ final Activity activity = getActivity();
+
+ if (savedInstanceState != null) {
+ mColors = (int[]) savedInstanceState.getSerializable(KEY_COLORS);
+ mSelectedColor = savedInstanceState.getInt(KEY_CURRENT_COLOR);
+ mColumns = savedInstanceState.getInt(KEY_COLUMNS);
+ mSize = savedInstanceState.getInt(KEY_SIZE);
+ }
+
+ View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
+ mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
+ mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker);
+ mPalette.init(mSize, mColumns, this);
+
+ if (mColors != null) {
+ showPalette();
+ }
+
+ if (mTitle == null) {
+ mTitle = activity.getString(mTitleResId);
+ }
+
+ mAlertDialog = new AlertDialog.Builder(activity)
+ .setTitle(mTitle)
+ .setView(view)
+ .create();
+
+ return mAlertDialog;
+ }
+
+ @Override
+ public void onColorSelected(int color) {
+ if (mListener != null) {
+ mListener.onColorSelected(color);
+ }
+
+ if (getTargetFragment() instanceof OnColorSelectedListener) {
+ final OnColorSelectedListener listener =
+ (OnColorSelectedListener) getTargetFragment();
+ listener.onColorSelected(color);
+ }
+
+ mSelectedColor = color;
+ dismiss();
+ }
+
+ public void showPalette() {
+ mProgress.setVisibility(View.GONE);
+ mPalette.setVisibility(View.VISIBLE);
+ mPalette.drawPalette(mColors, mSelectedColor);
+ }
+
+ public void showProgress() {
+ mProgress.setVisibility(View.VISIBLE);
+ mPalette.setVisibility(View.GONE);
+ }
+
+ public void setColors(int[] colors) {
+ if (colors == null) {
+ return;
+ }
+ mColors = colors;
+ showPalette();
+ }
+
+ public void setColors(int[] colors, int selectedColor) {
+ mSelectedColor = selectedColor;
+ setColors(colors);
+ }
+
+ public void setSelectedColor(int color) {
+ if (mSelectedColor != color) {
+ setColors(mColors, color);
+ }
+ }
+
+ public int[] getColors() {
+ return mColors;
+ }
+
+ public int getSelectedColor() {
+ return mSelectedColor;
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putSerializable(KEY_COLORS, mColors);
+ outState.putInt(KEY_CURRENT_COLOR, mSelectedColor);
+ outState.putInt(KEY_COLUMNS, mColumns);
+ outState.putInt(KEY_SIZE, mSize);
+ }
+
+ @Override
+ public void onDestroyView() {
+ if (getDialog() != null && getRetainInstance()) {
+ getDialog().setDismissMessage(null);
+ }
+ super.onDestroyView();
+ }
+}
diff --git a/src/com/android/colorpicker/ColorPickerPalette.java b/src/com/android/colorpicker/ColorPickerPalette.java
new file mode 100644
index 0000000..06621b2
--- /dev/null
+++ b/src/com/android/colorpicker/ColorPickerPalette.java
@@ -0,0 +1,143 @@
+/*
+ * 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.colorpicker;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.TableLayout;
+import android.widget.TableRow;
+
+import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
+
+/**
+ * A color picker custom view which creates an grid of color squares. The number of squares per
+ * row (and the padding between the squares) is determined by the user.
+ */
+public class ColorPickerPalette extends TableLayout {
+
+ public OnColorSelectedListener mOnColorSelectedListener;
+
+ private int mSwatchLength;
+ private int mMarginSize;
+ private int mNumColumns;
+
+ public ColorPickerPalette(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public ColorPickerPalette(Context context) {
+ super(context);
+ }
+
+ /**
+ * Initialize the size, columns, and listener. Size should be a pre-defined size (SIZE_LARGE
+ * or SIZE_SMALL) from ColorPickerDialogFragment.
+ */
+ public void init(int size, int columns, OnColorSelectedListener listener) {
+ mNumColumns = columns;
+ if (size == ColorPickerDialog.SIZE_LARGE) {
+ mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_large);
+ mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_large);
+ } else {
+ mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_small);
+ mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_small);
+ }
+ mOnColorSelectedListener = listener;
+ }
+
+ private TableRow createTableRow() {
+ TableRow row = new TableRow(getContext());
+ ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
+ LayoutParams.WRAP_CONTENT);
+ row.setLayoutParams(params);
+ return row;
+ }
+
+ /**
+ * Adds swatches to table in a serpentine format.
+ */
+ public void drawPalette(int[] colors, int selectedColor) {
+
+ if (colors == null) {
+ return;
+ }
+
+ this.removeAllViews();
+ int rowElements = 0;
+ int rowNumber = 0;
+
+ // Fills the table with swatches based on the array of colors.
+ TableRow row = createTableRow();
+ for (int color : colors) {
+ addSwatchToRow(row, createColorSwatch(color, selectedColor), rowNumber);
+ rowElements++;
+ if (rowElements == mNumColumns) {
+ addView(row);
+ row = createTableRow();
+ rowElements = 0;
+ rowNumber++;
+ }
+ }
+
+ // Create blank views to fill the row if the last row has not been filled.
+ if (rowElements > 0) {
+ while (rowElements != mNumColumns) {
+ addSwatchToRow(row, createBlankSpace(), rowNumber);
+ rowElements++;
+ }
+ addView(row);
+ }
+ }
+
+ /**
+ * Appends a swatch to the end of the row for even-numbered rows (starting with row 0),
+ * to the beginning of a row for odd-numbered rows.
+ */
+ private void addSwatchToRow(TableRow row, View swatch, int rowNumber) {
+ if (rowNumber % 2 == 0) {
+ row.addView(swatch);
+ } else {
+ row.addView(swatch, 0);
+ }
+ }
+
+ /**
+ * Creates a blank space to fill the row.
+ */
+ private ImageView createBlankSpace() {
+ ImageView view = new ImageView(getContext());
+ TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
+ params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
+ view.setLayoutParams(params);
+ return view;
+ }
+
+ /**
+ * Creates a color swatch.
+ */
+ private ColorPickerSwatch createColorSwatch(int color, int selectedColor) {
+ ColorPickerSwatch view = new ColorPickerSwatch(getContext(), color,
+ color == selectedColor, mOnColorSelectedListener);
+ TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
+ params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
+ view.setLayoutParams(params);
+ return view;
+ }
+}
diff --git a/src/com/android/colorpicker/ColorPickerSwatch.java b/src/com/android/colorpicker/ColorPickerSwatch.java
new file mode 100644
index 0000000..875282d
--- /dev/null
+++ b/src/com/android/colorpicker/ColorPickerSwatch.java
@@ -0,0 +1,89 @@
+/*
+ * 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.colorpicker;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.PorterDuff.Mode;
+import android.graphics.drawable.Drawable;
+import android.view.View;
+import android.widget.ImageView;
+
+import com.android.colorpicker.R;
+
+/**
+ * Creates a circular swatch of a specified color. Adds a checkmark if marked as checked.
+ */
+public class ColorPickerSwatch extends ImageView implements View.OnClickListener {
+
+ private int mColor;
+ private Drawable mColorDrawable;
+ private Drawable mCheckmark;
+ private OnColorSelectedListener mOnColorSelectedListener;
+
+ /**
+ * Interface for a callback when a color square is selected.
+ */
+ public interface OnColorSelectedListener {
+
+ /**
+ * Called when a specific color square has been selected.
+ */
+ public void onColorSelected(int color);
+ }
+
+ /**
+ * @param context
+ */
+ public ColorPickerSwatch(Context context) {
+ super(context);
+ }
+
+ public ColorPickerSwatch(Context context, int color, boolean checked,
+ OnColorSelectedListener listener) {
+ super(context);
+ setScaleType(ScaleType.FIT_XY);
+ Resources res = context.getResources();
+ mColorDrawable = res.getDrawable(R.drawable.color_picker_swatch);
+ mCheckmark = res.getDrawable(R.drawable.ic_colorpicker_swatch_selected);
+ mOnColorSelectedListener = listener;
+ setColor(color);
+ setChecked(checked);
+ setOnClickListener(this);
+ }
+
+ protected void setColor(int color) {
+ mColor = color;
+ mColorDrawable.setColorFilter(color, Mode.SRC_ATOP);
+ setBackgroundDrawable(mColorDrawable);
+ }
+
+ private void setChecked(boolean checked) {
+ if (checked) {
+ setImageDrawable(mCheckmark);
+ } else {
+ setImageDrawable(null);
+ }
+ }
+
+ @Override
+ public void onClick(View v) {
+ if (mOnColorSelectedListener != null) {
+ mOnColorSelectedListener.onColorSelected(mColor);
+ }
+ }
+}
diff --git a/src/com/android/colorpicker/HsvColorComparator.java b/src/com/android/colorpicker/HsvColorComparator.java
new file mode 100644
index 0000000..c44648f
--- /dev/null
+++ b/src/com/android/colorpicker/HsvColorComparator.java
@@ -0,0 +1,61 @@
+/*
+ * 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.colorpicker;
+
+import android.graphics.Color;
+
+import java.util.Comparator;
+
+/**
+ * A color comparator which compares based on hue, saturation, and value.
+ */
+public class HsvColorComparator implements Comparator<Integer> {
+
+ @Override
+ public int compare(Integer lhs, Integer rhs) {
+ float[] hsv = new float[3];
+ Color.colorToHSV(lhs, hsv);
+ float hue1 = hsv[0];
+ float sat1 = hsv[1];
+ float val1 = hsv[2];
+
+ float[] hsv2 = new float[3];
+ Color.colorToHSV(rhs, hsv2);
+ float hue2 = hsv2[0];
+ float sat2 = hsv2[1];
+ float val2 = hsv2[2];
+
+ if (hue1 < hue2) {
+ return 1;
+ } else if (hue1 > hue2) {
+ return -1;
+ } else {
+ if (sat1 < sat2) {
+ return 1;
+ } else if (sat1 > sat2) {
+ return -1;
+ } else {
+ if (val1 < val2) {
+ return 1;
+ } else if (val1 > val2) {
+ return -1;
+ }
+ }
+ }
+ return 0;
+ }
+}