<|start_filename|>sample/src/main/java/com/kunzisoft/androidclearchroma/sample/ViewColorActivity.java<|end_filename|>
package com.kunzisoft.androidclearchroma.sample;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.ColorInt;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.kunzisoft.androidclearchroma.ChromaUtil;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.view.ChromaColorView;

/**
 * An activity that simply displays the color view.
 */
public class ViewColorActivity extends AppCompatActivity implements OnColorChangedListener {

    private static final String SAVED_COLOR = "SAVED_COLOR";
    private @ColorInt int initialColor = Color.GREEN;

    private ActionBar toolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_color_view);

        toolbar = getSupportActionBar();
        if (toolbar != null) {
            toolbar.setDisplayHomeAsUpEnabled(true);
        }

        if (savedInstanceState != null) {
            initialColor = savedInstanceState.getInt(SAVED_COLOR, initialColor);
        }

        ChromaColorView chromaColorView = findViewById(R.id.chroma_color_view);
        chromaColorView.setOnColorChangedListener(this);
        chromaColorView.setCurrentColor(initialColor);
        // Set in layout chromaColorView.setIndicatorMode(IndicatorMode.DECIMAL);
        // Set in layout chromaColorView.setColorMode(ColorMode.CMYK255);
        onColorChanged(initialColor);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVED_COLOR, initialColor);
    }

    @Override
    public void onColorChanged(@ColorInt int color) {
        initialColor = color;
        toolbar.setBackgroundDrawable(new ColorDrawable(color));
        Utility.updatetatusBar(this, color);
        toolbar.setTitle(ChromaUtil.getFormattedColorString(color, false));
    }
}


<|start_filename|>sample/src/main/java/com/kunzisoft/androidclearchroma/sample/Utility.java<|end_filename|>
package com.kunzisoft.androidclearchroma.sample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;

public class Utility {

    public static void updatetatusBar(Activity activity, int newColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(darkenColor(newColor));
        }
    }

    private static int darkenColor(int color) {
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] *= 0.8f;
        return Color.HSVToColor(hsv);
    }

}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/HSV.java<|end_filename|>
package com.kunzisoft.androidclearchroma.colormode.mode;

import android.graphics.Color;

import com.kunzisoft.androidclearchroma.R;
import com.kunzisoft.androidclearchroma.colormode.Channel;

import java.util.ArrayList;
import java.util.List;

/**
 * Class for managed Hue-Saturation-Value color mode
 * @author <NAME>
 */
public class HSV implements AbstractColorMode {

    private float[] colorToHSV(int color) {
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        return hsv;
    }

