From 5d9a07839d2f1805de325b6a79ae815358b06d2b Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 8 Aug 2020 15:58:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=80=E4=B8=AAColorUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 6 +- .../java/io/legado/app/base/BaseActivity.kt | 6 +- .../main/java/io/legado/app/lib/theme/ATH.kt | 1 + .../io/legado/app/lib/theme/ColorUtils.kt | 156 ------------------ .../app/lib/theme/MaterialValueHelper.kt | 1 + .../io/legado/app/lib/theme/ThemeStore.kt | 1 + .../io/legado/app/lib/theme/TintHelper.kt | 1 + .../io/legado/app/ui/book/read/ReadMenu.kt | 6 +- .../app/ui/book/search/SearchActivity.kt | 1 - .../app/ui/config/ThemeConfigFragment.kt | 9 +- .../app/ui/widget/prefs/ColorPreference.kt | 2 +- .../app/ui/widget/prefs/PreferenceCategory.kt | 2 +- .../app/ui/widget/text/AccentBgTextView.kt | 2 +- .../java/io/legado/app/utils/ColorUtils.kt | 79 +++++++++ 14 files changed, 96 insertions(+), 177 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index eba95b8e9..c8cd2620c 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.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() { diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index df448cc5d..7a7d8a6a8 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/lib/theme/ATH.kt b/app/src/main/java/io/legado/app/lib/theme/ATH.kt index 8b6a4782c..13d5ed0d9 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATH.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATH.kt @@ -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.* diff --git a/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt b/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt deleted file mode 100644 index b4a17748e..000000000 --- a/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt +++ /dev/null @@ -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 - } - } -} diff --git a/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt b/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt index bc898d065..018e3dd60 100644 --- a/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt +++ b/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt @@ -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) diff --git a/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt b/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt index 4259b6a7c..321d2a8f0 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt @@ -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) diff --git a/app/src/main/java/io/legado/app/lib/theme/TintHelper.kt b/app/src/main/java/io/legado/app/lib/theme/TintHelper.kt index a54046dd3..76313cd46 100644 --- a/app/src/main/java/io/legado/app/lib/theme/TintHelper.kt +++ b/app/src/main/java/io/legado/app/lib/theme/TintHelper.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index 85e469a4a..178d2cbac 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -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.* diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 9223c96f5..acf63099b 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index b6bc2a3e0..2658b9470 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt index aae9c20b8..8f293eebb 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt @@ -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 { diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt index c150dbe56..a21d0e1ce 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt @@ -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) { diff --git a/app/src/main/java/io/legado/app/ui/widget/text/AccentBgTextView.kt b/app/src/main/java/io/legado/app/ui/widget/text/AccentBgTextView.kt index b2dcc2578..8da4fa137 100644 --- a/app/src/main/java/io/legado/app/ui/widget/text/AccentBgTextView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/text/AccentBgTextView.kt @@ -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 diff --git a/app/src/main/java/io/legado/app/utils/ColorUtils.kt b/app/src/main/java/io/legado/app/utils/ColorUtils.kt index 9eceb6724..d256f879f 100644 --- a/app/src/main/java/io/legado/app/utils/ColorUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ColorUtils.kt @@ -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 + } + } + }