Skip to content

Commit 2544c1f

Browse files
committed
[Color] Fix MaterialColors cannot handle CSL issue
Resolves #955 PiperOrigin-RevId: 440358122
1 parent a49fa6a commit 2544c1f

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

lib/java/com/google/android/material/color/MaterialColors.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import androidx.annotation.FloatRange;
2929
import androidx.annotation.IntRange;
3030
import androidx.annotation.NonNull;
31+
import androidx.core.content.ContextCompat;
3132
import androidx.core.graphics.ColorUtils;
3233
import 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.

lib/java/com/google/android/material/resources/MaterialAttributes.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ public static TypedValue resolve(@NonNull Context context, @AttrRes int attribut
5050
return null;
5151
}
5252

53-
/**
54-
* Returns the {@link TypedValue} for the provided {@code attributeResId}.
55-
*
56-
* @throws IllegalArgumentException if the attribute is not present in the current theme.
57-
*/
58-
public static int resolveOrThrow(
53+
@NonNull
54+
public static TypedValue resolveTypedValueOrThrow(
55+
@NonNull View componentView, @AttrRes int attributeResId) {
56+
return resolveTypedValueOrThrow(
57+
componentView.getContext(), attributeResId, componentView.getClass().getCanonicalName());
58+
}
59+
60+
@NonNull
61+
public static TypedValue resolveTypedValueOrThrow(
5962
@NonNull Context context,
6063
@AttrRes int attributeResId,
6164
@NonNull String errorMessageComponent) {
@@ -71,7 +74,19 @@ public static int resolveOrThrow(
7174
errorMessageComponent,
7275
context.getResources().getResourceName(attributeResId)));
7376
}
74-
return typedValue.data;
77+
return typedValue;
78+
}
79+
80+
/**
81+
* Returns the {@link TypedValue} for the provided {@code attributeResId}.
82+
*
83+
* @throws IllegalArgumentException if the attribute is not present in the current theme.
84+
*/
85+
public static int resolveOrThrow(
86+
@NonNull Context context,
87+
@AttrRes int attributeResId,
88+
@NonNull String errorMessageComponent) {
89+
return resolveTypedValueOrThrow(context, attributeResId, errorMessageComponent).data;
7590
}
7691

7792
/**
@@ -80,9 +95,9 @@ public static int resolveOrThrow(
8095
*
8196
* @throws IllegalArgumentException if the attribute is not present in the current theme.
8297
*/
83-
public static int resolveOrThrow(@NonNull View componentView, @AttrRes int attributeResId) {
84-
return resolveOrThrow(
85-
componentView.getContext(), attributeResId, componentView.getClass().getCanonicalName());
98+
public static int resolveOrThrow(
99+
@NonNull View componentView, @AttrRes int attributeResId) {
100+
return resolveTypedValueOrThrow(componentView, attributeResId).data;
86101
}
87102

88103
/**

0 commit comments

Comments
 (0)