Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import android.graphics.Color
import android.media.ExifInterface
import android.os.Handler
import android.os.Looper
import androidx.core.graphics.ColorUtils
import androidx.core.os.postDelayed
import org.fossify.commons.helpers.DARK_GREY
import org.fossify.commons.helpers.WCAG_AA_NORMAL
import java.text.DecimalFormat
import java.util.Locale
import java.util.Random

fun Int.getContrastColor(): Int {
val y = (299 * Color.red(this) + 587 * Color.green(this) + 114 * Color.blue(this)) / 1000
return if (y >= 149 && this != Color.BLACK) DARK_GREY else Color.WHITE
val luminance = ColorUtils.calculateLuminance(this)
return if (luminance > 0.5) DARK_GREY else Color.WHITE
}

fun Int.toHex() = String.format("#%06X", 0xFFFFFF and this).uppercase(Locale.getDefault())
Expand Down Expand Up @@ -192,11 +194,22 @@ fun Int.countdown(intervalMillis: Long, callback: (count: Int) -> Unit) {
}
}

fun Int.adjustSimColorForBackground(bg: Int): Int {
val hsv = FloatArray(3)
Color.colorToHSV(bg, hsv)
if (hsv[2] < 0.5) {
return this.lightenColor(24)
fun Int.adjustForContrast(
background: Int,
minContrast: Double = WCAG_AA_NORMAL,
): Int {
var color = this
var alpha = 0f
val target = if (ColorUtils.calculateLuminance(background) < 0.5) {
Color.WHITE
} else {
Color.BLACK
}

while (ColorUtils.calculateContrast(color, background) < minContrast && alpha < 1f) {
alpha += 0.05f
color = ColorUtils.blendARGB(this, target, alpha)
}
return this

return color
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const val HIGHER_ALPHA = 0.75f
const val LOWER_ALPHA_INT = 30
const val MEDIUM_ALPHA_INT = 90

const val WCAG_AA_NORMAL = 4.5
const val WCAG_AA_LARGE = 3.0

const val HOUR_MINUTES = 60
const val DAY_MINUTES = 24 * HOUR_MINUTES
const val WEEK_MINUTES = DAY_MINUTES * 7
Expand Down
Loading