    @Override
    public List<Channel> getChannels() {
        List<Channel> list = new ArrayList<>();

        list.add(new Channel(R.string.channel_hue, 0, 360, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) colorToHSV(color)[0];
            }
        }));

        list.add(new Channel(R.string.channel_saturation, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) (colorToHSV(color)[1] * 100);
            }
        }));

        list.add(new Channel(R.string.channel_value, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) (colorToHSV(color)[2] * 100);
            }
        }));

        return list;
    }

    @Override
    public int evaluateColor(List<Channel> channels) {
        return Color.HSVToColor(new float[]{
                ((float) channels.get(0).getProgress()),
                ((float) channels.get(1).getProgress()) / 100,
                ((float) channels.get(2).getProgress()) / 100
        });
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/listener/OnColorSelectedListener.java<|end_filename|>
package com.kunzisoft.androidclearchroma.listener;

import androidx.annotation.ColorInt;

/**
 * Callback listener for color selected
 * @author JJamet
 */
public interface OnColorSelectedListener {

    /**
     * Called when positive button has been clicked.
     * @param color int: The color that was clicked.
     */
    void onPositiveButtonClick(@ColorInt int color);

    /**
     * Called when negative button has been clicked.
     * @param color int: The color that was clicked.
     */
    void onNegativeButtonClick(@ColorInt int color);
}


<|start_filename|>sample/src/main/java/com/kunzisoft/androidclearchroma/sample/MainActivity.java<|end_filename|>
package com.kunzisoft.androidclearchroma.sample;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.kunzisoft.androidclearchroma.ChromaDialog;
import com.kunzisoft.androidclearchroma.ChromaUtil;
import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener;

/**
 * Main activity of sample project, contains buttons for show fragment, preferences and dialog
 */
public class MainActivity extends AppCompatActivity implements OnColorSelectedListener, OnColorChangedListener {

    private static final String EXTRA_COLOR = "EXTRA_COLOR";
    private static final String EXTRA_COLOR_MODE = "EXTRA_COLOR_MODE";
    private static final String EXTRA_INDICATOR_MODE = "EXTRA_INDICATOR_MODE";

    private Spinner spinner;
    private TextView textView;
    private Toolbar toolbar;
    private FloatingActionButton fab;

    private
    @ColorInt
    int color;
    private ColorMode colorMode;
    private IndicatorMode indicatorMode;

    private static final String TAG_CHROMA_DIALOG = "TAG_CHROMA_DIALOG";
    private ChromaDialog chromaDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Load defaultValues from prefs_v7.xml
        PreferenceManager.setDefaultValues(this, R.xml.prefs_v7, false);

        toolbar = findViewById(R.id.toolbar);
        textView = findViewById(R.id.text_view);
        spinner = findViewById(R.id.spinner);
        fab = findViewById(R.id.fab);

        if (savedInstanceState == null) {
            color = ContextCompat.getColor(this, R.color.colorPrimary);
            colorMode = ColorMode.RGB;
            indicatorMode = IndicatorMode.HEX;
        } else {
            color = savedInstanceState.getInt(EXTRA_COLOR);
            colorMode = ColorMode.getColorModeFromId(savedInstanceState.getInt(EXTRA_COLOR_MODE));
            indicatorMode = IndicatorMode.getIndicatorModeFromId(savedInstanceState.getInt(EXTRA_INDICATOR_MODE));
        }

        setSupportActionBar(toolbar);
        updateTextView(color);
        updateToolbarAndStatusBar(color);
        fab.setBackgroundTintList(ColorStateList.valueOf(color));
        setupSpinner();

        // Event for Floating Action Button
        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chromaDialog == null) {
                    chromaDialog = new ChromaDialog.Builder()
                            .initialColor(color)
                            .colorMode(colorMode)
                            .indicatorMode(indicatorMode) //HEX or DECIMAL;
                            .create();
                }
                chromaDialog.show(getSupportFragmentManager(), TAG_CHROMA_DIALOG);
            }
        });

        // Events for each button
        findViewById(R.id.button_open_fragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, FragmentColorActivity.class));
            }
        });

        // Events for each button
        findViewById(R.id.button_open_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, ViewColorActivity.class));
            }
        });


        findViewById(R.id.prefsButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PreferencesCompatActivity.class));
            }
        });
    }

    private void setupSpinner() {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item);
        for (ColorMode m : ColorMode.values()) {
            adapter.add(m.name());
        }
        adapter.notifyDataSetChanged();

        spinner.setAdapter(adapter);
        spinner.setSelection(colorMode.ordinal());
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                colorMode = ColorMode.values()[position];
                switch(colorMode) {
                    case CMYK:
                    case CMYK255:
                        indicatorMode = IndicatorMode.DECIMAL;
                }
                chromaDialog = null;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void updateTextView(int newColor) {
        textView.setText(ChromaUtil.getFormattedColorString(newColor, colorMode == ColorMode.ARGB));
        textView.setTextColor(newColor);
    }

    private void updateToolbarAndStatusBar(int newColor) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            toolbar.setBackground(new ColorDrawable(newColor));
        }
        else {
            toolbar.setBackgroundDrawable(new ColorDrawable(newColor));
        }
        Utility.updatetatusBar(this, newColor);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(EXTRA_COLOR, color);
        outState.putInt(EXTRA_COLOR_MODE, colorMode.ordinal());
        outState.putInt(EXTRA_INDICATOR_MODE, indicatorMode.ordinal());
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onPositiveButtonClick(@ColorInt int newColor) {
        color = newColor;
        chromaDialog = null;
        fab.setBackgroundTintList(ColorStateList.valueOf(color));
        updateTextView(newColor);
        updateToolbarAndStatusBar(newColor);
    }

    @Override
    public void onNegativeButtonClick(@ColorInt int newColor) {
        updateToolbarAndStatusBar(color);
    }

    @Override
    public void onColorChanged(@ColorInt int newColor) {
        updateToolbarAndStatusBar(newColor);
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/IndicatorMode.java<|end_filename|>
package com.kunzisoft.androidclearchroma;

/**
 * Indicator mode of color, can be Decimal or Hexadecimal
 * @author <NAME>
 */
public enum IndicatorMode {
    DECIMAL(0), HEX(1);

    private int i;

    /**
     * Unique id of IndicatorMode
     * @param id unique ID
     */
    IndicatorMode(int id) {
        i=id;
    }

    /**
     * Get id of Indicator Mode
     * @return ID
     */
    public int getId() {
        return i;
    }

    /**
     * Return mode from ID
     * @param id Unique ID
     * @return Mode
     */
    public static IndicatorMode getIndicatorModeFromId(int id) {
        switch (id) {
            default:
            case 0:
                return DECIMAL;
            case 1:
                return HEX;
        }
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/ShapePreviewPreference.java<|end_filename|>
package com.kunzisoft.androidclearchroma;

/**
 * Enumeration of each form available for preview.
 * @author JJamet
 */
public enum ShapePreviewPreference {
    CIRCLE,
    SQUARE,
    ROUNDED_SQUARE
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/view/ChromaColorView.java<|end_filename|>
package com.kunzisoft.androidclearchroma.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.graphics.drawable.DrawableCompat;

import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.R;
import com.kunzisoft.androidclearchroma.colormode.Channel;
import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Chroma color view to show a color view with channels
 * @author JJamet
 */
public class ChromaColorView extends RelativeLayout {

    private @ColorInt
    int currentColor = Color.GRAY;
    private ColorMode colorMode = ColorMode.RGB;
    private IndicatorMode indicatorMode = IndicatorMode.DECIMAL;

    private AppCompatImageView colorView;
    private List<ChannelView> channelViews = new ArrayList<>();

    private OnColorChangedListener mOnColorChangedListener;

    public ChromaColorView(Context context) {
        super(context);
        init(context, null);
    }

    public ChromaColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ChromaColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ChromaColorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attributeSet) {

        TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.ChromaColorView);
        try {
            currentColor = a.getColor(R.styleable.ChromaPreference_chromaInitialColor, currentColor);

            colorMode = ColorMode.values()[
                    a.getInt(R.styleable.ChromaPreference_chromaColorMode,
                            colorMode.ordinal())];

            indicatorMode = IndicatorMode.values()[
                    a.getInt(R.styleable.ChromaPreference_chromaIndicatorMode,
                            indicatorMode.ordinal())];
        }
        finally {
            a.recycle();
        }

        View root = inflate(context, R.layout.chroma_color, this);

        colorView = root.findViewById(R.id.color_view);
        createView();
    }

    private ChannelView.OnProgressChangedListener seekBarChangeListener = new ChannelView.OnProgressChangedListener() {
        @Override
        public void onProgressChanged() {
            List<Channel> channels = new ArrayList<>();
            for (ChannelView chan : channelViews) {
                channels.add(chan.getChannel());
            }
            currentColor = colorMode.getColorMode().evaluateColor(channels);
            // Listener for color selected in real time
            if (mOnColorChangedListener != null)
                mOnColorChangedListener.onColorChanged(currentColor);

            // Change view for visibility of color
            DrawableCompat.setTint(colorView.getDrawable(), currentColor);
            colorView.invalidate();
        }
    };

    private void createView() {
        DrawableCompat.setTint(colorView.getDrawable(), currentColor);

        ViewGroup channelContainer = findViewById(R.id.channel_container);
        channelContainer.removeAllViews();

        List<Channel> channels = colorMode.getColorMode().getChannels();
        channelViews.clear();
        for (Channel channel : channels) {
            ChannelView channelView = new ChannelView(getContext());

            // Assign the progress from the color
            channel.setProgress(channel.getExtractor().extract(currentColor));
            if(channel.getProgress() < channel.getMin() || channel.getProgress() > channel.getMax()) {
                throw new IllegalArgumentException(
                        "Initial progress " + channel.getProgress()
                                + " for channel: " + channel.getClass().getSimpleName()
                                + " must be between " + channel.getMin() + " and " + channel.getMax());
            }

            channelView.setChannel(channel, indicatorMode);
            channelViews.add(channelView);
        }

        for (ChannelView currentChannelView : channelViews) {
            currentChannelView.registerListener(seekBarChangeListener);
            channelContainer.addView(currentChannelView);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) currentChannelView.getLayoutParams();
            params.topMargin =
                    getResources().getDimensionPixelSize(R.dimen.channel_view_margin_top);
            params.bottomMargin =
                    getResources().getDimensionPixelSize(R.dimen.channel_view_margin_bottom);
        }
    }

    @Override
    public void invalidate() {
        createView();
        super.invalidate();
    }

    public void setOnColorChangedListener(OnColorChangedListener listener) {
        mOnColorChangedListener = listener;
    }

    public int getCurrentColor() {
        return currentColor;
    }

    public void setCurrentColor(int currentColor) {
        this.currentColor = currentColor;
        invalidate();
    }

    public ColorMode getColorMode() {
        return colorMode;
    }

    public void setColorMode(@NonNull ColorMode colorMode) {
        this.colorMode = colorMode;
        invalidate();
    }

    public IndicatorMode getIndicatorMode() {
        return indicatorMode;
    }

    public void setIndicatorMode(@NonNull IndicatorMode indicatorMode) {
        this.indicatorMode = indicatorMode;
        invalidate();
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.java<|end_filename|>
package com.kunzisoft.androidclearchroma;

import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener;

/**
 * An abstract class to make easier to use preferences through PreferencesFragmentCompat. <br >
 * Assign automatically the color in preference with the button of dialog
 * @author JJamet
 */
public abstract class ChromaPreferenceFragmentCompat extends PreferenceFragmentCompat implements OnColorSelectedListener {

    //I also recommend you to use some 3rd-party lib to style PreferenceFragmentCompat
    //because by default v7 prefs don't respect device theme and look ugly, even on v21+
    //theoretically useful links:
    // * http://stackoverflow.com/questions/32070670/preferencefragmentcompat-requires-preferencetheme-to-be-set
    // * https://github.com/consp1racy/android-support-preference

    private static final String TAG_FRAGMENT_DIALOG = "TAG_FRAGMENT_DIALOG";

    private Preference currentPreference;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        ChromaDialog chromaDialog = (ChromaDialog) (getFragmentManager() != null ? getFragmentManager().findFragmentByTag(TAG_FRAGMENT_DIALOG) : null);
        if (chromaDialog != null) {
            String keyPreference = chromaDialog.getKeyPreference();
            if (keyPreference != null)
                currentPreference = getPreferenceManager().findPreference(keyPreference);
            chromaDialog.setOnColorSelectedListener(this);
        }

        return view;
    }

    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        currentPreference = preference;
        // Try if the preference is one of our custom Preferences
        ChromaDialog dialogFragment = null;
        if (preference instanceof ChromaPreferenceCompat) {
            // Create a new instance of ChromaDialog with the key of the related
            // Preference
            dialogFragment = ChromaDialog.newInstance(
                    preference.getKey(),
                    ((ChromaPreferenceCompat) preference).getColor(),
                    ((ChromaPreferenceCompat) preference).getColorMode(),
                    ((ChromaPreferenceCompat) preference).getIndicatorMode());
            dialogFragment.setOnColorSelectedListener(this);
        }

        // If it was one of our custom Preferences, show its dialog
        if (dialogFragment != null && getFragmentManager() != null) {
            dialogFragment.setTargetFragment(this, 0);
            dialogFragment.show(getFragmentManager(), TAG_FRAGMENT_DIALOG);
        }
        // Could not be handled here. Try with the super method.
        else {
            super.onDisplayPreferenceDialog(preference);
        }
    }

    @Override
    public void onPositiveButtonClick(@ColorInt int color) {
        if (currentPreference instanceof ChromaPreferenceCompat) {
            ((ChromaPreferenceCompat) currentPreference).setColor(color);
        }
    }

    @Override
    public void onNegativeButtonClick(@ColorInt int color) {
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/ChromaDialog.java<|end_filename|>
package com.kunzisoft.androidclearchroma;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AlertDialog;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment;
import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener;

import static com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.ARG_COLOR_MODE;
import static com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.ARG_INDICATOR_MODE;
import static com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.ARG_INITIAL_COLOR;

/**
 * Create Dialog who contains a ChromaColorFragment with buttons for confirmation. <br />
 * To link events to OnColorSelectedListener, it is enough to implement the interface in the parent fragment or parent activity of the dialog.
 * @author JJamet - <NAME>
 */
public class ChromaDialog extends DialogFragment {

    private final static String TAG = "ChromaDialog";

    private final static String ARG_KEY = "ARG_KEY";

    private final static int DEFAULT_COLOR = Color.GRAY;
    private final static ColorMode DEFAULT_MODE = ColorMode.RGB;

    private final static String TAG_FRAGMENT_COLORS = "TAG_FRAGMENT_COLORS";

    private OnColorSelectedListener onColorSelectedListener;

    private ChromaColorFragment chromaColorFragment;

    private View rootView;

    /**
     * Build new instance of dialog
     * @param key Can be null, key link to preference, used when dialog is in a preference view
     * @param initialColor Initial color displayed in the dialog
     * @param colorMode Color mode for selection
     * @param indicatorMode Indicator mode
     * @return New instance of ChromaDialog
     */
    public static ChromaDialog newInstance(@Nullable String key, @ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
        ChromaDialog fragment = new ChromaDialog();
        Bundle args = makeArgs(initialColor, colorMode, indicatorMode);
        args.putString(ARG_KEY, key);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * Build new instance of dialog
     * @param initialColor Initial color displayed in the dialog
     * @param colorMode Color mode for selection
     * @param indicatorMode Indicator mode
     * @return New instance of ChromaDialog
     */
    private static ChromaDialog newInstance(@ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
        ChromaDialog fragment = new ChromaDialog();
        fragment.setArguments(makeArgs(initialColor, colorMode, indicatorMode));
        return fragment;
    }

    private static Bundle makeArgs(@ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
        Bundle args = new Bundle();
        args.putInt(ARG_INITIAL_COLOR, initialColor);
        args.putInt(ARG_COLOR_MODE, colorMode.ordinal());
        args.putInt(ARG_INDICATOR_MODE, indicatorMode.ordinal());
        return args;
    }

    /**
     * Builder class for ChromaDialog objects. Provides a convenient way to set the various fields of a Chroma Dialog and generate fragment associated.
     */
    public static class Builder {
        private
        @ColorInt
        int initialColor = DEFAULT_COLOR;
        private ColorMode colorMode = DEFAULT_MODE;
        private IndicatorMode indicatorMode = IndicatorMode.DECIMAL;

        public Builder initialColor(@ColorInt int initialColor) {
            this.initialColor = initialColor;
            return this;
        }

        public Builder colorMode(ColorMode colorMode) {
            this.colorMode = colorMode;
            //TODO
            /*
            IndicatorMode indicatorMode = IndicatorMode.HEX;
            if(colorMode == ColorMode.HSV
                    || colorMode == ColorMode.CMYK
                    || colorMode == ColorMode.HSL) indicatorMode = IndicatorMode.DECIMAL; // cuz HEX is dumb for those
                    */
            return this;
        }

        public Builder indicatorMode(IndicatorMode indicatorMode) {
            this.indicatorMode = indicatorMode;
            return this;
        }

        public ChromaDialog create() {
            return newInstance(initialColor, colorMode, indicatorMode);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        assert getActivity() != null;

        AlertDialog.Builder alertDialogBuilder =  new  AlertDialog.Builder(getActivity());

        rootView = getActivity().getLayoutInflater().inflate(R.layout.color_dialog_fragment, null);

        FragmentManager fragmentManager = getChildFragmentManager();
        chromaColorFragment = (ChromaColorFragment) fragmentManager.findFragmentByTag(TAG_FRAGMENT_COLORS);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (chromaColorFragment == null) {
            chromaColorFragment = ChromaColorFragment.newInstance(getArguments());
            fragmentTransaction.add(R.id.color_dialog_container, chromaColorFragment, TAG_FRAGMENT_COLORS).commit();
        }

        alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final Activity activity = getActivity();
                final Fragment fragment = getTargetFragment();
                if(onColorSelectedListener != null) {
                    onColorSelectedListener.onPositiveButtonClick(chromaColorFragment.getCurrentColor());
                } else if (activity instanceof OnColorSelectedListener) {
                    ((OnColorSelectedListener) activity).onPositiveButtonClick(chromaColorFragment.getCurrentColor());
                } else if (fragment instanceof OnColorSelectedListener) {
                    ((OnColorSelectedListener) fragment).onPositiveButtonClick(chromaColorFragment.getCurrentColor());
                }
                dismiss();
            }
        });

        alertDialogBuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final Activity activity = getActivity();
                final Fragment fragment = getTargetFragment();
                if(onColorSelectedListener != null) {
                    onColorSelectedListener.onNegativeButtonClick(chromaColorFragment.getCurrentColor());
                } else if (activity instanceof OnColorSelectedListener) {
                    ((OnColorSelectedListener) activity).onNegativeButtonClick(chromaColorFragment.getCurrentColor());
                } else if (fragment instanceof OnColorSelectedListener) {
                    ((OnColorSelectedListener) fragment).onNegativeButtonClick(chromaColorFragment.getCurrentColor());
                }
                dismiss();
            }
        });

        alertDialogBuilder.setView(rootView);

        Dialog dialog = alertDialogBuilder.create();
        // request a window without the title
        if (dialog.getWindow() != null) {
            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        }

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                measureLayout((Dialog) dialog);
            }
        });

        return dialog;
    }

    /**
     * Set new dimensions to dialog
     * @param ad dialog
     */
    private void measureLayout(Dialog ad) {
        TypedValue typedValue = new TypedValue();
        getResources().getValue(R.dimen.chroma_dialog_height_multiplier, typedValue, true);
        float heightMultiplier = typedValue.getFloat();
        int height = (int) ((ad.getContext().getResources().getDisplayMetrics().heightPixels) * heightMultiplier);

        getResources().getValue(R.dimen.chroma_dialog_width_multiplier, typedValue, true);
        float widthMultiplier = typedValue.getFloat();
        int width = (int) ((ad.getContext().getResources().getDisplayMetrics().widthPixels) * widthMultiplier);

        if (ad.getWindow() != null)
            ad.getWindow().setLayout(width, height);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    /**
     * Get key associated to preference, or null if key not present
     * @return a String value, or null
     */
    public String getKeyPreference() {
        return getArguments() != null ? getArguments().getString(ARG_KEY) : null;
    }

    /**
     * Get color listener if it was defined by setter else return null
     * @return Interface object who contains events
     */
    public OnColorSelectedListener getOnColorSelectedListener() {
        return onColorSelectedListener;
    }

    /**
     * Defined listener for click on positive and negative button
     * You can implement OnColorSelectedListener in activity or fragment without use this setter for a better usability
     * @param onColorSelectedListener Interface for events
     */
    public void setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) {
        this.onColorSelectedListener = onColorSelectedListener;
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java<|end_filename|>
package com.kunzisoft.androidclearchroma.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.view.ChromaColorView;

/**
 * Fragment used to show color views. The chromaColorFragment displays each color channel according to the chosen mode.
 * @author JJamet
 */
public class ChromaColorFragment extends Fragment {

    private static final String TAG = "ChromaColorFragment";

    public final static String ARG_INITIAL_COLOR = "arg_initial_color";
    public final static String ARG_COLOR_MODE = "arg_color_mode";
    public final static String ARG_INDICATOR_MODE = "arg_indicator_mode";

    private ChromaColorView chromaColorView;

    private @ColorInt int currentColor;
    private ColorMode colorMode;
    private IndicatorMode indicatorMode;

    /**
     * Must be used to create a new instance of the fragment.
     * @param initialColor Color originally viewed
     * @param colorMode Color mode use
     * @param indicatorMode Indicator used
     * @return New ChromaColorFragment
     */
    public static ChromaColorFragment newInstance(@ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
        ChromaColorFragment chromaColorFragment = new ChromaColorFragment();

        Bundle args = new Bundle();
        args.putInt(ARG_INITIAL_COLOR, initialColor);
        args.putInt(ARG_COLOR_MODE, colorMode.ordinal());
        args.putInt(ARG_INDICATOR_MODE, indicatorMode.ordinal());

        chromaColorFragment.setArguments(args);

        return chromaColorFragment;
    }

    /**
     * Must be used to create a new instance of the fragment.
     * @param args Only arguments ARG_INITIAL_COLOR, ARG_COLOR_MODE and ARG_INDICATOR_MODE in bundle will be interpreted
     * @return New ChromaColorFragment
     */
    public static ChromaColorFragment newInstance(Bundle args) {
        ChromaColorFragment chromaColorFragment = new ChromaColorFragment();
        chromaColorFragment.setArguments(args);
        return chromaColorFragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        chromaColorView = new ChromaColorView(getContext());
        chromaColorView.setCurrentColor(currentColor);
        chromaColorView.setColorMode(colorMode);
        chromaColorView.setIndicatorMode(indicatorMode);

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            assignArguments(savedInstanceState);
        } else if (getArguments() != null) {
            assignArguments(getArguments());
        }

        chromaColorView.setClipToPadding(false);

        // Listener for color selected in real time
        final Activity activity = getActivity();
        final Fragment fragment = getTargetFragment();
        if (activity instanceof OnColorChangedListener) {
            chromaColorView.setOnColorChangedListener(((OnColorChangedListener) activity));
        } else if (fragment instanceof OnColorChangedListener) {
            chromaColorView.setOnColorChangedListener(((OnColorChangedListener) fragment));
        }

        chromaColorView.invalidate();

        return chromaColorView;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(ARG_INITIAL_COLOR, chromaColorView.getCurrentColor());
        outState.putInt(ARG_COLOR_MODE, chromaColorView.getColorMode().ordinal());
        outState.putInt(ARG_INDICATOR_MODE, chromaColorView.getIndicatorMode().ordinal());
    }

    private void assignArguments(Bundle args) {
        if (args.containsKey(ARG_INITIAL_COLOR)) {
            currentColor = args.getInt(ARG_INITIAL_COLOR);
            if (chromaColorView != null)
                chromaColorView.setCurrentColor(currentColor);
        }
        if (args.containsKey(ARG_COLOR_MODE)) {
            colorMode = ColorMode.getColorModeFromId(args.getInt(ARG_COLOR_MODE));
            if (chromaColorView != null)
                chromaColorView.setColorMode(colorMode);
        }
        if (args.containsKey(ARG_INDICATOR_MODE)) {
            indicatorMode = IndicatorMode.getIndicatorModeFromId(args.getInt(ARG_INDICATOR_MODE));
            if (chromaColorView != null)
                chromaColorView.setIndicatorMode(indicatorMode);
        }
    }

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);

        assignArguments(args);
    }

    /**
     * Get current color selected with channels
     * @return int: current color selected in fragment
     */
    public int getCurrentColor() {
        return chromaColorView.getCurrentColor();
    }

    /**
     * Get color mode used
     * @return ColorMode: color mode in fragment
     */
    public ColorMode getColorMode() {
        return chromaColorView.getColorMode();
    }

    /**
     * Get indicator mode used
     * @return IndicatorMode : indicator in fragment
     */
    public IndicatorMode getIndicatorMode() {
        return chromaColorView.getIndicatorMode();
    }
}


