From f0bb7ce4b080e451081b0978d9c2bf427424958b Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 14 May 2022 19:23:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/ColorUtils.kt | 87 +------------------ .../java/io/legado/app/utils/RandomColor.kt | 61 +++++++++++++ 2 files changed, 65 insertions(+), 83 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/RandomColor.kt 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 6ac8eccd1..46be20973 100644 --- a/app/src/main/java/io/legado/app/utils/ColorUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ColorUtils.kt @@ -1,16 +1,18 @@ package io.legado.app.utils import android.graphics.Color - import androidx.annotation.ColorInt import androidx.annotation.FloatRange import androidx.core.graphics.ColorUtils -import java.util.* import kotlin.math.* @Suppress("unused", "MemberVisibilityCanBePrivate") object ColorUtils { + fun isColorLight(@ColorInt color: Int): Boolean { + return ColorUtils.calculateLuminance(color) >= 0.65 + } + fun intToString(intColor: Int): String { return String.format("#%06X", 0xFFFFFF and intColor) } @@ -39,10 +41,6 @@ object ColorUtils { return shiftColor(color, 1.1f) } - fun isColorLight(@ColorInt color: Int): Boolean { - return ColorUtils.calculateLuminance(color) >= 0.65 - } - @ColorInt fun invertColor(@ColorInt color: Int): Int { val r = 255 - Color.red(color) @@ -79,83 +77,6 @@ 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 - } - } - fun argb(R: Int, G: Int, B: Int): Int { return argb(Byte.MAX_VALUE.toInt(), R, G, B) } diff --git a/app/src/main/java/io/legado/app/utils/RandomColor.kt b/app/src/main/java/io/legado/app/utils/RandomColor.kt new file mode 100644 index 000000000..526afa6cc --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/RandomColor.kt @@ -0,0 +1,61 @@ +package io.legado.app.utils + +import android.graphics.Color +import java.util.* + +@Suppress("unused") +class RandomColor(alpha: Int, lower: Int, upper: Int) { + + constructor() : this(255, 80, 200) + + private var alpha: Int = 0 + private var lower: Int = 0 + private var upper: Int = 0 + + init { + require(upper > lower) { "must be lower < upper" } + setAlpha(alpha) + setLower(lower) + setUpper(upper) + } + + //随机数是前闭 后开 + fun build(): Int { + 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) + } + + 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 + } + +} \ No newline at end of file