Skip to content

Commit

Permalink
feat: 调整颜色生成算法,限制明度与饱和度,避免过于明亮的主题色
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-XiaoLiang committed Jun 13, 2023
1 parent 674a355 commit 9b056f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lQR/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "liang.lollipop.lqrdemo"
minSdk 27
targetSdk 33
versionCode 2_00_10
versionName "2.0.10"
versionCode 2_00_11
versionName "2.0.11"
}

buildTypes {
Expand Down
29 changes: 27 additions & 2 deletions pigment/src/main/java/com/lollipop/pigment/BlendMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lollipop.pigment

import android.graphics.Color
import androidx.core.graphics.ColorUtils
import com.lollipop.pigment.BlendMode.Dark.limitColor


sealed class BlendMode {
Expand Down Expand Up @@ -34,6 +35,25 @@ sealed class BlendMode {
hsl[1] = 0.1F
return ColorUtils.HSLToColor(hsl)
}

fun limit(color: Int, maxS: Float, minS: Float, maxL: Float, minL: Float): Int {
// [H, S, L]
val hsl = FloatArray(3)
ColorUtils.colorToHSL(color, hsl)
hsl[1] = limit(target = hsl[1], max = maxS, min = minS)
hsl[2] = limit(target = hsl[2], max = maxL, min = minL)
return ColorUtils.HSLToColor(hsl)
}

private fun limit(target: Float, max: Float, min: Float): Float {
if (target < min) {
return min
}
if (target > max) {
return max
}
return target
}
}

abstract fun original(src: Int): Int
Expand All @@ -46,9 +66,14 @@ sealed class BlendMode {

abstract fun background(src: Int): Int

protected fun Int.limitColor(): Int {
val color = this
return limit(color, 0.5F, 0.2F, 0.6F, 0.2F)
}

object Light : BlendMode() {
override fun original(src: Int): Int {
return src
return blend(src.limitColor(), Color.WHITE, 0.3F)
}

override fun variant(src: Int): Int {
Expand All @@ -71,7 +96,7 @@ sealed class BlendMode {

object Dark : BlendMode() {
override fun original(src: Int): Int {
return blend(src, Color.BLACK, 0.5F)
return blend(src.limitColor(), Color.BLACK, 0.5F)
}

override fun variant(src: Int): Int {
Expand Down

0 comments on commit 9b056f5

Please sign in to comment.