-
-
Save KatieBarnett/95ed365619d5846ba5cae38afcf4cf28 to your computer and use it in GitHub Desktop.
Pre-Android 12 detection on whether a color is dark or light from https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun colorIsDarkAdvanced(bgColor: Int): Boolean { | |
// hexToB | |
val uicolors = doubleArrayOf( | |
bgColor.red.toDouble() / 255.0, | |
bgColor.green.toDouble() / 255.0, | |
bgColor.blue.toDouble() / 255.0 | |
) | |
val c = uicolors.map { col -> | |
if (col <= 0.03928) { | |
col / 12.92 | |
} else { | |
Math.pow((col + 0.055) / 1.055, 2.4) | |
} | |
} | |
val L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2] | |
return L <= 0.179 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment