# Blackbody curve to floating RGB

I want to set the color of the lamps in my game with a black body curve, but the approximations I can find for it are all in sRGB color space; and I want it in linear floating RGB.

The closest thing i was able to find is this: https://mathematica.stackexchange.com/questions/57389/convert-spectral-distribution-to-rgb-color

But i really don't understand most of the notation in the answer.

Mainly I'm unsure: if i just use planck's law, using the wavelengths of the subpixels in a standard monitor--not that I know what those are--will it get the right answer, or would it need to be an integral with respect to cone response.

Furthermore I'm not sure how to normalize the resulting radiation density from.... whatever units its in, to 1.0 = X nits; as I'm unsure of the conversion between the units...

• I saw your question in gamedev. I was going to post an answer, but the question was already deleted. Took me a while to find the question moved here. – Theraot Jan 29 at 15:18

The Open Shading Language has a complete implementation which is based on this source code from Color Rendering of Spectra by John Walker, according to the comments. I can't vouch for its theoretical correctness but I have used it and it appears to work reasonably well.

• I believe this is the same one used in blender; it produces incorrect results below 2000C if i remember right? at least when i tried to use blender's black body node to color lava the results were very incorrect. but thank you though! – Patrick Jeeves Feb 8 at 17:12
• @PatrickJeeves I don't know about blender but this one looks "ok" even at low temperatures. It fades to orange, then red. The dynamic range is extreme but I think that is correct; picture a glowing piece of steel vs an arc welder flash. – Olivier Feb 9 at 18:45

The code below is based on the article How to Convert Temperature (K) to RGB: Algorithm and Sample Code with Neil Bartlett modifications from the article Color Temperature Conversion Of Homestar.io.

Bartlett has released a javascript library neilbartlett/color-temperature which computes RGB from kelvin with the color components in the range 0 to 255. The code below is adjusted for RGB components in the range 0.0 to 1.0.

float t = kelvin / 100.0;
float x;
float red;
float green;
float blue;
if (t <= 66)
{
red = 1.0;
}
else
{
x = t - 55.0;
red = clamp(
1.38030159086 + 0.00044786845 * x - 0.15785750233 * log(x),
0.0,
1.0);
}
if (t <= 10)
{
green = 0.0;
}
else if (t <= 66)
{
x = t - 2.0;
green = clamp(
-0.60884257109 - 0.00174890002 * x + 0.40977318429 * log(x),
0.0,
1.0);
}
else
{
x = t - 50.0;
green = clamp(
1.27627220616 + 0.0003115081 * x - 0.11013841706 * log(x),
0.0,
1.0);
}
if (t <= 20)
{
blue = 0.0;
}
else if (t <= 66)
{
x = t - 10.0;
blue = clamp(
-0.99909549742 + 0.00324474355 * x + 0.45364683926 * log(x),
0.0,
1.0);
}
else
{
blue = 1.0;
}


Note: log refers to natural log.

• which is in sRGB clamped to 0-255; i want it unclamped in linear space; floating point color can have values above 1.0. – Patrick Jeeves Jan 29 at 15:53