@@ -9,59 +9,16 @@ import android.os.Looper
99import androidx.core.graphics.ColorUtils
1010import androidx.core.os.postDelayed
1111import org.fossify.commons.helpers.DARK_GREY
12- import org.fossify.commons.helpers.MAX_ALPHA_INT
1312import org.fossify.commons.helpers.WCAG_AA_NORMAL
14- import org.fossify.commons.helpers.LUMINANCE_OFFSET
1513import java.text.DecimalFormat
1614import java.util.Locale
1715import java.util.Random
1816import kotlin.math.log10
1917import kotlin.math.pow
20- import kotlin.math.sqrt
2118
2219fun 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
6724fun Int.toHex () = String .format(" #%06X" , 0xFFFFFF and this ).uppercase(Locale .getDefault())
0 commit comments