diff options
author | James Kung <kingkung@google.com> | 2013-02-15 11:48:55 -0800 |
---|---|---|
committer | James Kung <kingkung@google.com> | 2013-02-15 17:15:01 -0800 |
commit | 2cfe25aefb32ed215f1c661d9670baf276fb7776 (patch) | |
tree | 5d7187a9fab24cc3ab606ebb9f1e6c750f2856b2 | |
parent | 4ae643f42d929a968658775a34e833c8d7c39a7a (diff) | |
download | android_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
-rw-r--r-- | Android.mk | 24 | ||||
-rw-r--r-- | AndroidManifest.xml | 23 | ||||
-rw-r--r-- | project.properties | 15 | ||||
-rw-r--r-- | res/drawable-hdpi/ic_colorpicker_swatch_selected.png | bin | 0 -> 2414 bytes | |||
-rw-r--r-- | res/drawable-mdpi/ic_colorpicker_swatch_selected.png | bin | 0 -> 1662 bytes | |||
-rw-r--r-- | res/drawable-xhdpi/ic_colorpicker_swatch_selected.png | bin | 0 -> 3241 bytes | |||
-rw-r--r-- | res/drawable/color_picker_swatch.xml | 16 | ||||
-rw-r--r-- | res/layout/color_picker_dialog.xml | 45 | ||||
-rw-r--r-- | res/values-v11/styles.xml | 26 | ||||
-rw-r--r-- | res/values-v14/styles.xml | 27 | ||||
-rw-r--r-- | res/values/dimens.xml | 21 | ||||
-rw-r--r-- | res/values/strings.xml | 20 | ||||
-rw-r--r-- | res/values/styles.xml | 35 | ||||
-rw-r--r-- | src/com/android/colorpicker/ColorPickerDialog.java | 182 | ||||
-rw-r--r-- | src/com/android/colorpicker/ColorPickerPalette.java | 143 | ||||
-rw-r--r-- | src/com/android/colorpicker/ColorPickerSwatch.java | 89 | ||||
-rw-r--r-- | src/com/android/colorpicker/HsvColorComparator.java | 61 |
17 files changed, 727 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk new file mode 100644 index 0000000..279c43a --- /dev/null +++ b/Android.mk @@ -0,0 +1,24 @@ +# 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. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := colorpicker +LOCAL_SDK_VERSION := 15 +LOCAL_SRC_FILES := \ + $(call all-java-files-under, src) \ + $(call all-logtags-files-under, src) +LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res +include $(BUILD_STATIC_JAVA_LIBRARY) diff --git a/AndroidManifest.xml b/AndroidManifest.xml new file mode 100644 index 0000000..e405a15 --- /dev/null +++ b/AndroidManifest.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.colorpicker" + android:versionCode="1" + android:versionName="1.0" > + <uses-sdk + android:minSdkVersion="15" + android:targetSdkVersion="17" /> +</manifest>
\ No newline at end of file diff --git a/project.properties b/project.properties new file mode 100644 index 0000000..36f1594 --- /dev/null +++ b/project.properties @@ -0,0 +1,15 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt + +# Project target. +target=android-15 +android.library=true diff --git a/res/drawable-hdpi/ic_colorpicker_swatch_selected.png b/res/drawable-hdpi/ic_colorpicker_swatch_selected.png Binary files differnew file mode 100644 index 0000000..3cbfe1a --- /dev/null +++ b/res/drawable-hdpi/ic_colorpicker_swatch_selected.png diff --git a/res/drawable-mdpi/ic_colorpicker_swatch_selected.png b/res/drawable-mdpi/ic_colorpicker_swatch_selected.png Binary files differnew file mode 100644 index 0000000..acbdeca --- /dev/null +++ b/res/drawable-mdpi/ic_colorpicker_swatch_selected.png diff --git a/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png b/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png Binary files differnew file mode 100644 index 0000000..812ff2c --- /dev/null +++ b/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png diff --git a/res/drawable/color_picker_swatch.xml b/res/drawable/color_picker_swatch.xml new file mode 100644 index 0000000..db71091 --- /dev/null +++ b/res/drawable/color_picker_swatch.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" />
\ No newline at end of file diff --git a/res/layout/color_picker_dialog.xml b/res/layout/color_picker_dialog.xml new file mode 100644 index 0000000..ea7c111 --- /dev/null +++ b/res/layout/color_picker_dialog.xml @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:gravity="center" > + + <FrameLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:gravity="center" + android:padding="28dp" > + + <ProgressBar + android:id="@android:id/progress" + style="?android:attr/progressBarStyleLarge" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:gravity="center" /> + + <com.android.colorpicker.ColorPickerPalette + android:id="@+id/color_picker" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:gravity="center" + android:visibility="gone" /> + </FrameLayout> +</ScrollView>
\ No newline at end of file diff --git a/res/values-v11/styles.xml b/res/values-v11/styles.xml new file mode 100644 index 0000000..918c716 --- /dev/null +++ b/res/values-v11/styles.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources> + + <!-- + Base application theme for API 11+. This theme completely replaces + AppBaseTheme from res/values/styles.xml on API 11+ devices. + --> + <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> + <!-- API 11 theme customizations can go here. --> + </style> + +</resources>
\ No newline at end of file diff --git a/res/values-v14/styles.xml b/res/values-v14/styles.xml new file mode 100644 index 0000000..a31cec1 --- /dev/null +++ b/res/values-v14/styles.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources> + + <!-- + Base application theme for API 14+. This theme completely replaces + AppBaseTheme from BOTH res/values/styles.xml and + res/values-v11/styles.xml on API 14+ devices. + --> + <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> + <!-- API 14 theme customizations can go here. --> + </style> + +</resources>
\ No newline at end of file diff --git a/res/values/dimens.xml b/res/values/dimens.xml new file mode 100644 index 0000000..fc78f2f --- /dev/null +++ b/res/values/dimens.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources> + <dimen name="color_swatch_large">64dip</dimen> + <dimen name="color_swatch_small">48dip</dimen> + <dimen name="color_swatch_margins_large">8dip</dimen> + <dimen name="color_swatch_margins_small">4dip</dimen> +</resources>
\ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml new file mode 100644 index 0000000..1c833e7 --- /dev/null +++ b/res/values/strings.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources> + + <string name="app_name">Color Picker</string> + +</resources>
\ No newline at end of file diff --git a/res/values/styles.xml b/res/values/styles.xml new file mode 100644 index 0000000..8fbc742 --- /dev/null +++ b/res/values/styles.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources> + + <!-- + Base application theme, dependent on API level. This theme is replaced + by AppBaseTheme from res/values-vXX/styles.xml on newer devices. + --> + <style name="AppBaseTheme" parent="android:Theme.Light"> + <!-- + Theme customizations available in newer API levels can go in + res/values-vXX/styles.xml, while customizations related to + backward-compatibility can go here. + --> + </style> + + <!-- Application theme. --> + <style name="AppTheme" parent="AppBaseTheme"> + <!-- All customizations that are NOT specific to a particular API-level can go here. --> + </style> + +</resources>
\ No newline at end of file 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; + } +} |