PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt

205 lines
7.1 KiB
Kotlin
Raw Normal View History

/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
2020-10-06 08:46:04 +00:00
*
*/
package code.name.monkey.retromusic.extensions
2020-05-20 20:28:38 +00:00
import android.app.Dialog
2019-12-01 15:27:01 +00:00
import android.content.Context
2019-12-16 05:06:08 +00:00
import android.content.res.ColorStateList
2019-12-01 15:27:01 +00:00
import android.graphics.Color
2020-09-05 15:54:16 +00:00
import android.graphics.drawable.Drawable
2020-07-19 14:39:04 +00:00
import android.widget.Button
2020-05-23 21:47:23 +00:00
import android.widget.CheckBox
import android.widget.SeekBar
2020-09-10 19:22:10 +00:00
import androidx.annotation.AttrRes
import androidx.annotation.CheckResult
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
2020-08-21 10:42:21 +00:00
import androidx.appcompat.widget.AppCompatImageView
2019-12-16 05:06:08 +00:00
import androidx.appcompat.widget.Toolbar
2020-09-05 15:54:16 +00:00
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
2020-05-20 20:28:38 +00:00
import androidx.fragment.app.Fragment
2020-05-20 10:14:49 +00:00
import code.name.monkey.appthemehelper.ThemeStore
2019-12-01 15:27:01 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
2020-08-11 18:29:44 +00:00
import code.name.monkey.appthemehelper.util.MaterialValueHelper
2020-07-19 21:00:30 +00:00
import code.name.monkey.retromusic.App
2019-12-01 15:27:01 +00:00
import code.name.monkey.retromusic.R
2020-08-11 21:31:09 +00:00
import com.google.android.material.button.MaterialButton
2020-08-11 18:29:44 +00:00
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
2020-08-21 05:01:53 +00:00
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.progressindicator.ProgressIndicator
2020-08-13 08:24:36 +00:00
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
fun Int.ripAlpha(): Int {
return ColorUtil.stripAlpha(this)
2019-12-01 15:27:01 +00:00
}
2020-05-20 20:28:38 +00:00
fun Dialog.colorControlNormal() = resolveColor(android.R.attr.colorControlNormal)
2020-09-12 19:39:46 +00:00
2019-12-16 05:06:08 +00:00
fun Toolbar.backgroundTintList() {
val surfaceColor = ATHUtil.resolveColor(context, R.attr.colorSurface, Color.BLACK)
val colorStateList = ColorStateList.valueOf(surfaceColor)
backgroundTintList = colorStateList
2020-02-22 12:10:12 +00:00
}
2020-05-20 20:28:38 +00:00
fun Context.accentColor() = ThemeStore.accentColor(this)
2020-02-22 12:10:12 +00:00
2020-05-20 20:28:38 +00:00
fun Fragment.accentColor() = ThemeStore.accentColor(requireContext())
fun Context.surfaceColor() = resolveColor(R.attr.colorSurface, Color.WHITE)
fun Fragment.surfaceColor() = resolveColor(R.attr.colorSurface, Color.WHITE)
fun Context.textColorSecondary() = resolveColor(android.R.attr.textColorSecondary)
fun Fragment.textColorSecondary() = resolveColor(android.R.attr.textColorSecondary)
fun Context.colorControlNormal() = resolveColor(android.R.attr.colorControlNormal)
fun Fragment.colorControlNormal() = resolveColor(android.R.attr.colorControlNormal)
fun Context.textColorPrimary() = resolveColor(android.R.attr.textColorPrimary)
fun Fragment.textColorPrimary() = resolveColor(android.R.attr.textColorPrimary)
fun Context.resolveColor(@AttrRes attr: Int, fallBackColor: Int = 0) =
ATHUtil.resolveColor(this, attr, fallBackColor)
fun Fragment.resolveColor(@AttrRes attr: Int, fallBackColor: Int = 0) =
ATHUtil.resolveColor(requireContext(), attr, fallBackColor)
2020-02-22 12:10:12 +00:00
2020-05-20 20:28:38 +00:00
fun Dialog.resolveColor(@AttrRes attr: Int, fallBackColor: Int = 0) =
ATHUtil.resolveColor(context, attr, fallBackColor)
2020-05-23 21:47:23 +00:00
fun CheckBox.addAccentColor() {
buttonTintList = ColorStateList.valueOf(ThemeStore.accentColor(context))
}
fun SeekBar.addAccentColor() {
val colorState = ColorStateList.valueOf(ThemeStore.accentColor(context))
progressTintList = colorState
thumbTintList = colorState
2020-07-19 11:21:15 +00:00
}
2020-07-19 14:39:04 +00:00
fun Button.accentTextColor() {
2020-07-19 21:00:30 +00:00
setTextColor(ThemeStore.accentColor(App.getContext()))
2020-08-11 18:29:44 +00:00
}
2020-09-10 19:22:10 +00:00
fun MaterialButton.accentTextColor() {
setTextColor(ThemeStore.accentColor(App.getContext()))
}
2020-09-12 19:39:46 +00:00
fun MaterialButton.accentBackgroundColor() {
backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(App.getContext()))
}
fun MaterialButton.accentOutlineColor() {
val color = ThemeStore.accentColor(context)
val colorStateList = ColorStateList.valueOf(color)
iconTint = colorStateList
strokeColor = colorStateList
setTextColor(colorStateList)
rippleColor = colorStateList
}
2020-08-11 18:29:44 +00:00
fun SeekBar.applyColor(@ColorInt color: Int) {
thumbTintList = ColorStateList.valueOf(color)
progressTintList = ColorStateList.valueOf(color)
progressBackgroundTintList = ColorStateList.valueOf(color)
}
fun ExtendedFloatingActionButton.accentColor() {
val color = ThemeStore.accentColor(context)
val textColor = MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color))
val colorStateList = ColorStateList.valueOf(color)
val textColorStateList = ColorStateList.valueOf(textColor)
backgroundTintList = colorStateList
setTextColor(textColorStateList)
iconTint = textColorStateList
2020-08-11 21:31:09 +00:00
}
2020-08-21 05:01:53 +00:00
fun FloatingActionButton.accentColor() {
val color = ThemeStore.accentColor(context)
val textColor = MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(color))
val colorStateList = ColorStateList.valueOf(color)
val textColorStateList = ColorStateList.valueOf(textColor)
backgroundTintList = colorStateList
imageTintList = textColorStateList
}
2020-08-11 21:31:09 +00:00
fun MaterialButton.applyColor(color: Int) {
val backgroundColorStateList = ColorStateList.valueOf(color)
val textColorColorStateList = ColorStateList.valueOf(
MaterialValueHelper.getPrimaryTextColor(
context,
ColorUtil.isColorLight(color)
)
)
backgroundTintList = backgroundColorStateList
setTextColor(textColorColorStateList)
iconTint = textColorColorStateList
2020-08-13 08:24:36 +00:00
}
2020-08-21 17:07:10 +00:00
fun MaterialButton.applyOutlineColor(color: Int) {
val textColorColorStateList = ColorStateList.valueOf(color)
setTextColor(textColorColorStateList)
iconTint = textColorColorStateList
}
2020-08-13 08:24:36 +00:00
fun TextInputLayout.accentColor() {
val accentColor = ThemeStore.accentColor(context)
val colorState = ColorStateList.valueOf(accentColor)
boxStrokeColor = accentColor
defaultHintTextColor = colorState
isHintAnimationEnabled = true
}
2020-08-14 08:34:03 +00:00
fun ProgressIndicator.accentColor() {
val accentColor = ThemeStore.accentColor(context)
indicatorColors = intArrayOf(accentColor)
trackColor = ColorUtil.withAlpha(accentColor, 0.2f)
}
fun ProgressIndicator.applyColor(color: Int) {
indicatorColors = intArrayOf(color)
trackColor = ColorUtil.withAlpha(color, 0.2f)
}
2020-08-14 08:34:03 +00:00
fun TextInputEditText.accentColor() {
2020-08-21 10:42:21 +00:00
}
fun AppCompatImageView.accentColor(): Int {
return ThemeStore.accentColor(context)
2020-09-05 15:54:16 +00:00
}
@CheckResult
fun Drawable.tint(@ColorInt color: Int): Drawable {
val tintedDrawable = DrawableCompat.wrap(this).mutate()
DrawableCompat.setTint(this, color)
return tintedDrawable
}
@CheckResult
fun Drawable.tint(context: Context, @ColorRes color: Int): Drawable {
return tint(context.getColorCompat(color))
}
@ColorInt
fun Context.getColorCompat(@ColorRes colorRes: Int): Int {
return ContextCompat.getColor(this, colorRes)
}