/*
* Copyright (C) 2017 Jared Rummler
*
* 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.jaredrummler.android.colorpicker;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import java.util.Arrays;
import java.util.Locale;
/**
* <p>A dialog to pick a color.</p>
*
* <p>The {@link Activity activity} that shows this dialog should implement {@link ColorPickerDialogListener}</p>
*
* <p>Example usage:</p>
*
* <pre>
* ColorPickerDialog.newBuilder().show(activity);
* </pre>
*/
public class ColorPickerDialog extends DialogFragment implements ColorPickerView.OnColorChangedListener, TextWatcher {
public static final int TYPE_CUSTOM = 0;
public static final int TYPE_PRESETS = 1;
/**
* Material design colors used as the default color presets
*/
public static final int[] MATERIAL_COLORS = {
0xFFF44336, // RED 500
0xFFE91E63, // PINK 500
0xFFFF2C93, // LIGHT PINK 500
0xFF9C27B0, // PURPLE 500
0xFF673AB7, // DEEP PURPLE 500
0xFF3F51B5, // INDIGO 500
0xFF2196F3, // BLUE 500
0xFF03A9F4, // LIGHT BLUE 500
0xFF00BCD4, // CYAN 500
0xFF009688, // TEAL 500
0xFF4CAF50, // GREEN 500
0xFF8BC34A, // LIGHT GREEN 500
0xFFCDDC39, // LIME 500
0xFFFFEB3B, // YELLOW 500
0xFFFFC107, // AMBER 500
0xFFFF9800, // ORANGE 500
0xFF795548, // BROWN 500
0xFF607D8B, // BLUE GREY 500
0xFF9E9E9E, // GREY 500
};
static final int ALPHA_THRESHOLD = 165;
private static final String TAG = "ColorPickerDialog";
private static final String ARG_ID = "id";
private static final String ARG_TYPE = "dialogType";
private static final String ARG_COLOR = "color";
private static final String ARG_ALPHA = "alpha";
private static final String ARG_PRESETS = "presets";
private static final String ARG_ALLOW_PRESETS = "allowPresets";
private static final String ARG_ALLOW_CUSTOM = "allowCustom";
private static final String ARG_DIALOG_TITLE = "dialogTitle";
private static final String ARG_SHOW_COLOR_SHADES = "showColorShades";
private static final String ARG_COLOR_SHAPE = "colorShape";
private static final String ARG_PRESETS_BUTTON_TEXT = "presetsButtonText";
private static final String ARG_CUSTOM_BUTTON_TEXT = "customButtonText";
private static final String ARG_SELECTED_BUTTON_TEXT = "selectedButtonText";
ColorPickerDialogListener colorPickerDialogListener;
FrameLayout rootView;
int[] presets;
@ColorInt
int color;
int dialogType;
int dialogId;
boolean showColorShades;
int colorShape;
// -- PRESETS --------------------------
ColorPaletteAdapter adapter;
LinearLayout shadesLayout;
SeekBar transparencySeekBar;
TextView transparencyPercText;
// -- CUSTOM ---------------------------
ColorPickerView colorPicker;
ColorPanelView newColorPanel;
EditText hexEditText;
private final OnTouchListener onPickerTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v,