shiraji / color-manager

Android color manager plugin for Android Studio/Intellij
https://plugins.jetbrains.com/plugin/8583
Apache License 2.0
55 stars 2 forks source link

Change label color to white or black according to darkness of the background #41

Closed nishiyamaosamu closed 7 years ago

nishiyamaosamu commented 7 years ago

I love this plugin! I suggest to change label color to white or black according to darkness of the background. Please check below...

private fun setBackgroundAndColorCode(cell: ColorManagerToolWindowCell, colorManagerColorTag: ColorManagerColorTag) {
    val backgroundColor = colorManagerColorTag.color
    if (backgroundColor == null) {
        cell.rootPanel.background = Color.WHITE
        cell.colorCodeLabel.text = colorManagerColorTag.errorMessage ?: "'${colorManagerColorTag.tag.value.trimmedText}' not found"
    } else {
        cell.rootPanel.background = backgroundColor
        cell.colorCodeLabel.text = colorManagerColorTag.colorCode ?: "???"
        cell.colorCodeLabel.foreground = if isColorDark(backgroundColor) Color.WHITE else Color.BLACK
    }
}

private fun isColorDark(color: Int): boolean {
    // luma is calculated with the formula Y′ = 0.299 R′ + 0.587 G′ + 0.114 B′.
    // https://en.wikipedia.org/wiki/Luma_%28video%29
    val darkness = 1-(0.299*Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255
    if(darkness<0.5){
        return false
    }else{
        return true
    }
}

I hope this helps you.