<p>The <a href="https://github.com/imageworks/OpenShadingLanguage/" rel="nofollow noreferrer">Open Shading Language</a> has <a href="https://github.com/imageworks/OpenShadingLanguage/blob/master/src/liboslexec/opcolor.cpp#L785" rel="nofollow noreferrer">a complete implementation</a> which is based on <a href="http://www.fourmilab.ch/documents/specrend/specrend.c" rel="nofollow noreferrer">this source code</a> from <a href="https://www.fourmilab.ch/documents/specrend/" rel="nofollow noreferrer">Color Rendering of Spectra</a> 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.</p>

CC BY-SA 4.0
2020-02-08T17:12:25.677Z
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
CC BY-SA 4.0
2020-02-09T18:45:20.497Z
@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
CC BY-SA 4.0
2020-01-29T18:16:23.403Z
2020-01-29T18:16:23.403Z
Olivier
<p>The code below is based on the article <a href="http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code" rel="nofollow noreferrer">How to Convert Temperature (K) to RGB: Algorithm and Sample Code</a> with Neil Bartlett modifications from the article <a href="http://www.zombieprototypes.com/?p=210" rel="nofollow noreferrer">Color Temperature Conversion Of Homestar.io</a>.</p>

<p>Bartlett has released a javascript library <a href="https://github.com/neilbartlett/color-temperature" rel="nofollow noreferrer"><code>neilbartlett/color-temperature</code></a> which computes RGB from kelvin with the color components in the range <code>0</code> to <code>255</code>. The code below is adjusted for RGB components in the range <code>0.0</code> to <code>1.0</code>.</p>

<pre><code>float t = kelvin / 100.0;
float x;
float red;
float green;
float blue;
if (t &lt;= 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 &lt;= 10)
{
    green = 0.0;
}
else if (t &lt;= 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 &lt;= 20)
{
    blue = 0.0;
}
else if (t &lt;= 66)
{
    x = t - 10.0;
    blue = clamp(
        -0.99909549742 + 0.00324474355 * x + 0.45364683926 * log(x),
        0.0,
        1.0);
}
else
{
    blue = 1.0;
}
</code></pre>

<p><strong>Note</strong>: <code>log</code> refers to natural log.</p>

CC BY-SA 4.0
2020-01-29T15:53:21.713Z
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
CC BY-SA 4.0
2020-01-29T15:17:22.400Z
2020-01-29T15:17:22.400Z
Theraot
<p>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.</p>

<p>The closest thing i was able to find is this: <a href="https://mathematica.stackexchange.com/questions/57389/convert-spectral-distribution-to-rgb-color">https://mathematica.stackexchange.com/questions/57389/convert-spectral-distribution-to-rgb-color</a></p>

<p>But i really don't understand most of the notation in the answer.</p>

<p>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.</p>

<p>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...</p>

CC BY-SA 4.0
2020-01-29T15:18:48.137Z
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
<physics>
Blackbody curve to floating RGB
Patrick Jeeves
# Blackbody curve to floating RGB

(Asked by: username_0 on 2020-01-29)

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...

**Tags:** physics

> **Comments:**
> **username_1**: 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.  

---

## 2 answers

### Answer by username_2 on 2020-01-29. Score: 2

The [Open Shading Language](https://github.com/imageworks/OpenShadingLanguage/) has [a complete implementation](https://github.com/imageworks/OpenShadingLanguage/blob/master/src/liboslexec/opcolor.cpp#L785) which is based on [this source code](http://www.fourmilab.ch/documents/specrend/specrend.c) from [Color Rendering of Spectra](https://www.fourmilab.ch/documents/specrend/) 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.

> **Comments:**
> **username_0**: 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!  
> **username_2**: @username_0 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.  

---

### Answer by username_1 on 2020-01-29. Score: 0

The code below is based on the article [How to Convert Temperature (K) to RGB: Algorithm and Sample Code](http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code) with Neil Bartlett modifications from the article [Color Temperature Conversion Of Homestar.io](http://www.zombieprototypes.com/?p=210).

Bartlett has released a javascript library [`neilbartlett/color-temperature`](https://github.com/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.

> **Comments:**
> **username_0**: which is in sRGB clamped to 0-255; i want it unclamped in linear space; floating point color can have values above 1.0.  

---

