Created
May 13, 2023 16:17
-
-
Save Tyrrrz/fb6fb3e83c5aff161945f235671655f8 to your computer and use it in GitHub Desktop.
Color temperature to RGB multipliers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Algorithm taken from http://tannerhelland.com/4435/convert-temperature-rgb-algorithm-code | |
private static double GetRed(double temperature) | |
{ | |
if (temperature > 6600) | |
{ | |
return Math.Clamp( | |
Math.Pow(temperature / 100 - 60, -0.1332047592) * 329.698727446 / 255, | |
0, 1 | |
); | |
} | |
return 1; | |
} | |
private static double GetGreen(double temperature) | |
{ | |
if (temperature > 6600) | |
{ | |
return Math.Clamp( | |
Math.Pow(temperature / 100 - 60, -0.0755148492) * 288.1221695283 / 255, | |
0, 1 | |
); | |
} | |
return Math.Clamp( | |
(Math.Log(temperature / 100) * 99.4708025861 - 161.1195681661) / 255, | |
0, 1 | |
); | |
} | |
private static double GetBlue(double temperature) | |
{ | |
if (temperature >= 6600) | |
return 1; | |
if (temperature <= 1900) | |
return 0; | |
return Math.Clamp( | |
(Math.Log(temperature / 100 - 10) * 138.5177312231 - 305.0447927307) / 255, | |
0, 1 | |
); | |
} | |
public static (double red, double green, double blue) ColorTemperatureToRgbMultipliers(double temperature) => | |
(GetRed(temperature), GetGreen(temperature), GetBlue(temperature)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment