// Path: libAppThemeHelper/src/main/java/com/kabouzeid/appthemehelper/util/ATHUtil.java
// public final class ATHUtil {
// 
//     public static boolean isWindowBackgroundDark(Context context) {
//         return !ColorUtil.isColorLight(ATHUtil.resolveColor(context, android.R.attr.windowBackground));
//     }
// 
//     public static int resolveColor(Context context, @AttrRes int attr) {
//         return resolveColor(context, attr, 0);
//     }
// 
//     public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
//         TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
// 
//         try {
//             return a.getColor(0, fallback);
// 
//         } finally {
//             a.recycle();
//         }
//     }
// 
//     public static boolean isInClassPath(@NonNull String clsName) {
//         try {
//             return inClassPath(clsName) != null;
//         } catch (Throwable t) {
//             return false;
//         }
//     }
// 
//     public static Class<?> inClassPath(@NonNull String clsName) {
//         try {
//             return Class.forName(clsName);
//         } catch (Throwable t) {
//             throw new IllegalStateException(String.format("%s is not in your class path! You must include the associated library.", clsName));
//         }
//     }
// 
//     private ATHUtil() {
//     }
// }
// 
// Path: libAppThemeHelper/src/main/java/com/kabouzeid/appthemehelper/util/ColorUtil.java
// public final class ColorUtil {
// 
//     public static int stripAlpha(@ColorInt int color) {
//         return 0xff000000 | color;
//     }
// 
//     @ColorInt
//     public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
//         if (by == 1f) return color;
//         int alpha = Color.alpha(color);
//         float[] hsv = new float[3];
//         Color.colorToHSV(color, hsv);
//         hsv[2] *= by; // value component
//         return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
//     }
// 
//     @ColorInt
//     public static int darkenColor(@ColorInt int color) {
//         return shiftColor(color, 0.9f);
//     }
// 
//     @ColorInt
//     public static int lightenColor(@ColorInt int color) {
//         return shiftColor(color, 1.1f);
//     }
// 
//     public static boolean isColorLight(@ColorInt int color) {
//         final double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
//         return darkness < 0.4;
//     }
// 
//     @ColorInt
//     public static int invertColor(@ColorInt int color) {
//         final int r = 255 - Color.red(color);
//         final int g = 255 - Color.green(color);
//         final int b = 255 - Color.blue(color);
//         return Color.argb(Color.alpha(color), r, g, b);
//     }
// 
//     @ColorInt
//     public static int adjustAlpha(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
//         int alpha = Math.round(Color.alpha(color) * factor);
//         int red = Color.red(color);
//         int green = Color.green(color);
//         int blue = Color.blue(color);
//         return Color.argb(alpha, red, green, blue);
//     }
// 
//     @ColorInt
//     public static int withAlpha(@ColorInt int baseColor, @FloatRange(from = 0.0, to = 1.0) float alpha) {
//         int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
//         int rgb = 0x00ffffff & baseColor;
//         return a + rgb;
//     }
// 
//     /**
//      * Taken from CollapsingToolbarLayout's CollapsingTextHelper class.
//      */
//     public static int blendColors(int color1, int color2, @FloatRange(from = 0.0, to = 1.0) float ratio) {
//         final float inverseRatio = 1f - ratio;
//         float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
//         float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
//         float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
//         float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
//         return Color.argb((int) a, (int) r, (int) g, (int) b);
//     }
// 
//     private ColorUtil() {
//     }
// }


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.annotation.AttrRes;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import com.kabouzeid.appthemehelper.R;
import com.kabouzeid.appthemehelper.util.ATHUtil;
import com.kabouzeid.appthemehelper.util.ColorUtil;
package com.kabouzeid.appthemehelper.core;



/**
 * @author Aidan Follestad (afollestad), Karim Abou Zeid (kabouzeid)
 */
public final class ThemeStore implements ThemeStorePrefKeys, ThemeStoreInterface {

    private final Context mContext;
    private final SharedPreferences.Editor mEditor;

    public static ThemeStore editTheme(@NonNull Context context) {
        return new ThemeStore(context);
    }

    @SuppressLint("CommitPrefEdits")
    private ThemeStore(@NonNull Context context) {
        mContext = context;
        mEditor = prefs(context).edit();
    }

    @Override
    public ThemeStore activityTheme(@StyleRes int theme) {
        mEditor.putInt(KEY_ACTIVITY_THEME, theme);
        return this;
    }