<|start_filename|>docs/index-files/index-5.html<|end_filename|>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="fr">
<head>
<!-- Generated by javadoc (1.8.0_112-release) on Thu Mar 30 01:48:50 CEST 2017 -->
<title>G-Index</title>
<meta name="date" content="2017-03-30">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">
<script type="text/javascript" src="../script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
    try {
        if (location.href.indexOf('is-external=true') == -1) {
            parent.document.title="G-Index";
        }
    }
    catch(err) {
    }
//-->
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!--   -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!--   -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li>
<li>Class</li>
<li><a href="../overview-tree.html">Tree</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li class="navBarCell1Rev">Index</li>
<li><a href="../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="index-4.html">Prev Letter</a></li>
<li><a href="index-6.html">Next Letter</a></li>
</ul>
<ul class="navList">
<li><a href="../index.html?index-files/index-5.html" target="_top">Frames</a></li>
<li><a href="index-5.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
  allClassesLink = document.getElementById("allclasses_navbar_top");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //-->
</script>
</div>
<a name="skip.navbar.top">
<!--   -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<div class="contentContainer"><a href="index-1.html">A</a>&nbsp;<a href="index-2.html">B</a>&nbsp;<a href="index-3.html">C</a>&nbsp;<a href="index-4.html">E</a>&nbsp;<a href="index-5.html">G</a>&nbsp;<a href="index-6.html">H</a>&nbsp;<a href="index-7.html">I</a>&nbsp;<a href="index-8.html">M</a>&nbsp;<a href="index-9.html">N</a>&nbsp;<a href="index-10.html">O</a>&nbsp;<a href="index-11.html">P</a>&nbsp;<a href="index-12.html">R</a>&nbsp;<a href="index-13.html">S</a>&nbsp;<a href="index-14.html">V</a>&nbsp;<a name="I:G">
<!--   -->
</a>
<h2 class="title">G</h2>
<dl>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/view/ChannelView.html#getChannel--">getChannel()</a></span> - Method in class com.kunzisoft.androidclearchroma.view.<a href="../com/kunzisoft/androidclearchroma/view/ChannelView.html" title="class in com.kunzisoft.androidclearchroma.view">ChannelView</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/AbstractColorMode.html#getChannels--">getChannels()</a></span> - Method in interface com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/AbstractColorMode.html" title="interface in com.kunzisoft.androidclearchroma.colormode.mode">AbstractColorMode</a></dt>
<dd>
<div class="block">Get each channel of color</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/ARGB.html#getChannels--">getChannels()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/ARGB.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">ARGB</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK.html#getChannels--">getChannels()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">CMYK</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/HSL.html#getChannels--">getChannels()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/HSL.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">HSL</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/HSV.html#getChannels--">getChannels()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/HSV.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">HSV</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/RGB.html#getChannels--">getChannels()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/RGB.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">RGB</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#getColor--">getColor()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>
<div class="block">Get current color of preference</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#getColorMode--">getColorMode()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>
<div class="block">Get current color mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html#getColorMode--">getColorMode()</a></span> - Method in enum com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html" title="enum in com.kunzisoft.androidclearchroma.colormode">ColorMode</a></dt>
<dd>
<div class="block">Get color mode object link to mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html#getColorMode--">getColorMode()</a></span> - Method in class com.kunzisoft.androidclearchroma.fragment.<a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html" title="class in com.kunzisoft.androidclearchroma.fragment">ChromaColorFragment</a></dt>
<dd>
<div class="block">Get color mode used</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html#getColorModeFromId-int-">getColorModeFromId(int)</a></span> - Static method in enum com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html" title="enum in com.kunzisoft.androidclearchroma.colormode">ColorMode</a></dt>
<dd>
<div class="block">Retrieves the color mode from id.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html#getCurrentColor--">getCurrentColor()</a></span> - Method in class com.kunzisoft.androidclearchroma.fragment.<a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html" title="class in com.kunzisoft.androidclearchroma.fragment">ChromaColorFragment</a></dt>
<dd>
<div class="block">Get current color selected with channels</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#getExtractor--">getExtractor()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>
<div class="block">Get color extractor</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaUtil.html#getFormattedColorString-int-boolean-">getFormattedColorString(int, boolean)</a></span> - Static method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaUtil.html" title="class in com.kunzisoft.androidclearchroma">ChromaUtil</a></dt>
<dd>
<div class="block">Get the string link to a int color</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html#getId--">getId()</a></span> - Method in enum com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html" title="enum in com.kunzisoft.androidclearchroma.colormode">ColorMode</a></dt>
<dd>
<div class="block">Get unique ID of mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/IndicatorMode.html#getId--">getId()</a></span> - Method in enum com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/IndicatorMode.html" title="enum in com.kunzisoft.androidclearchroma">IndicatorMode</a></dt>
<dd>
<div class="block">Get id of Indicator Mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#getIndicatorMode--">getIndicatorMode()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>
<div class="block">Get current indicator mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html#getIndicatorMode--">getIndicatorMode()</a></span> - Method in class com.kunzisoft.androidclearchroma.fragment.<a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html" title="class in com.kunzisoft.androidclearchroma.fragment">ChromaColorFragment</a></dt>
<dd>
<div class="block">Get indicator mode used</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/IndicatorMode.html#getIndicatorModeFromId-int-">getIndicatorModeFromId(int)</a></span> - Static method in enum com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/IndicatorMode.html" title="enum in com.kunzisoft.androidclearchroma">IndicatorMode</a></dt>
<dd>
<div class="block">Return mode from ID</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html#getKeyPreference--">getKeyPreference()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html" title="class in com.kunzisoft.androidclearchroma">ChromaDialog</a></dt>
<dd>
<div class="block">Get key associated to preference, or null if key not present</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#getMax--">getMax()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>
<div class="block">Return the maximum channel value.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#getMin--">getMin()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>
<div class="block">Return the minimum channel value.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#getNameResourceId--">getNameResourceId()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>
<div class="block">Get resource id of channel <br />
 Used to name the channel of color mode.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html#getOnColorSelectedListener--">getOnColorSelectedListener()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html" title="class in com.kunzisoft.androidclearchroma">ChromaDialog</a></dt>
