Skip to content

Commit 252b67f

Browse files
authored
fix: switch back to manual threshold based contrast calculation (#316)
Refs: FossifyOrg/Calendar#1065
1 parent 827c7ee commit 252b67f

2 files changed

Lines changed: 2 additions & 47 deletions

File tree

commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,16 @@ import android.os.Looper
99
import androidx.core.graphics.ColorUtils
1010
import androidx.core.os.postDelayed
1111
import org.fossify.commons.helpers.DARK_GREY
12-
import org.fossify.commons.helpers.MAX_ALPHA_INT
1312
import org.fossify.commons.helpers.WCAG_AA_NORMAL
14-
import org.fossify.commons.helpers.LUMINANCE_OFFSET
1513
import java.text.DecimalFormat
1614
import java.util.Locale
1715
import java.util.Random
1816
import kotlin.math.log10
1917
import kotlin.math.pow
20-
import kotlin.math.sqrt
2118

2219
fun Int.getContrastColor(): Int {
23-
return getContrastColor(DARK_GREY, Color.WHITE)
24-
}
25-
26-
fun Int.getContrastColor(firstColor: Int, secondColor: Int): Int {
27-
// Opaque background
28-
if (Color.alpha(this) == MAX_ALPHA_INT) {
29-
val contrastFirstColor = ColorUtils.calculateContrast(firstColor, this)
30-
val contrastSecondColor = ColorUtils.calculateContrast(secondColor, this)
31-
32-
return if (contrastFirstColor >= contrastSecondColor) firstColor else secondColor
33-
}
34-
35-
// Translucent background: fallback heuristic
36-
val luminanceBackground = ColorUtils.calculateLuminance(this)
37-
val luminanceFirstColor = ColorUtils.calculateLuminance(firstColor)
38-
val luminanceSecondColor = ColorUtils.calculateLuminance(secondColor)
39-
40-
val lightColor: Int
41-
val darkColor: Int
42-
val luminanceLight: Double
43-
val luminanceDark: Double
44-
45-
if (luminanceFirstColor >= luminanceSecondColor) {
46-
lightColor = firstColor
47-
darkColor = secondColor
48-
luminanceLight = luminanceFirstColor
49-
luminanceDark = luminanceSecondColor
50-
} else {
51-
lightColor = secondColor
52-
darkColor = firstColor
53-
luminanceLight = luminanceSecondColor
54-
luminanceDark = luminanceFirstColor
55-
}
56-
57-
// Compute crossover luminance where both candidates have equal WCAG contrast
58-
// against a (hypothetical) opaque background of luminance L:
59-
// (L + 0.05)^2 = (lLight + 0.05) * (lDark + 0.05)
60-
val threshold = sqrt((luminanceLight + LUMINANCE_OFFSET) * (luminanceDark + LUMINANCE_OFFSET)) - LUMINANCE_OFFSET
61-
62-
// If background is lighter than the threshold -> choose darker foreground,
63-
// else choose lighter foreground.
64-
return if (luminanceBackground >= threshold) darkColor else lightColor
20+
val luminance = ColorUtils.calculateLuminance(this)
21+
return if (luminance > 0.5) DARK_GREY else Color.WHITE
6522
}
6623

6724
fun Int.toHex() = String.format("#%06X", 0xFFFFFF and this).uppercase(Locale.getDefault())

commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ const val MAX_ALPHA_INT = 255
6565
const val WCAG_AA_NORMAL = 4.5
6666
const val WCAG_AA_LARGE = 3.0
6767

68-
const val LUMINANCE_OFFSET = 0.05
69-
7068
const val HOUR_MINUTES = 60
7169
const val DAY_MINUTES = 24 * HOUR_MINUTES
7270
const val WEEK_MINUTES = DAY_MINUTES * 7

0 commit comments

Comments
 (0)