|
|
@ -26,23 +26,60 @@ public class ThemeUtils { |
|
|
|
|
|
|
|
|
|
|
|
private static final TypedValue sTypedValue = new TypedValue(); |
|
|
|
private static final TypedValue sTypedValue = new TypedValue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the primary color of the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to get the theme from. |
|
|
|
|
|
|
|
* @return the primary color of the current theme. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getPrimaryColor(@NonNull Context context) { |
|
|
|
public static int getPrimaryColor(@NonNull Context context) { |
|
|
|
return getColor(context, R.attr.colorPrimary); |
|
|
|
return getColor(context, R.attr.colorPrimary); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the primary dark color of the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to get the theme from. |
|
|
|
|
|
|
|
* @return the primary dark color of the current theme. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getPrimaryColorDark(@NonNull Context context) { |
|
|
|
public static int getPrimaryColorDark(@NonNull Context context) { |
|
|
|
return getColor(context, R.attr.colorPrimaryDark); |
|
|
|
return getColor(context, R.attr.colorPrimaryDark); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the accent color of the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to get the theme from. |
|
|
|
|
|
|
|
* @return the accent color of the current theme. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getAccentColor(@NonNull Context context) { |
|
|
|
public static int getAccentColor(@NonNull Context context) { |
|
|
|
return getColor(context, R.attr.colorAccent); |
|
|
|
return getColor(context, R.attr.colorAccent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the color of the status bar as set in styles |
|
|
|
|
|
|
|
* for the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to get the theme from. |
|
|
|
|
|
|
|
* @return the status bar color of the current theme. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
@TargetApi(21) |
|
|
|
@TargetApi(21) |
|
|
|
public static int getStatusBarColor(@NonNull Context context) { |
|
|
|
public static int getStatusBarColor(@NonNull Context context) { |
|
|
|
return getColor(context, android.R.attr.statusBarColor); |
|
|
|
return getColor(context, android.R.attr.statusBarColor); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the color attribute from the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to get the theme from. |
|
|
|
|
|
|
|
* @param resource the color attribute resource. |
|
|
|
|
|
|
|
* @return the color for the given attribute. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getColor(@NonNull Context context, @AttrRes int resource) { |
|
|
|
public static int getColor(@NonNull Context context, @AttrRes int resource) { |
|
|
|
TypedArray a = context.obtainStyledAttributes(sTypedValue.data, new int[]{resource}); |
|
|
|
TypedArray a = context.obtainStyledAttributes(sTypedValue.data, new int[]{resource}); |
|
|
|
int color = a.getColor(0, 0); |
|
|
|
int color = a.getColor(0, 0); |
|
|
@ -50,16 +87,37 @@ public class ThemeUtils { |
|
|
|
return color; |
|
|
|
return color; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the icon color for the light theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @return the color of the icon. |
|
|
|
|
|
|
|
*/ |
|
|
|
@ColorInt |
|
|
|
@ColorInt |
|
|
|
public static int getIconLightThemeColor(@NonNull Context context) { |
|
|
|
public static int getIconLightThemeColor(@NonNull Context context) { |
|
|
|
return ContextCompat.getColor(context, R.color.icon_light_theme); |
|
|
|
return ContextCompat.getColor(context, R.color.icon_light_theme); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the icon color for the dark theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @return the color of the icon. |
|
|
|
|
|
|
|
*/ |
|
|
|
@ColorInt |
|
|
|
@ColorInt |
|
|
|
public static int getIconDarkThemeColor(@NonNull Context context) { |
|
|
|
public static int getIconDarkThemeColor(@NonNull Context context) { |
|
|
|
return ContextCompat.getColor(context, R.color.icon_dark_theme); |
|
|
|
return ContextCompat.getColor(context, R.color.icon_dark_theme); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the color icon for the light or |
|
|
|
|
|
|
|
* dark theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @param dark true for the dark theme, |
|
|
|
|
|
|
|
* false for the light theme. |
|
|
|
|
|
|
|
* @return the color of the icon. |
|
|
|
|
|
|
|
*/ |
|
|
|
@ColorInt |
|
|
|
@ColorInt |
|
|
|
public static int getIconThemeColor(@NonNull Context context, boolean dark) { |
|
|
|
public static int getIconThemeColor(@NonNull Context context, boolean dark) { |
|
|
|
return (dark) ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
|
return (dark) ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
@ -91,9 +149,20 @@ public class ThemeUtils { |
|
|
|
return bitmap; |
|
|
|
return bitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the icon with an applied color filter |
|
|
|
|
|
|
|
* for the correct theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @param res the drawable resource to use. |
|
|
|
|
|
|
|
* @param dark true for icon suitable for use with a dark theme, |
|
|
|
|
|
|
|
* false for icon suitable for use with a light theme. |
|
|
|
|
|
|
|
* @return a themed icon. |
|
|
|
|
|
|
|
*/ |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
public static Bitmap getThemedBitmap(@NonNull Context context, @DrawableRes int res, boolean dark) { |
|
|
|
public static Bitmap getThemedBitmap(@NonNull Context context, @DrawableRes int res, boolean dark) { |
|
|
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
|
|
|
|
|
|
|
|
|
Bitmap sourceBitmap = getBitmapFromVectorDrawable(context, res); |
|
|
|
Bitmap sourceBitmap = getBitmapFromVectorDrawable(context, res); |
|
|
|
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), |
|
|
|
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap.getWidth(), sourceBitmap.getHeight(), |
|
|
|
Bitmap.Config.ARGB_8888); |
|
|
|
Bitmap.Config.ARGB_8888); |
|
|
@ -106,19 +175,45 @@ public class ThemeUtils { |
|
|
|
return resultBitmap; |
|
|
|
return resultBitmap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the icon with an applied color filter |
|
|
|
|
|
|
|
* for the correct theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @param res the drawable resource to use. |
|
|
|
|
|
|
|
* @param dark true for icon suitable for use with a dark theme, |
|
|
|
|
|
|
|
* false for icon suitable for use with a light theme. |
|
|
|
|
|
|
|
* @return a themed icon. |
|
|
|
|
|
|
|
*/ |
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
public static Drawable getThemedDrawable(@NonNull Context context, @DrawableRes int res, boolean dark) { |
|
|
|
public static Drawable getThemedDrawable(@NonNull Context context, @DrawableRes int res, boolean dark) { |
|
|
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
|
int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); |
|
|
|
|
|
|
|
|
|
|
|
final Drawable drawable = getVectorDrawable(context, res); |
|
|
|
final Drawable drawable = getVectorDrawable(context, res); |
|
|
|
drawable.mutate(); |
|
|
|
drawable.mutate(); |
|
|
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); |
|
|
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); |
|
|
|
return drawable; |
|
|
|
return drawable; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The text hint color for dark theme or light theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param dark true for a text color suitable for use with a dark theme, |
|
|
|
|
|
|
|
* false for a text color suitable for use with a light theme. |
|
|
|
|
|
|
|
* @return a text color. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getThemedTextHintColor(boolean dark) { |
|
|
|
public static int getThemedTextHintColor(boolean dark) { |
|
|
|
return 0x80ffffff & (dark ? Color.WHITE : Color.BLACK); |
|
|
|
return 0x80ffffff & (dark ? Color.WHITE : Color.BLACK); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets the edit text text color for the current theme. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context to use. |
|
|
|
|
|
|
|
* @return a text color. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@ColorInt |
|
|
|
public static int getTextColor(@NonNull Context context) { |
|
|
|
public static int getTextColor(@NonNull Context context) { |
|
|
|
return getColor(context, android.R.attr.editTextColor); |
|
|
|
return getColor(context, android.R.attr.editTextColor); |
|
|
|
} |
|
|
|
} |
|
|
|