androidMain.tech.skot.core.resourcesExtensions.kt Maven / Gradle / Ivy
package tech.skot.core
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.ImageViewCompat
import tech.skot.core.view.*
import android.graphics.Color as AndroidColor
fun Color.toColor(context: Context): Int {
return when (this) {
is ColorHex -> AndroidColor.parseColor(this.color)
is ColorRef -> ContextCompat.getColor(context, this.res)
else -> android.graphics.Color.WHITE
}
}
fun View.setBackground(resource: Resource) {
when (resource) {
is Icon -> this.setBackgroundResource(resource.res)
is Color -> this.setBackgroundColor(resource.toColor(this.context))
}
}
fun View.setBackgroundTint(color: Color?) {
this.backgroundTintList = color?.let { ColorStateList.valueOf(color.toColor(this.context)) }
}
fun ImageView.setImageTint(color: Color?) {
ImageViewCompat.setImageTintList(this, color?.let { ColorStateList.valueOf(color.toColor(this.context)) })
}
fun Drawable.setTint(context: Context, color: Color): Drawable {
val wrappedDrawable = DrawableCompat.wrap(this)
DrawableCompat.setTint(wrappedDrawable, color.toColor(context))
return wrappedDrawable
}
fun Dimen.toPixelSize(context: Context): Int {
return when (this) {
is DimenDP -> (this.dp * Resources.getSystem().displayMetrics.density).toInt()
is DimenRef -> context.resources.getDimensionPixelSize(this.res)
else -> {
throw Exception("Dimen.toPixelSize can only be used with ${DimenDP::class.qualifiedName} and ${DimenRef::class.qualifiedName}, create a custom extension for custom dimen ${this::class.simpleName}")
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy