删除一个ColorUtils

pull/300/head
gedoor 4 years ago
parent c3c63b5464
commit 5d9a07839d
  1. 6
      app/src/main/java/io/legado/app/App.kt
  2. 6
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  3. 1
      app/src/main/java/io/legado/app/lib/theme/ATH.kt
  4. 156
      app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt
  5. 1
      app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt
  6. 1
      app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt
  7. 1
      app/src/main/java/io/legado/app/lib/theme/TintHelper.kt
  8. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt
  9. 1
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  10. 9
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  11. 2
      app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt
  12. 2
      app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt
  13. 2
      app/src/main/java/io/legado/app/ui/widget/text/AccentBgTextView.kt
  14. 79
      app/src/main/java/io/legado/app/utils/ColorUtils.kt

@ -20,12 +20,8 @@ import io.legado.app.help.ActivityHelp
import io.legado.app.help.AppConfig
import io.legado.app.help.CrashHandler
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.postEvent
import io.legado.app.utils.putPrefInt
import io.legado.app.utils.*
@Suppress("DEPRECATION")
class App : MultiDexApplication() {

@ -13,13 +13,9 @@ import io.legado.app.R
import io.legado.app.constant.AppConst
import io.legado.app.constant.Theme
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.applyOpenTint
import io.legado.app.utils.applyTint
import io.legado.app.utils.disableAutoFill
import io.legado.app.utils.hideSoftInput
import io.legado.app.utils.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

@ -19,6 +19,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
import io.legado.app.App
import io.legado.app.R
import io.legado.app.help.AppConfig
import io.legado.app.utils.ColorUtils
import io.legado.app.utils.dp
import io.legado.app.utils.getCompatColor
import kotlinx.android.synthetic.main.activity_main.view.*

@ -1,156 +0,0 @@
package io.legado.app.lib.theme
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import java.util.*
object ColorUtils {
fun intToString(intColor: Int): String {
return String.format("#%06X", 0xFFFFFF and intColor)
}
fun stripAlpha(@ColorInt color: Int): Int {
return -0x1000000 or color
}
@ColorInt
fun shiftColor(@ColorInt color: Int, @FloatRange(from = 0.0, to = 2.0) by: Float): Int {
if (by == 1f) return color
val alpha = Color.alpha(color)
val hsv = FloatArray(3)
Color.colorToHSV(color, hsv)
hsv[2] *= by // value component
return (alpha shl 24) + (0x00ffffff and Color.HSVToColor(hsv))
}
@ColorInt
fun darkenColor(@ColorInt color: Int): Int {
return shiftColor(color, 0.9f)
}
@ColorInt
fun lightenColor(@ColorInt color: Int): Int {
return shiftColor(color, 1.1f)
}
fun isColorLight(@ColorInt color: Int): Boolean {
val darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255
return darkness < 0.4
}
@ColorInt
fun invertColor(@ColorInt color: Int): Int {
val r = 255 - Color.red(color)
val g = 255 - Color.green(color)
val b = 255 - Color.blue(color)
return Color.argb(Color.alpha(color), r, g, b)
}
@ColorInt
fun adjustAlpha(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) factor: Float): Int {
val alpha = Math.round(Color.alpha(color) * factor)
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
return Color.argb(alpha, red, green, blue)
}
@ColorInt
fun withAlpha(@ColorInt baseColor: Int, @FloatRange(from = 0.0, to = 1.0) alpha: Float): Int {
val a = Math.min(255, Math.max(0, (alpha * 255).toInt())) shl 24
val rgb = 0x00ffffff and baseColor
return a + rgb
}
/**
* Taken from CollapsingToolbarLayout's CollapsingTextHelper class.
*/
fun blendColors(color1: Int, color2: Int, @FloatRange(from = 0.0, to = 1.0) ratio: Float): Int {
val inverseRatio = 1f - ratio
val a = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio
val r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio
val g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio
val b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio
return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
}
/**
* 按条件的到随机颜色
*
* @param alpha 透明
* @param lower 下边界
* @param upper 上边界
* @return 颜色值
*/
fun getRandomColor(alpha: Int, lower: Int, upper: Int): Int {
return RandomColor(alpha, lower, upper).color
}
/**
* @return 获取随机色
*/
fun getRandomColor(): Int {
return RandomColor(255, 80, 200).color
}
/**
* 随机颜色
*/
class RandomColor(alpha: Int, lower: Int, upper: Int) {
private var alpha: Int = 0
private var lower: Int = 0
private var upper: Int = 0
//随机数是前闭 后开
val color: Int
get() {
val red = getLower() + Random().nextInt(getUpper() - getLower() + 1)
val green = getLower() + Random().nextInt(getUpper() - getLower() + 1)
val blue = getLower() + Random().nextInt(getUpper() - getLower() + 1)
return Color.argb(getAlpha(), red, green, blue)
}
init {
require(upper > lower) { "must be lower < upper" }
setAlpha(alpha)
setLower(lower)
setUpper(upper)
}
private fun getAlpha(): Int {
return alpha
}
private fun setAlpha(alpha: Int) {
var alpha1 = alpha
if (alpha1 > 255) alpha1 = 255
if (alpha1 < 0) alpha1 = 0
this.alpha = alpha1
}
private fun getLower(): Int {
return lower
}
private fun setLower(lower: Int) {
var lower1 = lower
if (lower1 < 0) lower1 = 0
this.lower = lower1
}
private fun getUpper(): Int {
return upper
}
private fun setUpper(upper: Int) {
var upper1 = upper
if (upper1 > 255) upper1 = 255
this.upper = upper1
}
}
}

@ -6,6 +6,7 @@ import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import io.legado.app.R
import io.legado.app.utils.ColorUtils
/**
* @author Karim Abou Zeid (kabouzeid)

@ -11,6 +11,7 @@ import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import io.legado.app.App
import io.legado.app.R
import io.legado.app.utils.ColorUtils
/**
* @author Aidan Follestad (afollestad), Karim Abou Zeid (kabouzeid)

@ -18,6 +18,7 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import io.legado.app.R
import io.legado.app.utils.ColorUtils
/**
* @author afollestad, plusCubed

@ -13,8 +13,10 @@ import io.legado.app.R
import io.legado.app.constant.EventBus
import io.legado.app.help.AppConfig
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.*
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.buttonDisabledColor
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.view_read_menu.view.*

@ -19,7 +19,6 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchKeyword
import io.legado.app.lib.theme.*
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.ui.widget.recycler.LoadMoreView

@ -18,7 +18,6 @@ import io.legado.app.help.AppConfig
import io.legado.app.help.LauncherIconHelp
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.ui.widget.number.NumberPickerDialog
import io.legado.app.ui.widget.prefs.ColorPreference
import io.legado.app.ui.widget.prefs.IconListPreference
@ -171,14 +170,14 @@ class ThemeConfigFragment : BasePreferenceFragment(),
when (which) {
0 -> {
putPrefInt(PreferKey.cPrimary, getCompatColor(R.color.md_grey_100))
putPrefInt(PreferKey.cAccent, getCompatColor(R.color.lightBlue_color))
putPrefInt(PreferKey.cAccent, getCompatColor(R.color.md_deep_orange_600))
putPrefInt(PreferKey.cBackground, getCompatColor(R.color.md_grey_100))
putPrefInt(PreferKey.cBBackground, getCompatColor(R.color.md_grey_200))
AppConfig.isNightTheme = false
}
1 -> {
putPrefInt(PreferKey.cNPrimary, getCompatColor(R.color.md_grey_900))
putPrefInt(PreferKey.cNAccent, getCompatColor(R.color.lightBlue_color))
putPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_deep_orange_600))
putPrefInt(PreferKey.cNBackground, getCompatColor(R.color.md_grey_900))
putPrefInt(PreferKey.cNBBackground, getCompatColor(R.color.md_grey_900))
AppConfig.isNightTheme = true
@ -192,14 +191,14 @@ class ThemeConfigFragment : BasePreferenceFragment(),
}
3 -> {
putPrefInt(PreferKey.cPrimary, getCompatColor(R.color.white))
putPrefInt(PreferKey.cAccent, getCompatColor(R.color.lightBlue_color))
putPrefInt(PreferKey.cAccent, getCompatColor(R.color.md_deep_orange_600))
putPrefInt(PreferKey.cBackground, getCompatColor(R.color.white))
putPrefInt(PreferKey.cBBackground, getCompatColor(R.color.white))
AppConfig.isNightTheme = false
}
4 -> {
putPrefInt(PreferKey.cNPrimary, getCompatColor(R.color.black))
putPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_grey_500))
putPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_deep_orange_600))
putPrefInt(PreferKey.cNBackground, getCompatColor(R.color.black))
putPrefInt(PreferKey.cNBBackground, getCompatColor(R.color.black))
AppConfig.isNightTheme = true

@ -14,7 +14,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.jaredrummler.android.colorpicker.*
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.utils.ColorUtils
class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs),
ColorPickerDialogListener {

@ -9,9 +9,9 @@ import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceViewHolder
import io.legado.app.R
import io.legado.app.help.AppConfig
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.utils.ColorUtils
class PreferenceCategory(context: Context, attrs: AttributeSet) : PreferenceCategory(context, attrs) {

@ -5,9 +5,9 @@ import android.graphics.Color
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import io.legado.app.R
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.ColorUtils
import io.legado.app.utils.dp
import io.legado.app.utils.getCompatColor

@ -4,6 +4,7 @@ import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import java.util.*
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
@ -82,4 +83,82 @@ object ColorUtils {
return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
}
/**
* 按条件的到随机颜色
*
* @param alpha 透明
* @param lower 下边界
* @param upper 上边界
* @return 颜色值
*/
fun getRandomColor(alpha: Int, lower: Int, upper: Int): Int {
return RandomColor(alpha, lower, upper).color
}
/**
* @return 获取随机色
*/
fun getRandomColor(): Int {
return RandomColor(255, 80, 200).color
}
/**
* 随机颜色
*/
class RandomColor(alpha: Int, lower: Int, upper: Int) {
private var alpha: Int = 0
private var lower: Int = 0
private var upper: Int = 0
//随机数是前闭 后开
val color: Int
get() {
val red = getLower() + Random().nextInt(getUpper() - getLower() + 1)
val green = getLower() + Random().nextInt(getUpper() - getLower() + 1)
val blue = getLower() + Random().nextInt(getUpper() - getLower() + 1)
return Color.argb(getAlpha(), red, green, blue)
}
init {
require(upper > lower) { "must be lower < upper" }
setAlpha(alpha)
setLower(lower)
setUpper(upper)
}
private fun getAlpha(): Int {
return alpha
}
private fun setAlpha(alpha: Int) {
var alpha1 = alpha
if (alpha1 > 255) alpha1 = 255
if (alpha1 < 0) alpha1 = 0
this.alpha = alpha1
}
private fun getLower(): Int {
return lower
}
private fun setLower(lower: Int) {
var lower1 = lower
if (lower1 < 0) lower1 = 0
this.lower = lower1
}
private fun getUpper(): Int {
return upper
}
private fun setUpper(upper: Int) {
var upper1 = upper
if (upper1 > 255) upper1 = 255
this.upper = upper1
}
}
}

Loading…
Cancel
Save