Convert sRGB to CIE XYZ
CIE XYZ is the master color space from which nearly all perceptual color math descends. Converting an sRGB or hex value into XYZ tristimulus values is the first step toward Lab, Luv, color-difference (Delta-E) calculations, and device-independent color management. This converter handles the gamma linearization and the standard D65 matrix for you, and also reports chromaticity coordinates.
How it works
Each sRGB channel is first scaled to 0–1 and linearized: values at or below 0.04045 are divided by 12.92, and higher values follow ((c + 0.055) / 1.055) ^ 2.4. The linear R, G, B vector is then multiplied by the standard sRGB-to-XYZ matrix for the D65 white point:
X = 0.4124564*R + 0.3575761*G + 0.1804375*B
Y = 0.2126729*R + 0.7151522*G + 0.0721750*B
Z = 0.0193339*R + 0.1191920*G + 0.9503041*B
The result is multiplied by 100 so the reference white sits at Y = 100.
Example and notes
White #ffffff converts to approximately X = 95.05, Y = 100.00, Z = 108.88 — exactly the D65 reference white. Note that Y is luminance, so two colors with the same Y have equal brightness. The chromaticity pair x = X/(X+Y+Z) and y = Y/(X+Y+Z) strips out brightness entirely, which is why it is plotted on the familiar horseshoe-shaped CIE 1931 diagram. To feed these values into a Lab conversion, keep them on the Y = 100 scale.