Skip to content

Instantly share code, notes, and snippets.

@alorma
Last active Aug 12, 2020
Embed
What would you like to do?
fun Context.attributeColorWithAlpha(
@AttrRes overlayColorAttributeResId: Int,
@FloatRange(from = 0.0, to = 1.0) overlayAlpha: Float
): Int {
val surfaceColor = MaterialColors.getColor(this, R.attr.colorSurface, "Color surface not found")
val colorWithAlpha = MaterialColors.getColor(this, overlayColorAttributeResId, "Color attribute not found").let {
MaterialColors.compositeARGBWithAlpha(it, (255 * overlayAlpha).roundToInt())
}
return MaterialColors.layer(surfaceColor, colorWithAlpha)
}
val error8Alpha = context.attributeColorWithAlpha(R.attr.colorError, 0.08f)
val success50Alpha = context.attributeColorWithAlpha(R.attr.success, 0.5f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment