Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Created December 27, 2022 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandaliya/b7d50ea69aa6a86eb4beabc421153f00 to your computer and use it in GitHub Desktop.
Save imandaliya/b7d50ea69aa6a86eb4beabc421153f00 to your computer and use it in GitHub Desktop.
// how to get material color/colour programatically
// material color using code
import android.view.View
import androidx.core.content.ContextCompat
import com.google.android.material.color.MaterialColors
fun View.colorPrimary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorPrimary,
ContextCompat.getColor(context, R.color.md_theme_primary)
)
fun View.colorOnPrimary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnPrimary,
ContextCompat.getColor(context, R.color.md_theme_onPrimary)
)
fun View.colorPrimaryContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorPrimaryContainer,
ContextCompat.getColor(context, R.color.md_theme_primaryContainer)
)
fun View.colorOnPrimaryContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnPrimaryContainer,
ContextCompat.getColor(context, R.color.md_theme_onPrimaryContainer)
)
fun View.colorSecondary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorSecondary,
ContextCompat.getColor(context, R.color.md_theme_secondary)
)
fun View.colorOnSecondary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnSecondary,
ContextCompat.getColor(context, R.color.md_theme_onSecondary)
)
fun View.colorSecondaryContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorSecondaryContainer,
ContextCompat.getColor(context, R.color.md_theme_secondaryContainer)
)
fun View.colorOnSecondaryContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnSecondaryContainer,
ContextCompat.getColor(context, R.color.md_theme_onSecondaryContainer)
)
fun View.colorTertiary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorTertiary,
ContextCompat.getColor(context, R.color.md_theme_tertiary)
)
fun View.colorOnTertiary() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnTertiary,
ContextCompat.getColor(context, R.color.md_theme_onTertiary)
)
fun View.colorTertiaryContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorTertiaryContainer,
ContextCompat.getColor(context, R.color.md_theme_tertiaryContainer)
)
fun View.colorError() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorError,
ContextCompat.getColor(context, R.color.md_theme_error)
)
fun View.colorErrorContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorErrorContainer,
ContextCompat.getColor(context, R.color.md_theme_errorContainer)
)
fun View.colorOnError() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnError,
ContextCompat.getColor(context, R.color.md_theme_onError)
)
fun View.colorOnErrorContainer() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnErrorContainer,
ContextCompat.getColor(context, R.color.md_theme_onErrorContainer)
)
fun View.colorSurface() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorSurface,
ContextCompat.getColor(context, R.color.md_theme_surface)
)
fun View.colorOnSurface() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnSurface,
ContextCompat.getColor(context, R.color.md_theme_onSurface)
)
fun View.colorSurfaceVariant() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorSurfaceVariant,
ContextCompat.getColor(context, R.color.md_theme_surfaceVariant)
)
fun View.colorOnSurfaceVariant() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnSurfaceVariant,
ContextCompat.getColor(context, R.color.md_theme_onSurfaceVariant)
)
fun View.colorOutline() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOutline,
ContextCompat.getColor(context, R.color.md_theme_outline)
)
fun View.colorOnSurfaceInverse() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorOnSurfaceInverse,
ContextCompat.getColor(context, R.color.md_theme_inverseOnSurface)
)
fun View.colorSurfaceInverse() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorSurfaceInverse,
ContextCompat.getColor(context, R.color.md_theme_inverseSurface)
)
fun View.colorPrimaryInverse() = MaterialColors.getColor(
this,
com.google.android.material.R.attr.colorPrimaryInverse,
ContextCompat.getColor(context, R.color.md_theme_inversePrimary)
)
/*
this color still missing in programtically call
<color name="md_theme_shadow">#000000</color>
<color name="md_theme_surfaceTint">#BE003F</color>
<color name="md_theme_outlineVariant">#D7C1C2</color>
<color name="md_theme_scrim">#000000</color>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment