rgb function in Colors.kt translates float colours incorrectly.
The rgb
function in Colors.kt
translates float colours incorrectly.
If provided with the float 0.999
, it will compute the RGB color to be 254. This is incorrect. As a float it is computed to be 254.745, which should be rounded to 255, not 254.
This happens because the toInt
function does not round and just truncates the decimal.
Either use
fun rgb(red: Double, green: Double, blue: Double): Color {
require(red in 0.0F..1.0F) { "$red is not between 0 and 1." }
require(green in 0.0F..1.0F) { "$green is not between 0 and 1." }
require(blue in 0.0F..1.0F) { "$blue is not between 0 and 1." }
return rgb((red * 255).roundToInt(), (green * 255).roundToInt(), (blue * 255).roundToInt())
}
or
fun rgb(red: Double, green: Double, blue: Double): Color {
require(red in 0.0F..1.0F) { "$red is not between 0 and 1." }
require(green in 0.0F..1.0F) { "$green is not between 0 and 1." }
require(blue in 0.0F..1.0F) { "$blue is not between 0 and 1." }
return rgb((red * 255 + 0.5).toInt(), (green * 255 + 0.5).toInt(), (blue * 255 + 0.5).toInt())
}
to fix this.
Edited by solonovamax