From: Szymon Wilczek <[email protected]>
Date: 16 Nov 2026
Subject: [PATCH] Fix typos in the sRGB to CIE XYZ conversion
Merged: 17 Nov 2026, by Eli Zaretskii
signed, reported, fixes
- Author Szymon Wilczek author
- Merged-by Eli Zaretskii maintainer · merged it
diff
@@ -205,16 +205,16 @@ each element is between 0.0 and 1.0, inclusive." "Convert RED GREEN BLUE colors from the sRGB color space to CIE XYZ. RED, GREEN and BLUE should be between 0.0 and 1.0, inclusive." (let ((r (if (<= red 0.04045)- (/ red 12.95)+ (/ red 12.92) (expt (/ (+ red 0.055) 1.055) 2.4))) (g (if (<= green 0.04045)- (/ green 12.95)+ (/ green 12.92) (expt (/ (+ green 0.055) 1.055) 2.4))) (b (if (<= blue 0.04045)- (/ blue 12.95)+ (/ blue 12.92) (expt (/ (+ blue 0.055) 1.055) 2.4)))) (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))- (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))+ (+ (* 0.2126729 r) (* 0.7151522 g) (* 0.0721750 b)) (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b))))) (defun color-xyz-to-srgb (X Y Z) @@ -159,7 +159,7 @@ (should (color-tests--approx-equal (color-srgb-to-lab 0.1 0.2 0.3)- '(20.4762218 -0.6508996 -18.6340085))))+ '(20.4762439 -0.6509944 -18.6339706)))) (ert-deftest color-tests-lab-to-srgb () (should (equal (color-lab-to-srgb 0 0 0) '(0.0 0.0 0.0)))@@ -168,7 +168,7 @@ (color-lab-to-srgb 87.7347223 -86.1808176 83.1770651) '(0 1 0))) (should (color-tests--approx-equal- (color-lab-to-srgb 20.4762218 -0.6508996 -18.6340085)+ (color-lab-to-srgb 20.4762439 -0.6509944 -18.6339706) '(0.1 0.2 0.3)))) (ert-deftest color-tests-cie-de2000 ()@@ -178,13 +178,13 @@ (list (color-cie-de2000 (color-srgb-to-lab 1 0 0) (color-srgb-to-lab 0 0 1)))- '(52.8803934)))+ '(52.8811979))) (should (color-tests--approx-equal (list (color-cie-de2000 (color-srgb-to-lab 0.8 0 0) (color-srgb-to-lab 0.9 0 0)))- '(5.3844503))))+ '(5.3845329)))) (ert-deftest color-tests-clamp () (should (= (color-clamp 0) 0.0)) discussion · on the list
v1 · 2 messages
Szymon Wilczek author 16 Nov 2026
bug#82264: 31.1; [PATCH] color-srgb-to-xyz: wrong constants (12.95, 0.21266729)
`color-srgb-to-xyz' in lisp/color.el has two typos in its
constants
(present in Emacs 31.1 and on master):
1. The linear segment divides by 12.95:
(/ red 12.95)
The sRGB specification (IEC 61966-2-1) uses 12.92, and so does
the
inverse, `color-xyz-to-srgb' ((* 12.92 r)).
2. The red coefficient of Y is 0.21266729:
(+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
It should be 0.2126729, which matches the other matrix entries
(0.4124564, 0.3575761, ...) and the inverse matrix used in
`color-xyz-to-srgb'.
Because of this, white does not map to Y = 1 and the two functions
do
not round-trip. From emacs -Q:
(nth 1 (color-srgb-to-xyz 1.0 1.0 1.0))
=> 0.99999449 (expected 1.0)
(apply #'color-xyz-to-srgb (color-srgb-to-xyz 0.03 0.03 0.03))
=> (0.0299308 0.0299302 0.0299305) (expected 0.03 0.03 0.03)
(apply #'color-xyz-to-srgb (color-srgb-to-xyz 1.0 1.0 1.0))
=> (1.0000037 0.9999954 1.0000005)
With the attached patch the round trip is exact to about 1e-7, and
white gives Y = 1.0000001.
The errors propagate to `color-srgb-to-lab' and
`color-cie-de2000'.
The expected values in test/lisp/color-tests.el had been computed
with the typos, so the patch updates them too. I checked the new
L*a*b* value for sRGB (0.1 0.2 0.3) independently from the
specification, using the D65 white point from `color-d65-xyz'; it
agrees with the patched code.
I found this while computing relative luminance for a theme
package
(au-themes), which uses `color-srgb-to-xyz' for it.
Thanks,
Szymon Wilczek
Eli Zaretskii maintainer 17 Nov 2026
Thanks, I've now installed your patch on the master branch, and I'm
therefore closing this bug.