aboutsummaryrefslogtreecommitdiff
path: root/compat-30.el
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2025-03-30 01:25:25 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2025-03-30 01:30:59 +0100
commit8d896bf8f7542b14480c005e8ed5e87e06a506fc (patch)
tree1791e59f45c774a9fe1484fad4ae91204c1e7bfb /compat-30.el
parentc89bba6524501bde03db6266a3ac47b266b81e02 (diff)
compat-30: Add oklab color functions
Co-authored-by: Elijah Gabe Pérez <eg642616@gmail.com>
Diffstat (limited to 'compat-30.el')
-rw-r--r--compat-30.el44
1 files changed, 44 insertions, 0 deletions
diff --git a/compat-30.el b/compat-30.el
index cb6e442..2decb93 100644
--- a/compat-30.el
+++ b/compat-30.el
@@ -215,6 +215,50 @@ details."
(funcall completion-lazy-hilit-fn (copy-sequence str))
str))
+;;;; Defined in color.el
+
+(compat-defun color-oklab-to-xyz (l a b) ;; <compat-tests:color-oklab-to-xyz>
+ "Convert the OkLab color represented by L A B to CIE XYZ.
+Oklab is a perceptual color space created by Björn Ottosson
+<https://bottosson.github.io/posts/oklab/>. It has the property that
+changes in the hue and saturation of a color can be made while maintaining
+the same perceived lightness."
+ :feature color
+ (let ((ll (expt (+ (* 1.0 l) (* 0.39633779 a) (* 0.21580376 b)) 3))
+ (mm (expt (+ (* 1.00000001 l) (* -0.10556134 a) (* -0.06385417 b)) 3))
+ (ss (expt (+ (* 1.00000005 l) (* -0.08948418 a) (* -1.29148554 b)) 3)))
+ (list (+ (* ll 1.22701385) (* mm -0.55779998) (* ss 0.28125615))
+ (+ (* ll -0.04058018) (* mm 1.11225687) (* ss -0.07167668))
+ (+ (* ll -0.07638128) (* mm -0.42148198) (* ss 1.58616322)))))
+
+(compat-defun color-xyz-to-oklab (x y z) ;; <compat-tests:color-xyz-to-oklab>
+ "Convert the CIE XYZ color represented by X Y Z to Oklab."
+ :feature color
+ (let ((ll (+ (* x 0.8189330101) (* y 0.3618667424) (* z -0.1288597137)))
+ (mm (+ (* x 0.0329845436) (* y 0.9293118715) (* z 0.0361456387)))
+ (ss (+ (* x 0.0482003018) (* y 0.2643662691) (* z 0.6338517070))))
+ (let*
+ ((cube-root (lambda (f)
+ (if (< f 0)
+ (- (expt (- f) (/ 1.0 3.0)))
+ (expt f (/ 1.0 3.0)))))
+ (lll (funcall cube-root ll))
+ (mmm (funcall cube-root mm))
+ (sss (funcall cube-root ss)))
+ (list (+ (* lll 0.2104542553) (* mmm 0.7936177850) (* sss -0.0040720468))
+ (+ (* lll 1.9779984951) (* mmm -2.4285922050) (* sss 0.4505937099))
+ (+ (* lll 0.0259040371) (* mmm 0.7827717662) (* sss -0.8086757660))))))
+
+(compat-defun color-oklab-to-srgb (l a b) ;; <compat-tests:color-oklab-to-srgb>
+ "Convert the Oklab color represented by L A B to sRGB."
+ :feature color
+ (apply #'color-xyz-to-srgb (color-oklab-to-xyz l a b)))
+
+(compat-defun color-srgb-to-oklab (r g b) ;; <compat-tests:color-srgb-to-oklab>
+ "Convert the sRGB color R G B to Oklab."
+ :feature color
+ (apply #'color-xyz-to-oklab (color-srgb-to-xyz r g b)))
+
;;;; Defined in subr.el
(compat-defmacro static-if (condition then-form &rest else-forms) ;; <compat-tests:static-if>