summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ColorUtils.java
blob: a0de07c5f18d943c3645052def235786c59dd84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.mapbox.mapboxsdk.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.TypedValue;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;

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 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 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));
    }
}