A comprehensive guide to the CIE XYZ color space - the fundamental tristimulus color space that forms the mathematical foundation for all other color spaces, enabling accurate color conversions and serving as the reference standard for colorimetry and color management.
The CIE XYZ color space, established in 1931 by the International Commission on Illumination (CIE), is the fundamental tristimulus color space that forms the mathematical foundation for all modern color science. Based on the physiological response of the human visual system, XYZ provides a device-independent method for describing colors and serves as the intermediate color space for conversions between different color systems.
The XYZ color space is built on the principle that human color vision can be described using three values (tristimulus values) that correspond to the response of the eye's cone cells to light stimuli.
// XYZ color space implementation
class XYZColorSpace {
constructor() {
// Standard illuminants with XYZ white points
this.illuminants = {
'A': { X: 109.850, Y: 100.000, Z: 35.585, cct: 2856 },
'D50': { X: 96.422, Y: 100.000, Z: 82.521, cct: 5003 },
'D65': { X: 95.047, Y: 100.000, Z: 108.883, cct: 6504 },
'E': { X: 100.000, Y: 100.000, Z: 100.000, cct: 5454 }
};
// sRGB transformation matrices
this.sRGBMatrix = {
toXYZ: [
[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]
],
fromXYZ: [
[3.2404542, -1.5371385, -0.4985314],
[-0.9692660, 1.8760108, 0.0415560],
[0.0556434, -0.2040259, 1.0572252]
]
};
}
// Convert RGB to XYZ
rgbToXYZ(r, g, b) {
// Normalize and linearize RGB values
let rLin = this.srgbToLinear(r / 255);
let gLin = this.srgbToLinear(g / 255);
let bLin = this.srgbToLinear(b / 255);
// Apply transformation matrix
const [m1, m2, m3] = this.sRGBMatrix.toXYZ;
const X = (m1[0] * rLin + m1[1] * gLin + m1[2] * bLin) * 100;
const Y = (m2[0] * rLin + m2[1] * gLin + m2[2] * bLin) * 100;
const Z = (m3[0] * rLin + m3[1] * gLin + m3[2] * bLin) * 100;
return { X: Math.max(0, X), Y: Math.max(0, Y), Z: Math.max(0, Z) };
}
// Convert XYZ to RGB
xyzToRGB(X, Y, Z) {
// Normalize XYZ values
const xNorm = X / 100;
const yNorm = Y / 100;
const zNorm = Z / 100;
// Apply inverse transformation matrix
const [m1, m2, m3] = this.sRGBMatrix.fromXYZ;
let rLin = m1[0] * xNorm + m1[1] * yNorm + m1[2] * zNorm;
let gLin = m2[0] * xNorm + m2[1] * yNorm + m2[2] * zNorm;
let bLin = m3[0] * xNorm + m3[1] * yNorm + m3[2] * zNorm;
// Apply gamma correction
rLin = this.linearToSrgb(rLin);
gLin = this.linearToSrgb(gLin);
bLin = this.linearToSrgb(bLin);
return {
r: Math.max(0, Math.min(255, Math.round(rLin * 255))),
g: Math.max(0, Math.min(255, Math.round(gLin * 255))),
b: Math.max(0, Math.min(255, Math.round(bLin * 255)))
};
}
// Convert XYZ to LAB
xyzToLab(X, Y, Z, illuminant = 'D65') {
const wp = this.illuminants[illuminant];
const xn = X / wp.X;
const yn = Y / wp.Y;
const zn = Z / wp.Z;
const fx = this.labFunction(xn);
const fy = this.labFunction(yn);
const fz = this.labFunction(zn);
return {
L: Math.max(0, Math.min(100, 116 * fy - 16)),
a: Math.max(-128, Math.min(127, 500 * (fx - fy))),
b: Math.max(-128, Math.min(127, 200 * (fy - fz)))
};
}
// Convert LAB to XYZ
labToXYZ(L, a, b, illuminant = 'D65') {
const wp = this.illuminants[illuminant];
const fy = (L + 16) / 116;
const fx = a / 500 + fy;
const fz = fy - b / 200;
const xn = this.invLabFunction(fx);
const yn = this.invLabFunction(fy);
const zn = this.invLabFunction(fz);
return {
X: xn * wp.X,
Y: yn * wp.Y,
Z: zn * wp.Z
};
}
// Convert XYZ to chromaticity coordinates
xyzToChromaticity(X, Y, Z) {
const sum = X + Y + Z;
if (sum === 0) return { x: 0, y: 0, Y: 0 };
return { x: X / sum, y: Y / sum, Y: Y };
}
// Convert chromaticity coordinates to XYZ
chromaticityToXYZ(x, y, Y = 100) {
if (y === 0) return { X: 0, Y: 0, Z: 0 };
return {
X: (x * Y) / y,
Y: Y,
Z: ((1 - x - y) * Y) / y
};
}
// Chromatic adaptation between illuminants
chromaticAdaptation(xyz, sourceIlluminant, targetIlluminant) {
const sourceWP = this.illuminants[sourceIlluminant];
const targetWP = this.illuminants[targetIlluminant];
if (sourceIlluminant === targetIlluminant) return xyz;
// Bradford adaptation matrix
const bradfordMatrix = [
[0.8951, 0.2664, -0.1614],
[-0.7502, 1.7135, 0.0367],
[0.0389, -0.0685, 1.0296]
];
const inverseBradford = [
[0.9869929, -0.1470543, 0.1599627],
[0.4323053, 0.5183603, 0.0492912],
[-0.0085287, 0.0400428, 0.9684867]
];
// Convert white points to Bradford space
const sourceRGB = this.matrixMultiply(bradfordMatrix, [sourceWP.X, sourceWP.Y, sourceWP.Z]);
const targetRGB = this.matrixMultiply(bradfordMatrix, [targetWP.X, targetWP.Y, targetWP.Z]);
// Calculate adaptation matrix
const adaptMatrix = [
[targetRGB[0] / sourceRGB[0], 0, 0],
[0, targetRGB[1] / sourceRGB[1], 0],
[0, 0, targetRGB[2] / sourceRGB[2]]
];
// Apply adaptation
const colorRGB = this.matrixMultiply(bradfordMatrix, [xyz.X, xyz.Y, xyz.Z]);
const adaptedRGB = this.matrixMultiply(adaptMatrix, colorRGB);
const adaptedXYZ = this.matrixMultiply(inverseBradford, adaptedRGB);
return { X: adaptedXYZ[0], Y: adaptedXYZ[1], Z: adaptedXYZ[2] };
}
// Calculate tristimulus values from spectral data
spectralToXYZ(spectralData, illuminant = 'D65') {
const { wavelengths, reflectance } = spectralData;
let X = 0, Y = 0, Z = 0;
let normalizationFactor = 0;
for (let i = 0; i < wavelengths.length; i++) {
const wl = wavelengths[i];
const rho = reflectance[i];
const S = this.getIlluminantPower(illuminant, wl);
const xBar = this.getColorMatchingValue('x', wl);
const yBar = this.getColorMatchingValue('y', wl);
const zBar = this.getColorMatchingValue('z', wl);
const deltaWl = i < wavelengths.length - 1 ? wavelengths[i + 1] - wl : 5;
X += rho * S * xBar * deltaWl;
Y += rho * S * yBar * deltaWl;
Z += rho * S * zBar * deltaWl;
normalizationFactor += S * yBar * deltaWl;
}
const k = 100 / normalizationFactor;
return { X: X * k, Y: Y * k, Z: Z * k };
}
// Analyze XYZ color properties
analyzeXYZColor(X, Y, Z) {
const chromaticity = this.xyzToChromaticity(X, Y, Z);
const lab = this.xyzToLab(X, Y, Z);
return {
tristimulus: { X, Y, Z },
chromaticity: chromaticity,
luminance: Y,
lab: lab,
colorTemperature: this.estimateColorTemperature(chromaticity.x, chromaticity.y)
};
}
// Helper methods
srgbToLinear(value) {
return value <= 0.04045 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
}
linearToSrgb(value) {
return value <= 0.0031308 ? value * 12.92 : 1.055 * Math.pow(value, 1/2.4) - 0.055;
}
labFunction(t) {
const delta = 6 / 29;
return t > Math.pow(delta, 3) ? Math.pow(t, 1/3) : (t / (3 * delta * delta)) + (4 / 29);
}
invLabFunction(t) {
const delta = 6 / 29;
return t > delta ? Math.pow(t, 3) : 3 * delta * delta * (t - 4/29);
}
matrixMultiply(matrix, vector) {
return matrix.map(row => row.reduce((sum, val, i) => sum + val * vector[i], 0));
}
estimateColorTemperature(x, y) {
const n = (x - 0.3320) / (0.1858 - y);
const cct = 449 * Math.pow(n, 3) + 3525 * Math.pow(n, 2) + 6823.3 * n + 5520.33;
return Math.max(1000, Math.min(25000, cct));
}
getIlluminantPower(illuminant, wavelength) {
return 1.0; // Simplified - would need actual spectral data
}
getColorMatchingValue(component, wavelength) {
// Simplified approximation
if (component === 'y' && wavelength >= 500 && wavelength <= 600) {
return 1.0;
}
return 0.1;
}
}The X component roughly corresponds to the red-green opponent dimension and is derived from the red and green cone responses. It represents the mixture of cone responses that would be seen by the CIE standard observer.
The Y component represents luminance (brightness) and closely matches the photopic luminosity function of human vision. It's the most important component for perceived brightness.
The Z component roughly corresponds to the blue-yellow opponent dimension and is primarily derived from the blue cone response.
XYZ is based on color matching experiments where observers matched colors using three primary lights. The resulting color matching functions (x̄, ȳ, z̄) define how to calculate XYZ values from spectral data.
XYZ values are calculated by integrating the product of spectral reflectance, illuminant spectral power distribution, and color matching functions across the visible spectrum.
RGB colors are converted to XYZ using transformation matrices specific to each RGB color space. The process involves gamma correction and matrix multiplication.
XYZ serves as the intermediate step for LAB conversion, providing device-independent color representation before perceptual uniformity transformation.
Spectral reflectance data is converted to XYZ using the fundamental colorimetric equation involving color matching functions and illuminant data.
Explore these comprehensive pillar articles related to XYZ Color Space
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to triadic color schemes - three colors evenly spaced around the color wheel that create balanced, vibrant, and dynamic visual compositions with mathematical precision and artistic appeal.
Discover more about XYZ Color Space and related color theory concepts
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary