Skip to content

Commit d0577f8

Browse files
authored
fix: calculate the better contrasting color (#308)
Refs: FossifyOrg/Calendar#992
1 parent 4373d1c commit d0577f8

File tree

1 file changed

+8
-2
lines changed
  • commons/src/main/kotlin/org/fossify/commons/extensions

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ import kotlin.math.log10
1717
import kotlin.math.pow
1818

1919
fun Int.getContrastColor(): Int {
20-
val luminance = ColorUtils.calculateLuminance(this)
21-
return if (luminance > 0.5) DARK_GREY else Color.WHITE
20+
return getContrastColor(DARK_GREY, Color.WHITE)
21+
}
22+
23+
fun Int.getContrastColor(colorFirst: Int, colorSecond: Int): Int {
24+
val contrastFirst = ColorUtils.calculateContrast(colorFirst, this)
25+
val contrastSecond = ColorUtils.calculateContrast(colorSecond, this)
26+
27+
return if (contrastFirst >= contrastSecond) colorFirst else colorSecond
2228
}
2329

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

0 commit comments

Comments
 (0)