New color chooser dialog

This commit is contained in:
Karim Abou Zeid 2015-08-25 00:39:39 +02:00
commit d8e94c1c85
12 changed files with 826 additions and 445 deletions

View file

@ -6,6 +6,7 @@ import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
@ -19,6 +20,7 @@ import com.kabouzeid.gramophone.R;
public class ColorUtil {
public static final int PALETTE_BITMAP_SIZE = 100;
@ColorInt
public static int generateColor(Context context, Bitmap bitmap) {
return getColor(context, generatePalette(bitmap));
}
@ -29,6 +31,7 @@ public class ColorUtil {
.generate();
}
@ColorInt
public static int getColor(Context context, @Nullable Palette palette) {
if (palette != null) {
if (palette.getVibrantSwatch() != null) {
@ -48,6 +51,7 @@ public class ColorUtil {
return ColorUtil.resolveColor(context, R.attr.default_bar_color);
}
@ColorInt
public static int resolveColor(@NonNull Context context, @AttrRes int colorAttr) {
TypedArray a = context.obtainStyledAttributes(new int[]{colorAttr});
int resId = a.getColor(0, 0);
@ -55,38 +59,46 @@ public class ColorUtil {
return resId;
}
@ColorInt
public static int getOpaqueColor(@ColorInt int color) {
return color | 0xFF000000;
}
@ColorInt
public static int getColorWithAlpha(float alpha, @ColorInt int baseColor) {
int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
int rgb = 0x00ffffff & baseColor;
return a + rgb;
}
@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 getColorWithAlpha(alpha, Color.HSVToColor(hsv));
}
@SuppressWarnings("ResourceType")
@ColorInt
public static int shiftColorDown(@ColorInt int color) {
int alpha = Color.alpha(color);
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.9f; // value component
return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
return shiftColor(color, 0.9f);
}
@SuppressWarnings("ResourceType")
@ColorInt
public static int shiftColorUp(@ColorInt int color) {
int alpha = Color.alpha(color);
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 1.1f; // value component
return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
return shiftColor(color, 1.1f);
}
@ColorInt
public static int getPrimaryTextColor(final Context context, boolean dark) {
return dark ? ContextCompat.getColor(context, R.color.primary_text_default_material_light) : ContextCompat.getColor(context, R.color.primary_text_default_material_dark);
}
@ColorInt
public static int getSecondaryTextColor(final Context context, boolean dark) {
return dark ? ContextCompat.getColor(context, R.color.secondary_text_default_material_light) : ContextCompat.getColor(context, R.color.secondary_text_default_material_dark);
}
@ -103,14 +115,17 @@ public class ColorUtil {
return getLuminance(backgroundColor) > (255f / 1.3f);
}
@ColorInt
public static int getPrimaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getPrimaryTextColor(context, useDarkTextColorOnBackground(backgroundColor));
}
@ColorInt
public static int getSecondaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getSecondaryTextColor(context, useDarkTextColorOnBackground(backgroundColor));
}
@ColorInt
public static int getFabDrawableColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getPrimaryTextColor(context, useDarkFabDrawableOnBackground(backgroundColor));
}

View file

@ -9,6 +9,7 @@ import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -80,6 +81,13 @@ public class Util {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
public static Drawable resolveDrawable(@NonNull Context context, @AttrRes int drawableAttr) {
TypedArray a = context.obtainStyledAttributes(new int[]{drawableAttr});
Drawable drawable = a.getDrawable(0);
a.recycle();
return drawable;
}
public static Drawable getTintedDrawable(@NonNull Context context, @DrawableRes int drawableResId, int color) {
Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
if (drawable != null) {