@@ -31,46 +31,54 @@ public class ElevationOverlayProvider {
3131 private static final float FORMULA_MULTIPLIER = 4.5f ;
3232 private static final float FORMULA_OFFSET = 2f ;
3333
34- private final boolean elevationOverlaysEnabled ;
35- private final int elevationOverlaysColor ;
34+ private final boolean elevationOverlayEnabled ;
35+ private final int elevationOverlayColor ;
3636 private final int colorSurface ;
3737 private final float displayDensity ;
3838
3939 public ElevationOverlayProvider (Context context ) {
40- this .elevationOverlaysEnabled =
41- MaterialAttributes .resolveBoolean (context , R .attr .elevationOverlaysEnabled , false );
42- this .elevationOverlaysColor =
43- MaterialColors .getColor (context , R .attr .elevationOverlaysColor , Color .TRANSPARENT );
40+ this .elevationOverlayEnabled =
41+ MaterialAttributes .resolveBoolean (context , R .attr .elevationOverlayEnabled , false );
42+ this .elevationOverlayColor =
43+ MaterialColors .getColor (context , R .attr .elevationOverlayColor , Color .TRANSPARENT );
4444 this .colorSurface = MaterialColors .getColor (context , R .attr .colorSurface , Color .TRANSPARENT );
4545 this .displayDensity = context .getResources ().getDisplayMetrics ().density ;
4646 }
4747
4848 /**
49- * Applies the calculated elevation overlay (@see #layerOverlay(int, float)) only if the current
50- * theme's {@code R.attr.elevationOverlaysEnabled} is true and the {@code backgroundColor} matches
51- * the theme's surface color ({@code R.attr.colorSurface}); otherwise returns the {@code
52- * backgroundColor}.
49+ * Blends the calculated elevation overlay color (@see #compositeOverlayIfNeeded(int, float)) with
50+ * the current theme's color int value for {@code R.attr.colorSurface} if needed.
5351 */
5452 @ ColorInt
55- public int layerOverlayIfNeeded (@ ColorInt int backgroundColor , float elevation ) {
56- if (elevationOverlaysEnabled && isSurfaceColor (backgroundColor )) {
57- return layerOverlay (backgroundColor , elevation );
53+ public int compositeOverlayWithThemeSurfaceColorIfNeeded (float elevation ) {
54+ return compositeOverlayIfNeeded (colorSurface , elevation );
55+ }
56+
57+ /**
58+ * Blends the calculated elevation overlay color (@see #compositeOverlay(int, float)) with the
59+ * {@code backgroundColor}, only if the current theme's {@code R.attr.elevationOverlayEnabled} is
60+ * true and the {@code backgroundColor} matches the theme's surface color ({@code
61+ * R.attr.colorSurface}); otherwise returns the {@code backgroundColor}.
62+ */
63+ @ ColorInt
64+ public int compositeOverlayIfNeeded (@ ColorInt int backgroundColor , float elevation ) {
65+ if (elevationOverlayEnabled && isThemeSurfaceColor (backgroundColor )) {
66+ return compositeOverlay (backgroundColor , elevation );
5867 } else {
5968 return backgroundColor ;
6069 }
6170 }
6271
6372 /**
64- * Calculates a color that represents the layering of the current theme's {@code
65- * R.attr.elevationOverlaysColor} on top of the {@code backgroundColor}.
73+ * Blends the calculated elevation overlay color with the provided {@code backgroundColor}.
6674 *
67- * <p>An alpha level is applied to the {@code R.attr.elevationOverlaysColor } by using a formula
68- * that is based on the provided {@code elevation} value.
75+ * <p>An alpha level is applied to the theme's {@code R.attr.elevationOverlayColor } by using a
76+ * formula that is based on the provided {@code elevation} value.
6977 */
7078 @ ColorInt
71- public int layerOverlay (@ ColorInt int backgroundColor , float elevation ) {
79+ public int compositeOverlay (@ ColorInt int backgroundColor , float elevation ) {
7280 float overlayAlpha = calculateOverlayAlphaFraction (elevation );
73- return MaterialColors .layer (backgroundColor , elevationOverlaysColor , overlayAlpha );
81+ return MaterialColors .layer (backgroundColor , elevationOverlayColor , overlayAlpha );
7482 }
7583
7684 /**
@@ -95,33 +103,24 @@ public float calculateOverlayAlphaFraction(float elevation) {
95103 return Math .min (alphaFraction , 1 );
96104 }
97105
98- /** Returns the current theme's boolean value for {@code R.attr.elevationOverlaysEnabled }. */
99- public boolean isOverlaysEnabled () {
100- return elevationOverlaysEnabled ;
106+ /** Returns the current theme's boolean value for {@code R.attr.elevationOverlayEnabled }. */
107+ public boolean isThemeElevationOverlayEnabled () {
108+ return elevationOverlayEnabled ;
101109 }
102110
103- /** Returns the current theme's color int value for {@code R.attr.elevationOverlaysColor }. */
111+ /** Returns the current theme's color int value for {@code R.attr.elevationOverlayColor }. */
104112 @ ColorInt
105- public int getOverlaysColor () {
106- return elevationOverlaysColor ;
113+ public int getThemeElevationOverlayColor () {
114+ return elevationOverlayColor ;
107115 }
108116
109117 /** Returns the current theme's color int value for {@code R.attr.colorSurface}. */
110118 @ ColorInt
111- public int getSurfaceColor () {
119+ public int getThemeSurfaceColor () {
112120 return colorSurface ;
113121 }
114122
115- /**
116- * Returns the current theme's color int value for {@code R.attr.colorSurface}, with an elevation
117- * overlay applied if needed (@see #layerOverlayIfNeeded(int, float)).
118- */
119- @ ColorInt
120- public int getSurfaceColorWithOverlayIfNeeded (float elevation ) {
121- return layerOverlayIfNeeded (colorSurface , elevation );
122- }
123-
124- private boolean isSurfaceColor (@ ColorInt int color ) {
123+ private boolean isThemeSurfaceColor (@ ColorInt int color ) {
125124 return ColorUtils .setAlphaComponent (color , 255 ) == colorSurface ;
126125 }
127126}
0 commit comments