<dd>
<div class="block">Get color listener if it was defined by setter else return null</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#getProgress--">getProgress()</a></span> - Method in class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>
<div class="block">Get current progress of channel</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#getShapePreviewPreference--">getShapePreviewPreference()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>
<div class="block">Get shape of preview</div>
</dd>
</dl>
<a href="index-1.html">A</a>&nbsp;<a href="index-2.html">B</a>&nbsp;<a href="index-3.html">C</a>&nbsp;<a href="index-4.html">E</a>&nbsp;<a href="index-5.html">G</a>&nbsp;<a href="index-6.html">H</a>&nbsp;<a href="index-7.html">I</a>&nbsp;<a href="index-8.html">M</a>&nbsp;<a href="index-9.html">N</a>&nbsp;<a href="index-10.html">O</a>&nbsp;<a href="index-11.html">P</a>&nbsp;<a href="index-12.html">R</a>&nbsp;<a href="index-13.html">S</a>&nbsp;<a href="index-14.html">V</a>&nbsp;</div>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!--   -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!--   -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li>
<li>Class</li>
<li><a href="../overview-tree.html">Tree</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li class="navBarCell1Rev">Index</li>
<li><a href="../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="index-4.html">Prev Letter</a></li>
<li><a href="index-6.html">Next Letter</a></li>
</ul>
<ul class="navList">
<li><a href="../index.html?index-files/index-5.html" target="_top">Frames</a></li>
<li><a href="index-5.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
  allClassesLink = document.getElementById("allclasses_navbar_bottom");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //-->
