com.pixby.texo.EditTools.ColorTool.java Source code

Java tutorial

Introduction

Here is the source code for com.pixby.texo.EditTools.ColorTool.java

Source

package com.pixby.texo.EditTools;

/*
 * Copyright (c) 2016 David Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.graphics.ColorUtils;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.pixby.texo.EditTools.colormixer.AlphaSeekBar;
import com.pixby.texo.EditTools.colormixer.HueSeekBar;
import com.pixby.texo.R;

public class ColorTool extends ToolFragment {

    private int mInitialColor;
    private int mInitialAlpha;

    private final ArrayMap<Integer, Integer> mSampleColors = new ArrayMap<>();

    private View mSwatchWhite;
    private View mSwatchGrey;
    private View mSwatchBlack;
    private View mSwatchShade1;
    private View mSwatchShade2;
    private View mSwatchTint1;
    private View mSwatchTint2;

    private HueSeekBar mHueSeekBar;
    private AlphaSeekBar mAlphaSeekBar;

    public static ColorTool newInstance() {
        ColorTool f = new ColorTool();
        f.mToolViewId = R.layout.color_tool;
        return f;
    }

    @Override
    public void onStart() {
        super.onStart();
        init();
    }

    private void init() {
        setInitialColor(getWatermark().getColor());
        int progress = colorToProgress(getInitialColor());
        mHueSeekBar.setProgress(progress);
        setInitialAlpha(getWatermark().getAlpha());
        mAlphaSeekBar.setProgress(getInitialAlpha());
    }

    private void setInitialColor(@ColorInt int color) {
        mInitialColor = color;
    }

    private @ColorInt int getInitialColor() {
        return mInitialColor;
    }

    private void setInitialAlpha(int alpha) {
        mInitialAlpha = alpha;
    }

    private int getInitialAlpha() {
        return mInitialAlpha;
    }

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        mHueSeekBar = (HueSeekBar) v.findViewById(R.id.colorpicker);
        mAlphaSeekBar = (AlphaSeekBar) v.findViewById((R.id.alphapicker));
        mSwatchWhite = v.findViewById(R.id.swatch_white);
        mSwatchGrey = v.findViewById(R.id.swatch_grey);
        mSwatchBlack = v.findViewById(R.id.swatch_black);
        mSwatchShade1 = v.findViewById(R.id.swatch_shade1);
        mSwatchShade2 = v.findViewById(R.id.swatch_shade2);
        mSwatchTint1 = v.findViewById(R.id.swatch_tint1);
        mSwatchTint2 = v.findViewById(R.id.swatch_tint2);

        ImageView okButton = (ImageButton) v.findViewById(R.id.save_button);
        ImageButton cancelButton = (ImageButton) v.findViewById(R.id.cancel_button);

        View.OnClickListener swatchClickHandler = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int color = mSampleColors.get(v.getId());
                setColors(color);
                // if this is one of the preset color buttons set up swatches for this color
                if (isPresetColor(color)) {
                    setSwatchColors(color);
                }
            }
        };

        View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                drawBackground(v);
            }
        };

        mSwatchWhite.setOnFocusChangeListener(focusChangeListener);
        mSwatchGrey.setOnFocusChangeListener(focusChangeListener);
        mSwatchBlack.setOnFocusChangeListener(focusChangeListener);
        mSwatchShade1.setOnFocusChangeListener(focusChangeListener);
        mSwatchShade2.setOnFocusChangeListener(focusChangeListener);
        mSwatchTint1.setOnFocusChangeListener(focusChangeListener);
        mSwatchTint2.setOnFocusChangeListener(focusChangeListener);

        setSwatchColors(getInitialColor());

        mSwatchWhite.setOnClickListener(swatchClickHandler);
        mSwatchGrey.setOnClickListener(swatchClickHandler);
        mSwatchBlack.setOnClickListener(swatchClickHandler);
        mSwatchShade1.setOnClickListener(swatchClickHandler);
        mSwatchShade2.setOnClickListener(swatchClickHandler);
        mSwatchTint1.setOnClickListener(swatchClickHandler);
        mSwatchTint2.setOnClickListener(swatchClickHandler);

        SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    if (seekBar instanceof HueSeekBar) {
                        int color = hueToColor(progress);
                        setColors(color);
                        setSwatchColors(color);
                    }
                    if (seekBar instanceof AlphaSeekBar) {
                        setWatermarkAlpha(progress);
                    }
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

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

        mHueSeekBar.setOnSeekBarChangeListener(seekBarListener);
        mAlphaSeekBar.setOnSeekBarChangeListener(seekBarListener);

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListener().onSave(getTag());
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // restore initial state - user cancelled
                setWatermarkColor(getInitialColor());
                setWatermarkAlpha(getInitialAlpha());
                getListener().onCancel(getTag());
            }
        });
    }

    private int hueToColor(float hue) {
        float[] hsv = new float[3];
        hsv[0] = hue;
        hsv[1] = 1.f;
        hsv[2] = 1.f;
        return Color.HSVToColor(hsv);
    }

    // if color is preset color progress goes to 0 else get hue from color and use that as progress value
    private @ColorInt int colorToProgress(@ColorInt int color) {
        int progress;
        if (isPresetColor(color)) {
            progress = 0;
        } else {
            float[] hsv = new float[3];
            Color.colorToHSV(color, hsv);
            progress = (int) hsv[0];
        }
        return progress;
    }

    private boolean isPresetColor(@ColorInt int color) {
        return color == Color.WHITE || color == Color.GRAY || color == Color.BLACK;
    }

    private void setSwatchColors(@ColorInt int color) {
        calcSampleColors(color);

        drawBackground(mSwatchWhite);
        drawBackground(mSwatchGrey);
        drawBackground(mSwatchBlack);
        drawBackground(mSwatchShade1);
        drawBackground(mSwatchShade2);
        drawBackground(mSwatchTint1);
        drawBackground(mSwatchTint2);
    }

    private void drawBackground(View v) {
        int color = mSampleColors.get(v.getId());
        LayerDrawable layerDrawable = (LayerDrawable) v.getBackground();
        if (layerDrawable != null) {

            GradientDrawable shape = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.color_fill);
            shape.setColorFilter(color, PorterDuff.Mode.MULTIPLY);

            shape = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.color_focus);
            int alpha = v.hasFocus() ? 255 : 0;
            shape.setAlpha(alpha);
        }
    }

    private void calcSampleColors(@ColorInt int color) {
        mSampleColors.clear();

        mSampleColors.put(R.id.swatch_white, Color.WHITE);
        mSampleColors.put(R.id.swatch_grey, Color.GRAY);
        mSampleColors.put(R.id.swatch_black, Color.BLACK);

        // if color is black create lighter greys
        if (color == Color.BLACK) {
            mSampleColors.put(R.id.swatch_shade1, getColorFromResource(R.color.shade_grey1));
            mSampleColors.put(R.id.swatch_shade2, getColorFromResource(R.color.shade_grey2));
            mSampleColors.put(R.id.swatch_tint1, getColorFromResource(R.color.tint_grey1));
            mSampleColors.put(R.id.swatch_tint2, getColorFromResource(R.color.tint_grey2));
        } else {
            mSampleColors.put(R.id.swatch_shade1, createTintOrShade(color, 0.25f));
            mSampleColors.put(R.id.swatch_shade2, createTintOrShade(color, 0.33f));
            mSampleColors.put(R.id.swatch_tint1, createTintOrShade(color, 0.66f));
            mSampleColors.put(R.id.swatch_tint2, createTintOrShade(color, 0.75f));
        }
    }

    @SuppressWarnings("deprecation")
    private @ColorInt int getColorFromResource(int id) {
        @ColorInt
        int color;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            color = getResources().getColor(id, null);
        } else {
            color = getResources().getColor(id);
        }
        return color;
    }

    // factor > .5 creates tint, < .5 creates shade
    private @ColorInt int createTintOrShade(@ColorInt int color, float factor) {
        float[] hsl = new float[3];
        ColorUtils.colorToHSL(color, hsl);
        hsl[2] *= factor;
        return ColorUtils.HSLToColor(hsl);
    }

    private void setColors(int color) {
        setWatermarkColor(color);
        mAlphaSeekBar.setColor(color);
        setWatermarkAlpha(mAlphaSeekBar.getProgress());
    }

    private void setWatermarkColor(@ColorInt int color) {
        getWatermark().setColor(color);
        getEditView().invalidate();
    }

    private void setWatermarkAlpha(int alpha) {
        getWatermark().setAlpha(alpha);
        getEditView().invalidate();
    }
}