    @Override
    public ThemeStore primaryColor(@ColorInt int color) {
        mEditor.putInt(KEY_PRIMARY_COLOR, color);
// Path: libAppThemeHelper/src/main/java/com/kabouzeid/appthemehelper/util/ATHUtil.java
// public final class ATHUtil {
// 
//     public static boolean isWindowBackgroundDark(Context context) {
//         return !ColorUtil.isColorLight(ATHUtil.resolveColor(context, android.R.attr.windowBackground));
//     }
// 
//     public static int resolveColor(Context context, @AttrRes int attr) {
//         return resolveColor(context, attr, 0);
//     }
// 
//     public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
//         TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
// 
//         try {
//             return a.getColor(0, fallback);
// 
//         } finally {
//             a.recycle();
//         }
//     }
// 
//     public static boolean isInClassPath(@NonNull String clsName) {
//         try {
//             return inClassPath(clsName) != null;
//         } catch (Throwable t) {
//             return false;
//         }
//     }
// 
//     public static Class<?> inClassPath(@NonNull String clsName) {
//         try {
//             return Class.forName(clsName);
//         } catch (Throwable t) {
//             throw new IllegalStateException(String.format("%s is not in your class path! You must include the associated library.", clsName));
//         }
//     }
// 
//     private ATHUtil() {
//     }
// }
// 
// Path: libAppThemeHelper/src/main/java/com/kabouzeid/appthemehelper/util/ColorUtil.java
// public final class ColorUtil {
// 
//     public static int stripAlpha(@ColorInt int color) {
//         return 0xff000000 | color;
//     }
// 
//     @ColorInt
//     public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
//         if (by == 1f) return color;
//         int alpha = Color.alpha(color);
//         float[] hsv = new float[3];
//         Color.colorToHSV(color, hsv);
//         hsv[2] *= by; // value component
//         return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
//     }
// 
//     @ColorInt
//     public static int darkenColor(@ColorInt int color) {
//         return shiftColor(color, 0.9f);
//     }
// 
//     @ColorInt
//     public static int lightenColor(@ColorInt int color) {
//         return shiftColor(color, 1.1f);
//     }
// 
//     public static boolean isColorLight(@ColorInt int color) {
//         final double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
//         return darkness < 0.4;
//     }
// 
//     @ColorInt
//     public static int invertColor(@ColorInt int color) {
//         final int r = 255 - Color.red(color);
//         final int g = 255 - Color.green(color);
//         final int b = 255 - Color.blue(color);
//         return Color.argb(Color.alpha(color), r, g, b);
//     }
// 
//     @ColorInt
//     public static int adjustAlpha(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
//         int alpha = Math.round(Color.alpha(color) * factor);
//         int red = Color.red(color);
//         int green = Color.green(color);
//         int blue = Color.blue(color);
//         return Color.argb(alpha, red, green, blue);
//     }
// 
//     @ColorInt
//     public static int withAlpha(@ColorInt int baseColor, @FloatRange(from = 0.0, to = 1.0) float alpha) {
//         int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
//         int rgb = 0x00ffffff & baseColor;
//         return a + rgb;
//     }
// 
//     /**
//      * Taken from CollapsingToolbarLayout's CollapsingTextHelper class.
//      */
//     public static int blendColors(int color1, int color2, @FloatRange(from = 0.0, to = 1.0) float ratio) {
//         final float inverseRatio = 1f - ratio;
//         float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
//         float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
//         float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
//         float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
//         return Color.argb((int) a, (int) r, (int) g, (int) b);
//     }
// 
//     private ColorUtil() {
//     }
// }


// Path: libAppThemeHelper/src/main/java/com/kabouzeid/appthemehelper/core/ThemeStore.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.annotation.AttrRes;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import com.kabouzeid.appthemehelper.R;
import com.kabouzeid.appthemehelper.util.ATHUtil;
import com.kabouzeid.appthemehelper.util.ColorUtil;

package com.kabouzeid.appthemehelper.core;



/**
 * @author Aidan Follestad (afollestad), Karim Abou Zeid (kabouzeid)
 */
public final class ThemeStore implements ThemeStorePrefKeys, ThemeStoreInterface {

    private final Context mContext;
    private final SharedPreferences.Editor mEditor;

    public static ThemeStore editTheme(@NonNull Context context) {
        return new ThemeStore(context);
    }

    @SuppressLint("CommitPrefEdits")
    private ThemeStore(@NonNull Context context) {
        mContext = context;
        mEditor = prefs(context).edit();
    }

    @Override
    public ThemeStore activityTheme(@StyleRes int theme) {
        mEditor.putInt(KEY_ACTIVITY_THEME, theme);
        return this;
    }

    @Override
    public ThemeStore primaryColor(@ColorInt int color) {
        mEditor.putInt(KEY_PRIMARY_COLOR, color);
        if (autoGeneratePrimaryDark(mContext)) primaryColorDark(ColorUtil.darkenColor(color));