</script>
</div>
<a name="skip.navbar.bottom">
<!--   -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
</body>
</html>


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/view/ChannelView.java<|end_filename|>
package com.kunzisoft.androidclearchroma.view;

import android.content.Context;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.R;
import com.kunzisoft.androidclearchroma.colormode.Channel;

/**
 * Channel view to show a color channel
 * @author JJamet, <NAME>
 */
public class ChannelView extends RelativeLayout {

    private TextView label;
    private TextView progressView;
    private SeekBar seekbar;

    private Channel channel;
    private IndicatorMode indicatorMode;

    private OnProgressChangedListener listener;

    public ChannelView(Context context) {
        super(context);
        init(context);
    }

    public ChannelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ChannelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ChannelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View rootView = inflate(context, R.layout.channel_row, this);
        label = rootView.findViewById(R.id.label);
        progressView = rootView.findViewById(R.id.progress_text);
        seekbar = rootView.findViewById(R.id.seekbar);
    }

    private void bindViews() {
        label.setText(getContext().getString(channel.getNameResourceId()));

        setProgress(progressView, channel.getProgress());

        seekbar.setMax(channel.getMax());
        seekbar.setProgress(channel.getProgress());

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                channel.setProgress(progress);
                setProgress(progressView, progress);
                if(listener != null) {
                    listener.onProgressChanged();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

    private void setProgress(TextView view, int progress) {
        view.setText(indicatorMode == IndicatorMode.HEX
                ? Integer.toHexString(progress)
                : String.valueOf(progress));
    }

    /**
     * Attach a change listener to the channel view
     * @param listener Listener to attach
     */
    public void registerListener(OnProgressChangedListener listener) {
        this.listener = listener;
    }

    /**
     * Set a new channel to the view, the IndicatorMode.DECIMAL is used
     * @param channel Channel to assign
     */
    public void setChannel(Channel channel) {
        setChannel(channel, IndicatorMode.DECIMAL);
    }

    /**
     * Set a new channel to the view with a specific IndicatorMode
     * @param channel Channel to assign
     * @param indicatorMode Indicator to use
     */
    public void setChannel(Channel channel, IndicatorMode indicatorMode) {
        this.channel = channel;
        this.indicatorMode = indicatorMode;

        bindViews();
    }

    public Channel getChannel() {
        return channel;
    }

    public IndicatorMode getIndicatorMode() {
        return indicatorMode;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        listener = null;
    }

    public interface OnProgressChangedListener {
        void onProgressChanged();
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/HSL.java<|end_filename|>
package com.kunzisoft.androidclearchroma.colormode.mode;

import android.graphics.Color;
import com.kunzisoft.androidclearchroma.R;
import com.kunzisoft.androidclearchroma.colormode.Channel;

import java.util.ArrayList;
import java.util.List;

/**
 * Class for managed Hue-Saturation-Lightness color mode
 * @author <NAME>
 */
public class HSL implements AbstractColorMode {

//    thx @xpansive!
//    function hsv2hsl(a,b,c){return[a,b*c/((a=(2-b)*c)<1?a:2-a),a/2]}
//    function hsl2hsv(a,b,c){b*=c<.5?c:1-c;return[a,2*b/(c+b),c+b]}

    private float[] color2hsl(int color) {
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        return hsv2hsl(hsv);
    }

    private float[] hsv2hsl(float[] hsv) {
        float a = hsv[0];
        float b = hsv[1];
        float c = hsv[2];

        return new float[] { a, b * c / ((a = (2 - b) * c) < 1 ? a : 2), a/2 };
    }

    private float[] hsl2hsv(float[] hsl) {
        float a = hsl[0];
        float b = hsl[1];
        float c = hsl[2];

        b *= c < 0.5 ? c : 1 - c;
        if(b == 0) {
            b = 0.001f;
        }

        return new float[] { a, 2 * b / (c + b), c + b };
    }

    @Override
    public List<Channel> getChannels() {
        List<Channel> list = new ArrayList<>();

        list.add(new Channel(R.string.channel_hue, 0, 360, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) color2hsl(color)[0];
            }
        }));

        list.add(new Channel(R.string.channel_saturation, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return 100 - (int) (color2hsl(color)[1] * 100);
            }
        }));

        list.add(new Channel(R.string.channel_lightness, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) (color2hsl(color)[2] * 100);
            }
        }));

        return list;
    }

    @Override
    public int evaluateColor(List<Channel> channels) {

        float[] hsv = hsl2hsv(new float[]{
                ((float) channels.get(0).getProgress()),
                ((float) channels.get(1).getProgress()) / 100,
                ((float) channels.get(2).getProgress()) / 100
        });

        return Color.HSVToColor(hsv);
    }
}


<|start_filename|>docs/index-files/index-3.html<|end_filename|>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="fr">
<head>
<!-- Generated by javadoc (1.8.0_112-release) on Thu Mar 30 01:48:50 CEST 2017 -->
<title>C-Index</title>
<meta name="date" content="2017-03-30">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">
<script type="text/javascript" src="../script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
    try {
        if (location.href.indexOf('is-external=true') == -1) {
            parent.document.title="C-Index";
        }
    }
    catch(err) {
    }
//-->
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!--   -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!--   -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li>
<li>Class</li>
<li><a href="../overview-tree.html">Tree</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li class="navBarCell1Rev">Index</li>
<li><a href="../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="index-2.html">Prev Letter</a></li>
<li><a href="index-4.html">Next Letter</a></li>
</ul>
<ul class="navList">
<li><a href="../index.html?index-files/index-3.html" target="_top">Frames</a></li>
<li><a href="index-3.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
  allClassesLink = document.getElementById("allclasses_navbar_top");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //-->
</script>
</div>
<a name="skip.navbar.top">
<!--   -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<div class="contentContainer"><a href="index-1.html">A</a>&nbsp;<a href="index-2.html">B</a>&nbsp;<a href="index-3.html">C</a>&nbsp;<a href="index-4.html">E</a>&nbsp;<a href="index-5.html">G</a>&nbsp;<a href="index-6.html">H</a>&nbsp;<a href="index-7.html">I</a>&nbsp;<a href="index-8.html">M</a>&nbsp;<a href="index-9.html">N</a>&nbsp;<a href="index-10.html">O</a>&nbsp;<a href="index-11.html">P</a>&nbsp;<a href="index-12.html">R</a>&nbsp;<a href="index-13.html">S</a>&nbsp;<a href="index-14.html">V</a>&nbsp;<a name="I:C">
<!--   -->
</a>
<h2 class="title">C</h2>
<dl>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode"><span class="typeNameLink">Channel</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/colormode/package-summary.html">com.kunzisoft.androidclearchroma.colormode</a></dt>
<dd>
<div class="block">Channel for manipulate data of element in color mode</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#Channel-int-int-int-com.kunzisoft.androidclearchroma.colormode.Channel.ColorExtractor-">Channel(int, int, int, Channel.ColorExtractor)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html#Channel-int-int-int-int-com.kunzisoft.androidclearchroma.colormode.Channel.ColorExtractor-">Channel(int, int, int, int, Channel.ColorExtractor)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.colormode.<a href="../com/kunzisoft/androidclearchroma/colormode/Channel.html" title="class in com.kunzisoft.androidclearchroma.colormode">Channel</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/Channel.ColorExtractor.html" title="interface in com.kunzisoft.androidclearchroma.colormode"><span class="typeNameLink">Channel.ColorExtractor</span></a> - Interface in <a href="../com/kunzisoft/androidclearchroma/colormode/package-summary.html">com.kunzisoft.androidclearchroma.colormode</a></dt>
<dd>
<div class="block">Must be implemented for extract color in a color mode</div>
</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/view/ChannelView.html" title="class in com.kunzisoft.androidclearchroma.view"><span class="typeNameLink">ChannelView</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/view/package-summary.html">com.kunzisoft.androidclearchroma.view</a></dt>
<dd>
<div class="block">TODO Must be a fragment, isn't a normal view</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/view/ChannelView.html#ChannelView-com.kunzisoft.androidclearchroma.colormode.Channel-int-com.kunzisoft.androidclearchroma.IndicatorMode-android.content.Context-">ChannelView(Channel, int, IndicatorMode, Context)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.view.<a href="../com/kunzisoft/androidclearchroma/view/ChannelView.html" title="class in com.kunzisoft.androidclearchroma.view">ChannelView</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/view/ChannelView.OnProgressChangedListener.html" title="interface in com.kunzisoft.androidclearchroma.view"><span class="typeNameLink">ChannelView.OnProgressChangedListener</span></a> - Interface in <a href="../com/kunzisoft/androidclearchroma/view/package-summary.html">com.kunzisoft.androidclearchroma.view</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html" title="class in com.kunzisoft.androidclearchroma.fragment"><span class="typeNameLink">ChromaColorFragment</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/fragment/package-summary.html">com.kunzisoft.androidclearchroma.fragment</a></dt>
<dd>
<div class="block">Fragment used to show color views.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html#ChromaColorFragment--">ChromaColorFragment()</a></span> - Constructor for class com.kunzisoft.androidclearchroma.fragment.<a href="../com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.html" title="class in com.kunzisoft.androidclearchroma.fragment">ChromaColorFragment</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html" title="class in com.kunzisoft.androidclearchroma"><span class="typeNameLink">ChromaDialog</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a></dt>
<dd>
<div class="block">Create Dialog who contains a ChromaColorFragment with buttons for confirmation.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html#ChromaDialog--">ChromaDialog()</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaDialog.html" title="class in com.kunzisoft.androidclearchroma">ChromaDialog</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.Builder.html" title="class in com.kunzisoft.androidclearchroma"><span class="typeNameLink">ChromaDialog.Builder</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a></dt>
<dd>
<div class="block">Builder class for ChromaDialog objects.</div>
</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma"><span class="typeNameLink">ChromaPreferenceCompat</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a></dt>
<dd>
<div class="block">Compatibility DialogPreference class that handles preferences, selected color display, and dialog box for selecting this color.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#ChromaPreferenceCompat-android.content.Context-android.util.AttributeSet-int-int-">ChromaPreferenceCompat(Context, AttributeSet, int, int)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#ChromaPreferenceCompat-android.content.Context-android.util.AttributeSet-int-">ChromaPreferenceCompat(Context, AttributeSet, int)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#ChromaPreferenceCompat-android.content.Context-android.util.AttributeSet-">ChromaPreferenceCompat(Context, AttributeSet)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html#ChromaPreferenceCompat-android.content.Context-">ChromaPreferenceCompat(Context)</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceCompat</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.html" title="class in com.kunzisoft.androidclearchroma"><span class="typeNameLink">ChromaPreferenceFragmentCompat</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a></dt>
<dd>
<div class="block">An abstract class to make easier to use preferences through PreferencesFragmentCompat.</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.html#ChromaPreferenceFragmentCompat--">ChromaPreferenceFragmentCompat()</a></span> - Constructor for class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.html" title="class in com.kunzisoft.androidclearchroma">ChromaPreferenceFragmentCompat</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/ChromaUtil.html" title="class in com.kunzisoft.androidclearchroma"><span class="typeNameLink">ChromaUtil</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a></dt>
<dd>
<div class="block">Utility class for pre-configured methods</div>
</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode"><span class="typeNameLink">CMYK</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/colormode/mode/package-summary.html">com.kunzisoft.androidclearchroma.colormode.mode</a></dt>
<dd>
<div class="block">Class for managed Cyan-Magenta-Yellow-Black color mode <br />
 Each value of channel is between 0 and 100</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK.html#CMYK--">CMYK()</a></span> - Constructor for class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">CMYK</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode"><span class="typeNameLink">CMYK255</span></a> - Class in <a href="../com/kunzisoft/androidclearchroma/colormode/mode/package-summary.html">com.kunzisoft.androidclearchroma.colormode.mode</a></dt>
