Szymon Wilczek

research / emacs-mirror/emacs / 6eb054325e3b

Fix typos in the sRGB to CIE XYZ conversion

emacs-mirror/emacs

+8 −8

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

* lisp/color.el (color-srgb-to-xyz): Divide the linear segment by 12.92, not 12.95, and use 0.2126729, not 0.21266729, as the red coefficient of Y, as the sRGB specification and the inverse conversion in 'color-xyz-to-srgb' do. * test/lisp/color-tests.el (color-tests-srgb-to-lab) (color-tests-lab-to-srgb, color-tests-cie-de2000): Update the expected values, which had been computed with the typos. (Bug#82264)

signed, reported, fixes

  • Author Szymon Wilczek author
  • Merged-by Eli Zaretskii maintainer · merged it

diff

lisp/color.el +4 −4
@@ -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)
test/lisp/color-tests.el +4 −4
@@ -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
applied

Eli Zaretskii maintainer 17 Nov 2026

Thanks, I've now installed your patch on the master branch, and I'm therefore closing this bug.
the whole thread on yhetil.org
18/18
salina
aurum
herbarium
mysterium
parchment
perseida
pluvia
whispergrove
yerba
M-x 0/0

keybindings & navigation

M-x / Alt+x Open Emacs command runner
Shift + W Jump to writings & essays (/writings)
Shift + R Jump to research & upstream work (/research)
Shift + T Choose colour theme (au-themes-select)
j / k Scroll down / up
g g Scroll to the top of page
Shift + G Scroll to the bottom of page
? Toggle this keybindings help cheat sheet
Esc / C-g Dismiss modal or close active menu