Как я в итоге сделал (фактически, решение описано
Сергей Соколов ):
object ColorHandler {
/**
* Color sample: 556A74
*/
fun getColorName(color: String): String {
if (color in names.keys)
return names[color]!!
val r = color.slice(0..1).toInt(16)
val g = color.slice(2..3).toInt(16)
val b = color.slice(4..5).toInt(16)
var currentMin: Pair<Int, String>? = null
var range: Int
names.forEach { (colorValue, name) ->
range = (
(r - colorValue.slice(0..1).toInt(16)).pow() +
(g - colorValue.slice(2..3).toInt(16)).pow() +
(b - colorValue.slice(4..5).toInt(16)).pow()
)
when {
currentMin == null -> {
currentMin = range to name
}
currentMin!!.first > range -> {
currentMin = range to name
}
currentMin!!.first < range -> {
return@forEach
}
}
}
return currentMin!!.second
}
}