<dd>
<div class="block">Class for managed Cyan-Magenta-Yellow-Black color mode <br />
 Each value of channel is between 0 and 255</div>
</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.html#CMYK255--">CMYK255()</a></span> - Constructor for class com.kunzisoft.androidclearchroma.colormode.mode.<a href="../com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.html" title="class in com.kunzisoft.androidclearchroma.colormode.mode">CMYK255</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.Builder.html#colorMode-com.kunzisoft.androidclearchroma.colormode.ColorMode-">colorMode(ColorMode)</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaDialog.Builder.html" title="class in com.kunzisoft.androidclearchroma">ChromaDialog.Builder</a></dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/ColorMode.html" title="enum in com.kunzisoft.androidclearchroma.colormode"><span class="typeNameLink">ColorMode</span></a> - Enum in <a href="../com/kunzisoft/androidclearchroma/colormode/package-summary.html">com.kunzisoft.androidclearchroma.colormode</a></dt>
<dd>
<div class="block">Enumeration of each color mode</div>
</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/package-summary.html">com.kunzisoft.androidclearchroma</a> - package com.kunzisoft.androidclearchroma</dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/package-summary.html">com.kunzisoft.androidclearchroma.colormode</a> - package com.kunzisoft.androidclearchroma.colormode</dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/colormode/mode/package-summary.html">com.kunzisoft.androidclearchroma.colormode.mode</a> - package com.kunzisoft.androidclearchroma.colormode.mode</dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/fragment/package-summary.html">com.kunzisoft.androidclearchroma.fragment</a> - package com.kunzisoft.androidclearchroma.fragment</dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/listener/package-summary.html">com.kunzisoft.androidclearchroma.listener</a> - package com.kunzisoft.androidclearchroma.listener</dt>
<dd>&nbsp;</dd>
<dt><a href="../com/kunzisoft/androidclearchroma/view/package-summary.html">com.kunzisoft.androidclearchroma.view</a> - package com.kunzisoft.androidclearchroma.view</dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="../com/kunzisoft/androidclearchroma/ChromaDialog.Builder.html#create--">create()</a></span> - Method in class com.kunzisoft.androidclearchroma.<a href="../com/kunzisoft/androidclearchroma/ChromaDialog.Builder.html" title="class in com.kunzisoft.androidclearchroma">ChromaDialog.Builder</a></dt>
<dd>&nbsp;</dd>
</dl>
<a href="index-1.html">A</a>&nbsp;<a href="index-2.html">B</a>&nbsp;<a href="index-3.html">C</a>&nbsp;<a href="index-4.html">E</a>&nbsp;<a href="index-5.html">G</a>&nbsp;<a href="index-6.html">H</a>&nbsp;<a href="index-7.html">I</a>&nbsp;<a href="index-8.html">M</a>&nbsp;<a href="index-9.html">N</a>&nbsp;<a href="index-10.html">O</a>&nbsp;<a href="index-11.html">P</a>&nbsp;<a href="index-12.html">R</a>&nbsp;<a href="index-13.html">S</a>&nbsp;<a href="index-14.html">V</a>&nbsp;</div>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!--   -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!--   -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../overview-summary.html">Overview</a></li>
<li>Package</li>
<li>Class</li>
<li><a href="../overview-tree.html">Tree</a></li>
<li><a href="../deprecated-list.html">Deprecated</a></li>
<li class="navBarCell1Rev">Index</li>
<li><a href="../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="index-2.html">Prev Letter</a></li>
<li><a href="index-4.html">Next Letter</a></li>
</ul>
<ul class="navList">
<li><a href="../index.html?index-files/index-3.html" target="_top">Frames</a></li>
<li><a href="index-3.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
  allClassesLink = document.getElementById("allclasses_navbar_bottom");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //-->
</script>
</div>
<a name="skip.navbar.bottom">
<!--   -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
</body>
</html>


<|start_filename|>sample/src/main/java/com/kunzisoft/androidclearchroma/sample/PreferencesCompatActivity.java<|end_filename|>
package com.kunzisoft.androidclearchroma.sample;

import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MenuItem;

import com.kunzisoft.androidclearchroma.ChromaPreferenceCompat;
import com.kunzisoft.androidclearchroma.ChromaPreferenceFragmentCompat;
import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.colormode.ColorMode;

/**
 * Sample Activity for show chroma preferences with compatibility class
 */
public class PreferencesCompatActivity extends AppCompatActivity {

    private static final String TAG_PREFERENCE_FRAGMENT = "TAG_PREFERENCE_FRAGMENT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Manage fragment who contains list of preferences
        ChromaPreferenceFragmentCompat chromaPreferenceFragmentCompat =
                (ChromaPreferenceFragmentCompat) getSupportFragmentManager().findFragmentByTag(TAG_PREFERENCE_FRAGMENT);

        if(chromaPreferenceFragmentCompat == null)
            chromaPreferenceFragmentCompat = new ColorPreferenceFragmentCompat();

        getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, chromaPreferenceFragmentCompat, TAG_PREFERENCE_FRAGMENT)
                .commit();

        ActionBar toolbar = getSupportActionBar();
        if(toolbar != null) {
            toolbar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Tiny class inherited from ChromaPreferenceFragmentCompat who manage chroma preferences and callback of DialogFragment
     */
    public static class ColorPreferenceFragmentCompat extends ChromaPreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle bundle, String s) {
            addPreferencesFromResource(R.xml.prefs_v7); // load your ChromaPreferenceCompat prefs from xml

            //or add them manually
            ChromaPreferenceCompat pref = new ChromaPreferenceCompat(getContext());
            pref.setIcon(R.drawable.palette);
            pref.setTitle("RGB(added from java)");
            pref.setSummary("Summary ...");
            pref.setColorMode(ColorMode.RGB);
            pref.setIndicatorMode(IndicatorMode.HEX);
            pref.setKey("any_key_you_need");
            getPreferenceScreen().addPreference(pref);
        }
    }
}


