mirror of
https://github.com/Docile-Alligator/Infinity-For-Reddit.git
synced 2026-05-13 17:17:01 +00:00
Generate a track color for ThemedMaterialSwitch.
This commit is contained in:
@ -6,6 +6,7 @@ import android.content.res.ColorStateList
|
||||
import android.util.AttributeSet
|
||||
import com.google.android.material.materialswitch.MaterialSwitch
|
||||
import ml.docilealligator.infinityforreddit.Infinity
|
||||
import ml.docilealligator.infinityforreddit.utils.getMaterialSwitchTrackColorFromThumbColor
|
||||
|
||||
class ThemedMaterialSwitch @JvmOverloads constructor(
|
||||
context: Context,
|
||||
@ -21,7 +22,7 @@ class ThemedMaterialSwitch @JvmOverloads constructor(
|
||||
intArrayOf(R.attr.state_checked)
|
||||
)
|
||||
val colors = intArrayOf(
|
||||
customThemeWrapper.colorPrimary
|
||||
getMaterialSwitchTrackColorFromThumbColor(customThemeWrapper.colorAccent)
|
||||
)
|
||||
setTrackTintList(ColorStateList(states, colors))
|
||||
}
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
package ml.docilealligator.infinityforreddit.utils
|
||||
|
||||
import android.graphics.Color
|
||||
import androidx.core.graphics.ColorUtils
|
||||
|
||||
fun getMaterialSwitchTrackColorFromThumbColor(thumbColor: Int): Int {
|
||||
val blendedColor = if (ColorUtils.calculateLuminance(thumbColor) < 0.5) Color.WHITE else Color.BLACK
|
||||
val originalAlpha = Color.alpha(thumbColor)
|
||||
val opaqueThumbColor = ColorUtils.setAlphaComponent(thumbColor, 255)
|
||||
val opaqueNewColor = ColorUtils.blendARGB(opaqueThumbColor, blendedColor, 0.6f)
|
||||
|
||||
return ColorUtils.setAlphaComponent(opaqueNewColor, originalAlpha)
|
||||
}
|
||||
Reference in New Issue
Block a user