2828import androidx .annotation .FloatRange ;
2929import androidx .annotation .IntRange ;
3030import androidx .annotation .NonNull ;
31+ import androidx .core .content .ContextCompat ;
3132import androidx .core .graphics .ColorUtils ;
3233import com .google .android .material .resources .MaterialAttributes ;
3334
@@ -66,7 +67,9 @@ private MaterialColors() {
6667 */
6768 @ ColorInt
6869 public static int getColor (@ NonNull View view , @ AttrRes int colorAttributeResId ) {
69- return MaterialAttributes .resolveOrThrow (view , colorAttributeResId );
70+ return resolveColor (
71+ view .getContext (),
72+ MaterialAttributes .resolveTypedValueOrThrow (view , colorAttributeResId ));
7073 }
7174
7275 /**
@@ -77,7 +80,10 @@ public static int getColor(@NonNull View view, @AttrRes int colorAttributeResId)
7780 @ ColorInt
7881 public static int getColor (
7982 Context context , @ AttrRes int colorAttributeResId , String errorMessageComponent ) {
80- return MaterialAttributes .resolveOrThrow (context , colorAttributeResId , errorMessageComponent );
83+ return resolveColor (
84+ context ,
85+ MaterialAttributes .resolveTypedValueOrThrow (
86+ context , colorAttributeResId , errorMessageComponent ));
8187 }
8288
8389 /**
@@ -99,12 +105,22 @@ public static int getColor(
99105 @ NonNull Context context , @ AttrRes int colorAttributeResId , @ ColorInt int defaultValue ) {
100106 TypedValue typedValue = MaterialAttributes .resolve (context , colorAttributeResId );
101107 if (typedValue != null ) {
102- return typedValue . data ;
108+ return resolveColor ( context , typedValue ) ;
103109 } else {
104110 return defaultValue ;
105111 }
106112 }
107113
114+ private static int resolveColor (@ NonNull Context context , @ NonNull TypedValue typedValue ) {
115+ if (typedValue .resourceId != 0 ) {
116+ // Color State List
117+ return ContextCompat .getColor (context , typedValue .resourceId );
118+ } else {
119+ // Color Int
120+ return typedValue .data ;
121+ }
122+ }
123+
108124 /**
109125 * Convenience method that calculates {@link MaterialColors#layer(View, int, int, float)} without
110126 * an {@code overlayAlpha} value by passing in {@code 1f} for the alpha value.
0 commit comments