<|start_filename|>library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceCompat.java<|end_filename|>
package com.kunzisoft.androidclearchroma;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.preference.DialogPreference;
import androidx.preference.PreferenceViewHolder;

import com.kunzisoft.androidclearchroma.colormode.ColorMode;

/**
 * Compatibility DialogPreference class that handles preferences, selected color display, and dialog box for selecting this color.
 * @author JJamet - <NAME>
 */
public class ChromaPreferenceCompat extends DialogPreference {

    private static final String TAG = "ChromaPreferenceCompat";

    private AppCompatImageView backgroundPreview;
    private AppCompatImageView colorPreview;

    private static final int DEFAULT_COLOR = Color.WHITE;
    private static final ColorMode DEFAULT_COLOR_MODE = ColorMode.RGB;
    private static final IndicatorMode DEFAULT_INDICATOR_MODE = IndicatorMode.DECIMAL;
    private static final ShapePreviewPreference DEFAULT_SHAPE_PREVIEW = ShapePreviewPreference.CIRCLE;
    private static final String COLOR_TAG_SUMMARY = "[color]";

    private int color;
    private ColorMode colorMode;
    private IndicatorMode indicatorMode;
    private ShapePreviewPreference shapePreviewPreference;
    private CharSequence summaryPreference;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ChromaPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    public ChromaPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public ChromaPreferenceCompat(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ChromaPreferenceCompat(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        setWidgetLayoutResource(R.layout.preference_layout);
        loadValuesFromXml(attrs);
        updatePreview();
    }

    private void loadValuesFromXml(AttributeSet attrs) {
        if(attrs == null) {
            color = DEFAULT_COLOR;
            colorMode = DEFAULT_COLOR_MODE;
            indicatorMode = DEFAULT_INDICATOR_MODE;
            shapePreviewPreference = DEFAULT_SHAPE_PREVIEW;
            summaryPreference = COLOR_TAG_SUMMARY;
        }
        else {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ChromaPreference);
            try {
                color = a.getColor(R.styleable.ChromaPreference_chromaInitialColor, DEFAULT_COLOR);

                colorMode = ColorMode.values()[
                        a.getInt(R.styleable.ChromaPreference_chromaColorMode,
                                DEFAULT_COLOR_MODE.ordinal())];

                indicatorMode = IndicatorMode.values()[
                        a.getInt(R.styleable.ChromaPreference_chromaIndicatorMode,
                                DEFAULT_INDICATOR_MODE.ordinal())];

                shapePreviewPreference = ShapePreviewPreference.values()[
                        a.getInt(R.styleable.ChromaPreference_chromaShapePreview,
                                DEFAULT_SHAPE_PREVIEW.ordinal())];

                summaryPreference = getSummary();
            }
            finally {
                a.recycle();
            }
        }
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        if (restorePersistedValue) {
            color = getPersistedInt(color);
        } else {
            color = (int) defaultValue;
            persistInt(color);
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return Color.parseColor(a.getString(index));
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        backgroundPreview = holder.itemView.findViewById(R.id.backgroundPreview);
        colorPreview = holder.itemView.findViewById(R.id.colorPreview);
        updatePreview();

        if(!isEnabled()) {
            colorPreview.getDrawable().mutate().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    @Override
    public void onAttached() {
        super.onAttached();
        updatePreview();
    }

    private Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int widthLight, int heightLight, float radius) {
        Bitmap output = Bitmap.createBitmap(widthLight, heightLight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(output);
        Paint paintColor = new Paint();
        paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);

        RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));

        canvas.drawRoundRect(rectF, radius, radius, paintColor);

        Paint paintImage = new Paint();
        paintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        canvas.drawBitmap(bitmap, -(bitmap.getWidth() - widthLight)/2 , -(bitmap.getHeight() - heightLight)/2, paintImage);

        return output;
    }

    synchronized private void updatePreview() {
        try {
            if(colorPreview != null) {
                int shapeWidth = getContext().getResources()
                        .getDimensionPixelSize(R.dimen.shape_preference_width);
                float radius = shapeWidth/2;

                // Update shape of color preview
                switch (shapePreviewPreference) {
                    default:
                    case CIRCLE:
                        colorPreview.setImageResource(R.drawable.circle);
                        break;
                    case SQUARE:
                        colorPreview.setImageResource(R.drawable.square);
                        radius = 0;
                        break;
                    case ROUNDED_SQUARE:
                        colorPreview.setImageResource(R.drawable.rounded_square);
                        radius = getContext().getResources().getDimension(R.dimen.shape_radius_preference);
                        break;
                }

                // Update color
                colorPreview.getDrawable()
                        .setColorFilter(new PorterDuffColorFilter(
                                color, PorterDuff.Mode.MULTIPLY));

                // Bitmap to crop for background
                Bitmap draughtboard = BitmapFactory.decodeResource(
                        getContext().getResources(), R.drawable.draughtboard);
                draughtboard = getRoundedCroppedBitmap(draughtboard, shapeWidth, shapeWidth, radius);
                backgroundPreview.setImageBitmap(draughtboard);

                colorPreview.invalidate();
                backgroundPreview.invalidate();
            }
            setSummary(summaryPreference);
        }
        catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Cannot update preview: " + e.toString());
        }
    }

    @Override
    protected void onClick() {
        getPreferenceManager().showDialog(this);
    }

    @Override
    protected boolean persistInt(int value) {
        color = value;
        updatePreview();
        return super.persistInt(value);
    }

    /**
     * {@inheritDoc}
     * If [color] is present in summary, it's replaced by string of color
     */
    @Override
    public void setSummary(CharSequence summary) {
        String summaryWithColor = null;
        if(summary != null) {
            summaryWithColor = summary.toString().replace(COLOR_TAG_SUMMARY,
                    ChromaUtil.getFormattedColorString(color, colorMode == ColorMode.ARGB));
        }
        super.setSummary(summaryWithColor);
    }

    /**
     * Get current color of preference
     * @return IntColor
     */
    public int getColor() {
        return color;
    }

    /**
     * Assign color of preference
     * @param color IntColor
     */
    public void setColor(@ColorInt int color) {
        persistInt(color);
        notifyChanged();
    }

    /**
     * Get current color mode
     * @return ColorMode
     */
    public ColorMode getColorMode() {
        return colorMode;
    }

    /**
     * Assign color mode
     * @param colorMode ColorMode
     */
    public void setColorMode(ColorMode colorMode) {
        this.colorMode = colorMode;
        notifyChanged();
    }

    /**
     * Get current indicator mode
     * @return IndicatorMode
     */
    public IndicatorMode getIndicatorMode() {
        return indicatorMode;
    }

    /**
     * Assign indicator mode
     * @param indicatorMode IndicatorMode
     */
    public void setIndicatorMode(IndicatorMode indicatorMode) {
        this.indicatorMode = indicatorMode;
        notifyChanged();
    }

    /**
     * Get shape of preview
     * @return The shape for preview
     */
    public ShapePreviewPreference getShapePreviewPreference() {
        return shapePreviewPreference;
    }

    /**
     * Defined a shape container for preview of color
     * @param shapePreviewPreference Shape wanted for preview
     */
    public void setShapePreviewPreference(ShapePreviewPreference shapePreviewPreference) {
        this.shapePreviewPreference = shapePreviewPreference;
        notifyChanged();
    }
}
