Skip to content

temperature - Color Temperature ​

Color temperature read/write support. Color temperature is expressed in Kelvin (K), ranging from 1000–40000K. Low color temperatures are warm (orange-red), while high color temperatures are cool (blue-white).

Install ​

ts
import xcolor from '@xpyjs/color'
import temperaturePlugin from '@xpyjs/color/plugins/temperature'

xcolor.use(temperaturePlugin)

Methods ​

Color Temperature Spectrum (K)1000K2700K4000K5500K6500K10000K20000K40000KWarmCool

temperature() ​

Getter/setter mode. Without arguments, returns the approximate color temperature (K) of the current color; with an argument, sets the color based on the color temperature.

Read Color Temperature ​

ts
xcolor('#ff8000').temperature()   // ≈ 1800 (warm light)
xcolor('#ffffff').temperature()   // 6500 (D65 daylight neutral)
xcolor('#80b0ff').temperature()   // > 10000 (cool light)

Set Color Temperature ​

ts
xcolor('#000000').temperature(2700).toHex()  // '#ffa757' (warm white)
xcolor('#000000').temperature(5500).toHex()  // daylight
xcolor('#000000').temperature(9000).toHex()  // cool blue light

Return value: In getter mode returns number (K), in setter mode returns this.

Common Color Temperature Reference ​

Color Temperature (K)Light Source
1850Candle
2700Incandescent lamp
4000Fluorescent lamp
5500Noon daylight
6500Overcast / D65 standard illuminant
9000+Blue sky

Practical Scenarios ​

Simulate Sunrise to Sunset ​

ts
const daylight = [2000, 3500, 5500, 7000, 4000, 2500]
const palette = daylight.map(k => xcolor('#fff').temperature(k).toHex())

Released under the MIT License.