Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Last active January 30, 2025 14:32

Revisions

  1. paulkaplan renamed this gist Mar 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tempToRGB → colorTempToRGB.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    // but I can't make any promises about the quality of the algorithm's estimates above 40000 K.)


    var tempToRGB = function(kelvin){
    function colorTemperatureToRGB(kelvin){

    var temp = kelvin / 100;

  2. paulkaplan created this gist Mar 18, 2013.
    61 changes: 61 additions & 0 deletions tempToRGB
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    // From http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/

    // Start with a temperature, in Kelvin, somewhere between 1000 and 40000. (Other values may work,
    // but I can't make any promises about the quality of the algorithm's estimates above 40000 K.)


    var tempToRGB = function(kelvin){

    var temp = kelvin / 100;

    var red, green, blue;

    if( temp <= 66 ){

    red = 255;

    green = temp;
    green = 99.4708025861 * Math.log(green) - 161.1195681661;


    if( temp <= 19){

    blue = 0;

    } else {

    blue = temp-10;
    blue = 138.5177312231 * Math.log(blue) - 305.0447927307;

    }

    } else {

    red = temp - 60;
    red = 329.698727446 * Math.pow(red, -0.1332047592);

    green = temp - 60;
    green = 288.1221695283 * Math.pow(green, -0.0755148492 );

    blue = 255;

    }


    return {
    r : clamp(red, 0, 255),
    g : clamp(green, 0, 255),
    b : clamp(blue, 0, 255)
    }

    }


    function clamp( x, min, max ) {

    if(x<min){ return min; }
    if(x>max){ return max; }

    return x;

    }