function XYZToColorTemperature(X, Y, Z: Double): Double;
var
n, u, v, duv: Double;
begin
// Normalize XYZ values
n := X + Y + Z;
if (n = 0) then
Exit(0);
X := X / n;
Y := Y / n;
Z := Z / n;
// Calculate u and v values
u := (4 * X) / (-2 * X + 12 * Y + 3);
v := (9 * Y) / (-2 * X + 12 * Y + 3);
// Calculate duv (distance from Planckian locus)
duv := 0;
if (u - 0.20917 * v) > 0.46228 then
duv := (u - 0.20917 * v - 0.46228) / 0.02
else if ((0.08067 * v - u) > 0.17697) then
duv := (0.08067 * v - u - 0.17697) / 0.02;
// Calculate color temperature
Result := (1.0 / ((-4.607 * duv + 2.9672) * 1e-3) + 1.0 / 500) * 1e3;
end;
留言