From 146c017e176238d8eaed57e61ec6a881380d55a4 Mon Sep 17 00:00:00 2001 From: Robert Ros Date: Wed, 11 Feb 2026 21:41:15 +0100 Subject: [PATCH] Change the implementation of getContrastColor to actually comparing the contrast of both foreground colors against the background color. --- .../main/kotlin/org/fossify/commons/extensions/Int.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt index 6dc9ce249..42de9253a 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -17,8 +17,14 @@ import kotlin.math.log10 import kotlin.math.pow fun Int.getContrastColor(): Int { - val luminance = ColorUtils.calculateLuminance(this) - return if (luminance > 0.5) DARK_GREY else Color.WHITE + return getContrastColor(DARK_GREY, Color.WHITE) +} + +fun Int.getContrastColor(colorFirst: Int, colorSecond: Int): Int { + val contrastFirst = ColorUtils.calculateContrast(colorFirst, this) + val contrastSecond = ColorUtils.calculateContrast(colorSecond, this) + + return if (contrastFirst >= contrastSecond) colorFirst else colorSecond } fun Int.toHex() = String.format("#%06X", 0xFFFFFF and this).uppercase(Locale.getDefault())