diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java | 183 |
1 files changed, 93 insertions, 90 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java index 190e0b9b8f..d45d647247 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java @@ -19,101 +19,104 @@ import java.util.regex.Pattern; public class ColorUtils { - /** - * Returns a color integer associated as primary color from a theme based on a {@link Context}. - * - * @param context The context used to style the color attributes. - * @return The primary color value of current theme in the form 0xAARRGGBB. - */ - @ColorInt - public static int getPrimaryColor(@NonNull Context context) { - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = context.getTheme(); - theme.resolveAttribute(R.attr.colorPrimary, typedValue, true); - return typedValue.data; - } + /** + * Returns a color integer associated as primary color from a theme based on a {@link Context}. + * + * @param context The context used to style the color attributes. + * @return The primary color value of current theme in the form 0xAARRGGBB. + */ + @ColorInt + public static int getPrimaryColor(@NonNull Context context) { + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = context.getTheme(); + theme.resolveAttribute(R.attr.colorPrimary, typedValue, true); + return typedValue.data; + } - /** - * Returns a color integer associated as primary dark color from a theme based on a {@link Context}. - * - * @param context The context used to style the color attributes. - * @return The primary dark color value of current theme in the form 0xAARRGGBB. - */ - @ColorInt - public static int getPrimaryDarkColor(@NonNull Context context) { - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = context.getTheme(); - theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true); - return typedValue.data; - } + /** + * Returns a color integer associated as primary dark color from a theme based on a {@link Context}. + * + * @param context The context used to style the color attributes. + * @return The primary dark color value of current theme in the form 0xAARRGGBB. + */ + @ColorInt + public static int getPrimaryDarkColor(@NonNull Context context) { + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = context.getTheme(); + theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true); + return typedValue.data; + } - /** - * Returns a color integer associated as accent color from a theme based on a {@link Context}. - * - * @param context The context used to style the color attributes. - * @return The accent color value of current theme in the form 0xAARRGGBB. - */ - @ColorInt - public static int getAccentColor(@NonNull Context context) { - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = context.getTheme(); - theme.resolveAttribute(R.attr.colorAccent, typedValue, true); - return typedValue.data; - } + /** + * Returns a color integer associated as accent color from a theme based on a {@link Context}. + * + * @param context The context used to style the color attributes. + * @return The accent color value of current theme in the form 0xAARRGGBB. + */ + @ColorInt + public static int getAccentColor(@NonNull Context context) { + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = context.getTheme(); + theme.resolveAttribute(R.attr.colorAccent, typedValue, true); + return typedValue.data; + } - /** - * Returns a color state list associated with a theme based on a {@link Context} - * - * @param color The color used for tinting. - * @return A ColorStateList object containing the primary color of a theme - */ - @NonNull - public static ColorStateList getSelector(@ColorInt int color) { - return new ColorStateList( - new int[][]{ - new int[]{android.R.attr.state_pressed}, - new int[]{} - }, - new int[]{ - color, - color - } - ); - } + /** + * Returns a color state list associated with a theme based on a {@link Context} + * + * @param color The color used for tinting. + * @return A ColorStateList object containing the primary color of a theme + */ + @NonNull + public static ColorStateList getSelector(@ColorInt int color) { + return new ColorStateList( + new int[][] { + new int[] {android.R.attr.state_pressed}, + new int[] {} + }, + new int[] { + color, + color + } + ); + } - /** - * Set a color tint list to the {@link Drawable} of an {@link ImageView}. - * - * @param imageView The view to set the default tint list. - * @param tintColor The color to tint. - */ - public static void setTintList(@NonNull ImageView imageView, @ColorInt int tintColor) { - Drawable originalDrawable = imageView.getDrawable(); - Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable); - DrawableCompat.setTintList(wrappedDrawable, getSelector(tintColor)); - } + /** + * Set a color tint list to the {@link Drawable} of an {@link ImageView}. + * + * @param imageView The view to set the default tint list. + * @param tintColor The color to tint. + */ + public static void setTintList(@NonNull ImageView imageView, @ColorInt int tintColor) { + Drawable originalDrawable = imageView.getDrawable(); + Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable); + DrawableCompat.setTintList(wrappedDrawable, getSelector(tintColor)); + } - static int normalizeColorComponent(String value) { - return (int) (Float.parseFloat(value) * 255); - } + static int normalizeColorComponent(String value) { + return (int) (Float.parseFloat(value) * 255); + } - /** - * Convert an rgba string to a Color int. - * - * @param value the String representation of rgba - * @return the int representation of rgba - * @throws ConversionException on illegal input - */ - @ColorInt - public static int rgbaToColor(String value) { - Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,?\\s*(\\d+\\.?\\d*)?\\s*\\)"); - Matcher m = c.matcher(value); - if (m.matches() && m.groupCount() == 3) { - return Color.rgb(normalizeColorComponent(m.group(1)), normalizeColorComponent(m.group(2)), normalizeColorComponent(m.group(3))); - } else if (m.matches() && m.groupCount() == 4) { - return Color.argb(normalizeColorComponent(m.group(4)), normalizeColorComponent(m.group(1)), normalizeColorComponent(m.group(2)), normalizeColorComponent(m.group(3))); - } else { - throw new ConversionException("Not a valid rgb/rgba value"); - } + /** + * Convert an rgba string to a Color int. + * + * @param value the String representation of rgba + * @return the int representation of rgba + * @throws ConversionException on illegal input + */ + @ColorInt + public static int rgbaToColor(String value) { + Pattern c = Pattern.compile("rgba?\\s*\\(\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*,\\s*(\\d+\\.?\\d*)\\s*," + + "?\\s*(\\d+\\.?\\d*)?\\s*\\)"); + Matcher m = c.matcher(value); + if (m.matches() && m.groupCount() == 3) { + return Color.rgb(normalizeColorComponent(m.group(1)), normalizeColorComponent(m.group(2)), + normalizeColorComponent(m.group(3))); + } else if (m.matches() && m.groupCount() == 4) { + return Color.argb(normalizeColorComponent(m.group(4)), normalizeColorComponent(m.group(1)), + normalizeColorComponent(m.group(2)), normalizeColorComponent(m.group(3))); + } else { + throw new ConversionException("Not a valid rgb/rgba value"); } + } }
\ No newline at end of file |