Skip to content

Instantly share code, notes, and snippets.

@rdb
Last active January 5, 2016 17:38
Show Gist options
  • Save rdb/218948a56493b21521ae to your computer and use it in GitHub Desktop.
Save rdb/218948a56493b21521ae to your computer and use it in GitHub Desktop.
Color temperature to linearized sRGB color
// Recalculate the color.
PN_stdfloat mm = 1000.0 / temperature;
PN_stdfloat mm2 = mm * mm;
PN_stdfloat mm3 = mm2 * mm;
PN_stdfloat x, y;
if (temperature < 4000) {
x = -0.2661239 * mm3 - 0.2343580 * mm2 + 0.8776956 * mm + 0.179910;
} else {
x = -3.0258469 * mm3 + 2.1070379 * mm2 + 0.2226347 * mm + 0.240390;
}
PN_stdfloat x2 = x * x;
PN_stdfloat x3 = x2 * x;
if (temperature < 2222) {
y = -1.1063814 * x3 - 1.34811020 * x2 + 2.18555832 * x - 0.20219683;
} else if (temperature < 4000) {
y = -0.9549476 * x3 - 1.37418593 * x2 + 2.09137015 * x - 0.16748867;
} else {
y = 3.0817580 * x3 - 5.87338670 * x2 + 3.75112997 * x - 0.37001483;
}
// xyY to XYZ, assuming Y=1.
LVecBase3 xyz(x / y, 1, (1 - x - y) / y);
// Convert XYZ to linearized sRGB.
const static LMatrix3 xyz_to_rgb(
3.2406255, -0.9689307, 0.0557101,
-1.537208, 1.8757561, -0.2040211,
-0.4986286, 0.0415175, 1.0569959);
LColor color(xyz_to_rgb.xform(xyz), 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment