object ColorUtils {
fun calculateGradientCenter(
color1: String,
color2: String,
percentage: String
): String {
val c1 = Color.parseColor(color2)
val c2 = Color.parseColor(color1)
val weight = percentage.removeSuffix("%").toDouble() / 100.0
val red = (Color.red(c1) * weight + Color.red(c2) * (1 - weight)).toInt()
val green = (Color.green(c1) * weight + Color.green(c2) * (1 - weight)).toInt()
val blue = (Color.blue(c1) * weight + Color.blue(c2) * (1 - weight)).toInt()
return String.format("#%02X%02X%02X", red, green, blue).toUpperCase()
}
}
val result1 = ColorUtils.calculateGradientCenter(
"
"
"50%"
) // 输出:
//这里注意 0 + (255 - 0) / 2 = 127.5 如果不四舍五入就是