Skip to content

Instantly share code, notes, and snippets.

@yanzm
Last active October 5, 2024 07:31
Show Gist options
  • Save yanzm/2a7c378050cacf55a82a2078d1909e02 to your computer and use it in GitHub Desktop.
Save yanzm/2a7c378050cacf55a82a2078d1909e02 to your computer and use it in GitHub Desktop.
import kotlin.math.ln
import kotlin.math.pow
fun colorTemperatureToRGB(kelvin: Int): Triple<Float, Float, Float> {
val temp = kelvin / 100.0
val red: Double
val green: Double
val blue: Double
if (temp <= 66) {
red = 255.0
green = 99.4708025861 * ln(temp) - 161.1195681661
blue = if (temp <= 19) {
0.0
} else {
138.5177312231 * ln(temp - 10) - 305.0447927307
}
} else {
red = 329.698727446 * (temp - 60).pow(-0.1332047592)
green = 288.1221695283 * (temp - 60).pow(-0.0755148492)
blue = 255.0
}
return Triple(
red.toFloat().coerceIn(0f, 255f) / 255f,
green.toFloat().coerceIn(0f, 255f) / 255f,
blue.toFloat().coerceIn(0f, 255f) / 255f,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment