Skip to content

Instantly share code, notes, and snippets.

@byValle
Last active December 24, 2023 07:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save byValle/b81391dcb2edaef9c651132ab3f123a9 to your computer and use it in GitHub Desktop.
Save byValle/b81391dcb2edaef9c651132ab3f123a9 to your computer and use it in GitHub Desktop.
RGB color from light K temperature - Port to C++ for Unreal Engine of https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html
//Temperature input in Kelvin valid in the range 1000 K to 40000 K. White light = 6500K
int Temperature = 6500;
//Temperature divided by 100 for calculations
float KtoRGB_Temperature = Temperature / 100;
//RGB components
float KtoRGB_Red;
float KtoRGB_Green;
float KtoRGB_Blue;
//Final result as an RGB Linear Color
FLinearColor KtoRGB;
//RED
if (KtoRGB_Temperature <= 66)
{
KtoRGB_Red = 255;
}
else
{
KtoRGB_Red = 329.698727446 * pow(KtoRGB_Temperature - 60, -0.1332047592);
}
if (KtoRGB_Red < 0)
{
KtoRGB_Red = 0;
}
if (KtoRGB_Red > 255)
{
KtoRGB_Red = 255;
}
//GREEN
if (KtoRGB_Temperature <= 66)
{
KtoRGB_Green = 99.4708025861 * log(KtoRGB_Temperature) - 161.1195681661;
}
else
{
KtoRGB_Green = 288.1221695283 * pow(KtoRGB_Temperature - 60, -0.0755148492);
}
if (KtoRGB_Green < 0)
{
KtoRGB_Green = 0;
}
if (KtoRGB_Green > 255)
{
KtoRGB_Green = 255;
}
//BLUE
if (KtoRGB_Temperature >= 66)
{
KtoRGB_Blue = 255;
}
else
{
if (KtoRGB_Temperature <= 19)
{
KtoRGB_Blue = 0;
}
else
{
KtoRGB_Blue = 138.5177312231 * log(KtoRGB_Temperature - 10) - 305.0447927307;
}
}
if (KtoRGB_Blue < 0)
{
KtoRGB_Blue = 0;
}
if (KtoRGB_Blue > 255)
{
KtoRGB_Blue = 255;
}
//FINAL RESULT AS FLINEARCOLOR 0-1
KtoRGB = FLinearColor(KtoRGB_Red / 255, KtoRGB_Green / 255, KtoRGB_Blue / 255, 1.0f);
//Kelvin Temperature to RGB | Credit to Tanner Helland for the base algorithm
//Port to C++ for Unreal Engine by Jorge Valle Hurtado - byValle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment