34 lines
1.6 KiB
Plaintext
34 lines
1.6 KiB
Plaintext
#! Contains color temperature: the color of a black body's light at a
|
|||
|
|
#! temperature, as lamps' colors are named.
|
||
|
|
|
||
|
|
import std::math;
|
||
|
|
import malunal::linear;
|
||
|
|
module malunal::photometry;
|
||
|
|
|
||
|
|
#! The color of a black body's light at a temperature, in kelvin, as a lamp's
|
||
|
|
#! is named: a candle's about 1,900 K, a warm bulb's 2,700, daylight's 6,500,
|
||
|
|
#! and a blue sky's 10,000 and more. It's linear sRGB, its greatest channel 1,
|
||
|
|
#! as a light's color is, by its intensity. A temperature below 1,000 K, or past
|
||
|
|
#! 40,000, is taken as either.
|
||
|
|
#!
|
||
|
|
#! The color is of Krystek's fit of the Planckian locus, within a thousandth of
|
||
|
|
#! it from 1,000 K to 15,000, made sRGB of CIE 1931's XYZ, a channel past sRGB's
|
||
|
|
#! colors taken as none.
|
||
|
|
pub def color_of_temperature(kelvin: f32): vec3 {
|
||
|
|
val t := math::clamp(kelvin, 1000f, 40000f);
|
||
|
|
val tt := t * t;
|
||
|
|
# Where it lies in CIE 1960's u and v, then in 1931's x and y.
|
||
|
|
val u := (0.860117757 + 1.54118254e-4 * t + 1.28641212e-7 * tt) / (1 + 8.42420235e-4 * t + 7.08145163e-7 * tt);
|
||
|
|
val v := (0.317398726 + 4.22806245e-5 * t + 4.20481691e-8 * tt) / (1 - 2.89741816e-5 * t + 1.61456053e-7 * tt);
|
||
|
|
val d := 2 * u - 8 * v + 4;
|
||
|
|
val x := 3 * u / d;
|
||
|
|
val y := 2 * v / d;
|
||
|
|
# Of a brightness of 1.
|
||
|
|
val big_x := x / y;
|
||
|
|
val big_z := (1 - x - y) / y;
|
||
|
|
val r := math::max(3.2404542 * big_x - 1.5371385 - 0.4985314 * big_z, 0f);
|
||
|
|
val g := math::max(-0.9692660 * big_x + 1.8760108 + 0.0415560 * big_z, 0f);
|
||
|
|
val b := math::max(0.0556434 * big_x - 0.2040259 + 1.0572252 * big_z, 0f);
|
||
|
|
val most := math::max(r, math::max(g, b));
|
||
|
|
return vec3{ r / most, g / most, b / most };
|
||
|
|
}
|