Skip to content

Commit 14460ac

Browse files
committed
Add a layering method to blend surface and onSurface colors without an alpha
value. PiperOrigin-RevId: 218902432
1 parent 52eb143 commit 14460ac

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import android.view.View;
2828

2929
/**
30-
* A utility class for common color variants used in Material themes
30+
* A utility class for common color variants used in Material themes.
3131
*
3232
* @hide
3333
*/
@@ -44,6 +44,18 @@ public static int getColor(View view, @AttrRes int colorAttributeResId) {
4444
return MaterialAttributes.resolveAttributeOrThrow(view, colorAttributeResId).data;
4545
}
4646

47+
/**
48+
* Convenience method that calculates {@link MaterialColors#layer(View, int, int, float)} without
49+
* an {@code overlayAlpha} value by passing in {@code 1f} for the alpha value.
50+
*/
51+
@ColorInt
52+
public static int layer(
53+
View view,
54+
@AttrRes int backgroundColorAttributeResId,
55+
@AttrRes int overlayColorAttributeResId) {
56+
return layer(view, backgroundColorAttributeResId, overlayColorAttributeResId, 1f);
57+
}
58+
4759
/**
4860
* Convenience method that wraps {@link MaterialColors#layer(int, int, float)} for layering colors
4961
* from theme attributes.
@@ -70,6 +82,15 @@ public static int layer(
7082
@FloatRange(from = 0.0, to = 1.0) float overlayAlpha) {
7183
int computedAlpha = Math.round(Color.alpha(overlayColor) * overlayAlpha);
7284
int computedOverlayColor = ColorUtils.setAlphaComponent(overlayColor, computedAlpha);
73-
return ColorUtils.compositeColors(computedOverlayColor, backgroundColor);
85+
return layer(backgroundColor, computedOverlayColor);
86+
}
87+
88+
/**
89+
* Calculates a color that represents the layering of the {@code overlayColor} on top of the
90+
* {@code backgroundColor}.
91+
*/
92+
@ColorInt
93+
public static int layer(@ColorInt int backgroundColor, @ColorInt int overlayColor) {
94+
return ColorUtils.compositeColors(overlayColor, backgroundColor);
7495
}
7596
}

0 commit comments

Comments
 (0)