Skip to content

Instantly share code, notes, and snippets.

@alorma
Last active August 12, 2020 06:31
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save alorma/56078b41281928cbfc188c54693bb962 to your computer and use it in GitHub Desktop.
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