Skip to content

Commit 6a767d7

Browse files
dsn5ftleticiarossi
authored andcommitted
Update ElevationOverlayProvider compositeOverlay() to preserve original background color alpha
PiperOrigin-RevId: 278625128 (cherry picked from commit 7f04320)
1 parent de444c6 commit 6a767d7

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/java/com/google/android/material/elevation/ElevationOverlayProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ public int compositeOverlay(
114114
*/
115115
@ColorInt
116116
public int compositeOverlay(@ColorInt int backgroundColor, float elevation) {
117-
float overlayAlpha = calculateOverlayAlphaFraction(elevation);
118-
return MaterialColors.layer(backgroundColor, elevationOverlayColor, overlayAlpha);
117+
float overlayAlphaFraction = calculateOverlayAlphaFraction(elevation);
118+
int backgroundAlpha = Color.alpha(backgroundColor);
119+
int backgroundColorOpaque = ColorUtils.setAlphaComponent(backgroundColor, 255);
120+
int overlayColorOpaque =
121+
MaterialColors.layer(backgroundColorOpaque, elevationOverlayColor, overlayAlphaFraction);
122+
return ColorUtils.setAlphaComponent(overlayColorOpaque, backgroundAlpha);
119123
}
120124

121125
/**

lib/javatests/com/google/android/material/elevation/ElevationOverlayProviderTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.Context;
2424
import android.graphics.Color;
2525
import android.os.Build.VERSION_CODES;
26+
import androidx.core.graphics.ColorUtils;
2627
import androidx.core.view.ViewCompat;
2728
import android.view.View;
2829
import android.view.ViewGroup;
@@ -44,6 +45,8 @@ public class ElevationOverlayProviderTest {
4445
private static final float ELEVATION_ZERO = 0;
4546
private static final float ELEVATION_NON_ZERO = 4;
4647
private static final float ELEVATION_OVERLAY_VIEW_PARENT = 8;
48+
private static final int ALPHA_TRANSLUCENT = 75;
49+
private static final int ALPHA_TRANSPARENT = 0;
4750
private static final String EXPECTED_ELEVATION_NON_ZERO_COLOR_WITH_OVERLAY = "ff181818";
4851
private static final String EXPECTED_ELEVATION_NON_ZERO_COLOR_WITH_OVERLAY_AND_VIEW = "ff232323";
4952
private static final float EXPECTED_ELEVATION_NON_ZERO_ALPHA_FRACTION = 0.09f;
@@ -77,6 +80,32 @@ public void initOverlayView() {
7780
.isEqualTo(provider.compositeOverlay(backgroundColor, ELEVATION_NON_ZERO));
7881
}
7982

83+
@Test
84+
public void
85+
givenOverlayEnabledAndTranslucentSurfaceColorAndElevation_whenCompositeOverlayIfNeeded_returnsColorWithOverlay() {
86+
provider = new ElevationOverlayProvider(context);
87+
88+
int backgroundColor =
89+
ColorUtils.setAlphaComponent(provider.getThemeSurfaceColor(), ALPHA_TRANSLUCENT);
90+
int actualOverlayColor = provider.compositeOverlayIfNeeded(backgroundColor, ELEVATION_NON_ZERO);
91+
assertThat(actualOverlayColor)
92+
.isEqualTo(provider.compositeOverlay(backgroundColor, ELEVATION_NON_ZERO));
93+
assertThat(Color.alpha(actualOverlayColor)).isEqualTo(ALPHA_TRANSLUCENT);
94+
}
95+
96+
@Test
97+
public void
98+
givenOverlayEnabledAndTransparentSurfaceColorAndElevation_whenCompositeOverlayIfNeeded_returnsColorWithOverlay() {
99+
provider = new ElevationOverlayProvider(context);
100+
101+
int backgroundColor =
102+
ColorUtils.setAlphaComponent(provider.getThemeSurfaceColor(), ALPHA_TRANSPARENT);
103+
int actualOverlayColor = provider.compositeOverlayIfNeeded(backgroundColor, ELEVATION_NON_ZERO);
104+
assertThat(actualOverlayColor)
105+
.isEqualTo(provider.compositeOverlay(backgroundColor, ELEVATION_NON_ZERO));
106+
assertThat(Color.alpha(actualOverlayColor)).isEqualTo(ALPHA_TRANSPARENT);
107+
}
108+
80109
@Test
81110
public void
82111
givenOverlayEnabledAndSurfaceColorAndElevationAndOverlayView_whenCompositeOverlayIfNeeded_returnsColorWithOverlay() {

0 commit comments

Comments
 (0)