fork download
  1. // Colour temperature to RGB: prints the approximate sRGB white point for 1000 K to 6500 K in 100 K steps.
  2. //
  3. // Algorithm: Tanner Helland, "How to Convert Temperature (K) to RGB: Algorithm and Sample Code" (2012),
  4. // a curve fit to Mitchell Charity's blackbody colour data. It is an approximation for UI work and
  5. // previews, not a colorimetric conversion.
  6. //
  7. // Built by the makers of Sundown for Mac (https://t...content-available-to-author-only...n.com). To compare common night settings,
  8. // try the Blue Light Calculator: https://t...content-available-to-author-only...n.com/tools/blue-light-calculator
  9.  
  10. function kelvinToRgb(kelvin) {
  11. const t = kelvin / 100;
  12. let r, g, b;
  13.  
  14. if (t <= 66) {
  15. r = 255;
  16. g = 99.4708025861 * Math.log(t) - 161.1195681661;
  17. } else {
  18. r = 329.698727446 * Math.pow(t - 60, -0.1332047592);
  19. g = 288.1221695283 * Math.pow(t - 60, -0.0755148492);
  20. }
  21.  
  22. if (t >= 66) {
  23. b = 255;
  24. } else if (t <= 19) {
  25. b = 0;
  26. } else {
  27. b = 138.5177312231 * Math.log(t - 10) - 305.0447927307;
  28. }
  29.  
  30. const clamp = (v) => Math.round(Math.min(255, Math.max(0, v)));
  31. return [clamp(r), clamp(g), clamp(b)];
  32. }
  33.  
  34. function toHex([r, g, b]) {
  35. return "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("").toUpperCase();
  36. }
  37.  
  38. console.log("Kelvin Hex R G B");
  39. for (let k = 1000; k <= 6500; k += 100) {
  40. const rgb = kelvinToRgb(k);
  41. console.log(
  42. String(k).padEnd(8) + " " + toHex(rgb) + " " + rgb.map((v) => String(v).padStart(3)).join(" ")
  43. );
  44. }
  45.  
Success #stdin #stdout 0.06s 43656KB
stdin
Standard input is empty
stdout
Kelvin   Hex       R    G    B
1000     #FF4400  255   68    0
1100     #FF4D00  255   77    0
1200     #FF5600  255   86    0
1300     #FF5E00  255   94    0
1400     #FF6500  255  101    0
1500     #FF6C00  255  108    0
1600     #FF7300  255  115    0
1700     #FF7900  255  121    0
1800     #FF7E00  255  126    0
1900     #FF8400  255  132    0
2000     #FF890E  255  137   14
2100     #FF8E1B  255  142   27
2200     #FF9227  255  146   39
2300     #FF9732  255  151   50
2400     #FF9B3D  255  155   61
2500     #FF9F46  255  159   70
2600     #FFA34F  255  163   79
2700     #FFA757  255  167   87
2800     #FFAA5F  255  170   95
2900     #FFAE67  255  174  103
3000     #FFB16E  255  177  110
3100     #FFB475  255  180  117
3200     #FFB87B  255  184  123
3300     #FFBB81  255  187  129
3400     #FFBE87  255  190  135
3500     #FFC18D  255  193  141
3600     #FFC392  255  195  146
3700     #FFC697  255  198  151
3800     #FFC99D  255  201  157
3900     #FFCBA1  255  203  161
4000     #FFCEA6  255  206  166
4100     #FFD0AB  255  208  171
4200     #FFD3AF  255  211  175
4300     #FFD5B3  255  213  179
4400     #FFD7B7  255  215  183
4500     #FFDABB  255  218  187
4600     #FFDCBF  255  220  191
4700     #FFDEC3  255  222  195
4800     #FFE0C7  255  224  199
4900     #FFE2CA  255  226  202
5000     #FFE4CE  255  228  206
5100     #FFE6D1  255  230  209
5200     #FFE8D5  255  232  213
5300     #FFEAD8  255  234  216
5400     #FFECDB  255  236  219
5500     #FFEDDE  255  237  222
5600     #FFEFE1  255  239  225
5700     #FFF1E4  255  241  228
5800     #FFF3E7  255  243  231
5900     #FFF4EA  255  244  234
6000     #FFF6ED  255  246  237
6100     #FFF8F0  255  248  240
6200     #FFF9F2  255  249  242
6300     #FFFBF5  255  251  245
6400     #FFFDF8  255  253  248
6500     #FFFEFA  255  254  250