16f711a3c9a76a3b1c401778be499c8fd16ac2c0
[gnus] / lisp / color-lab.el
1 ;;; color-lab.el --- Color manipulation laboratory routines
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This package provides color manipulation functions.
26
27 ;;; Code:
28
29 ;; Emacs < 23.3
30 (unless (boundp 'float-pi)
31   (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...)."))
32
33 (defun rgb->hsv (red green blue)
34   "Convert RED GREEN BLUE values to HSV representation.
35 Hue is in radian. Saturation and values are between 0 and 1."
36    (let* ((r (float red))
37          (g (float green))
38          (b (float blue))
39          (max (max r g b))
40          (min (min r g b)))
41      (list
42       (/ (* 2 float-pi
43             (cond ((and (= r g) (= g b)) 0)
44                   ((and (= r max)
45                         (>= g b))
46                    (* 60 (/ (- g b) (- max min))))
47                   ((and (= r max)
48                         (< g b))
49                    (+ 360 (* 60 (/ (- g b) (- max min)))))
50                   ((= max g)
51                    (+ 120 (* 60 (/ (- b r) (- max min)))))
52                   ((= max b)
53                        (+ 240 (* 60 (/ (- r g) (- max min)))))))
54          360)
55       (if (= max 0)
56           0
57         (- 1 (/ min max)))
58       (/ max 255.0))))
59
60 (defun rgb->hsl (red green blue)
61   "Convert RED GREEN BLUE colors to their HSL representation.
62 RED, GREEN and BLUE must be between 0 and 255."
63   (let* ((r (/ red 255.0))
64          (g (/ green 255.0))
65          (b (/ blue 255.0))
66          (max (max r g b))
67          (min (min r g b))
68          (delta (- max min))
69          (l (/ (+ max min) 2.0)))
70     (list
71      (if (= max min)
72          0
73        (* 2 float-pi
74           (/ (cond ((= max r)
75                     (+ (/ (- g b) delta) (if (< g b) 6 0)))
76                    ((= max g)
77                  (+ (/ (- b r) delta) 2))
78                    (t
79                     (+ (/ (- r g) delta) 4)))
80              6)))
81      (if (= max min)
82          0
83        (if (> l 0.5)
84            (/ delta (- 2 (+ max min)))
85          (/ delta (+ max min))))
86      l)))
87
88 (defun rgb->xyz (red green blue)
89   "Converts RED GREEN BLUE colors to CIE XYZ representation.
90 RED, BLUE and GREEN must be between 0 and 1."
91   (let ((r (if (<= red 0.04045)
92                (/ red 12.95)
93              (expt (/ (+ red 0.055) 1.055) 2.4)))
94         (g (if (<= green 0.04045)
95                (/ green 12.95)
96              (expt (/ (+ green 0.055) 1.055) 2.4)))
97         (b (if (<= blue 0.04045)
98                (/ blue 12.95)
99              (expt (/ (+ blue 0.055) 1.055) 2.4))))
100     (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))
101           (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
102           (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b)))))
103
104 (defun xyz->rgb (X Y Z)
105   "Converts CIE XYZ colors to RGB."
106   (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z)))
107         (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z)))
108         (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z))))
109     (list (if (<= r 0.0031308)
110               (* 12.92 r)
111             (- (* 1.055 (expt r (/ 1 2.4))) 0.055))
112           (if (<= g 0.0031308)
113               (* 12.92 g)
114             (- (* 1.055 (expt g (/ 1 2.4))) 0.055))
115           (if (<= b 0.0031308)
116               (* 12.92 b)
117             (- (* 1.055 (expt b (/ 1 2.4))) 0.055)))))
118
119 (defconst color-lab-d65-xyz '(0.950455 1.0 1.088753)
120   "D65 white point in CIE XYZ.")
121
122 (defconst color-lab-ε (/ 216 24389.0))
123 (defconst color-lab-κ (/ 24389 27.0))
124
125 (defun xyz->lab (X Y Z &optional white-point)
126   "Converts CIE XYZ to CIE L*a*b*.
127 WHITE-POINT can be specified as (X Y Z) white point to use. If
128 none is set, `color-lab-d65-xyz' is used."
129   (destructuring-bind (Xr Yr Zr) (or white-point color-lab-d65-xyz)
130       (let* ((xr (/ X Xr))
131              (yr (/ Y Yr))
132              (zr (/ Z Zr))
133              (fx (if (> xr color-lab-ε)
134                      (expt xr (/ 1 3.0))
135                    (/ (+ (* color-lab-κ xr) 16) 116.0)))
136              (fy (if (> yr color-lab-ε)
137                      (expt yr (/ 1 3.0))
138                    (/ (+ (* color-lab-κ yr) 16) 116.0)))
139              (fz (if (> zr color-lab-ε)
140                      (expt zr (/ 1 3.0))
141                    (/ (+ (* color-lab-κ zr) 16) 116.0))))
142         (list
143          (- (* 116 fy) 16)                  ; L
144          (* 500 (- fx fy))                  ; a
145          (* 200 (- fy fz))))))              ; b
146
147 (defun lab->xyz (L a b &optional white-point)
148   "Converts CIE L*a*b* to CIE XYZ.
149 WHITE-POINT can be specified as (X Y Z) white point to use. If
150 none is set, `color-lab-d65-xyz' is used."
151   (destructuring-bind (Xr Yr Zr) (or white-point color-lab-d65-xyz)
152       (let* ((fy (/ (+ L 16) 116.0))
153              (fz (- fy (/ b 200.0)))
154              (fx (+ (/ a 500.0) fy))
155              (xr (if (> (expt fx 3) color-lab-ε)
156                      (expt fx 3)
157                (/ (- (* fx 116) 16) color-lab-κ)))
158              (yr (if (> L (* color-lab-κ color-lab-ε))
159                      (expt (/ (+ L 16) 116.0) 3)
160                    (/ L color-lab-κ)))
161              (zr (if (> (expt fz 3) color-lab-ε)
162                      (expt fz 3)
163                    (/ (- (* 116 fz) 16) color-lab-κ))))
164         (list (* xr Xr)                 ; X
165               (* yr Yr)                 ; Y
166               (* zr Zr)))))             ; Z
167
168 (defun rgb->lab (red green blue)
169   "Converts RGB to CIE L*a*b*."
170   (apply 'xyz->lab (rgb->xyz red green blue)))
171
172 (defun rgb->normalize (color)
173   "Normalize a RGB color to values between [0,1]."
174   (mapcar (lambda (x) (/ x 65535.0)) (x-color-values color)))
175
176 (defun lab->rgb (L a b)
177   "Converts CIE L*a*b* to RGB."
178   (apply 'xyz->rgb (lab->xyz L a b)))
179
180 (defun color-lab-ciede2000 (color1 color2 &optional kL kC kH)
181   "Computes the CIEDE2000 color distance between COLOR1 and COLOR2.
182 Colors must be in CIE L*a*b* format."
183   (destructuring-bind (L₁ a₁ b₁) color1
184     (destructuring-bind (L₂ a₂ b₂) color2
185       (let* ((kL (or kL 1))
186              (kC (or kC 1))
187              (kH (or kH 1))
188              (C₁ (sqrt (+ (expt a₁ 2) (expt b₁ 2))))
189              (C₂ (sqrt (+ (expt a₂ 2) (expt b₂ 2))))
190              (C̄ (/ (+ C₁ C₂) 2.0))
191              (G (* 0.5 (- 1 (sqrt (/ (expt C̄ 7) (+ (expt C̄ 7) (expt 25 7)))))))
192              (a′₁ (* (+ 1 G) a₁))
193              (a′₂ (* (+ 1 G) a₂))
194              (C′₁ (sqrt (+ (expt a′₁ 2) (expt b₁ 2))))
195              (C′₂ (sqrt (+ (expt a′₂ 2) (expt b₂ 2))))
196              (h′₁ (if (and (= b₁ 0) (= a′₁ 0))
197                       0
198                     (let ((v (atan b₁ a′₁)))
199                       (if (< v 0)
200                           (+ v (* 2 float-pi))
201                         v))))
202              (h′₂ (if (and (= b₂ 0) (= a′₂ 0))
203                       0
204                     (let ((v (atan b₂ a′₂)))
205                       (if (< v 0)
206                           (+ v (* 2 float-pi))
207                         v))))
208              (ΔL′ (- L₂ L₁))
209              (ΔC′ (- C′₂ C′₁))
210              (Δh′ (cond ((= (* C′₁ C′₂) 0)
211                          0)
212                         ((<= (abs (- h′₂ h′₁)) float-pi)
213                          (- h′₂ h′₁))
214                         ((> (- h′₂ h′₁) float-pi)
215                          (- (- h′₂ h′₁) (* 2 float-pi)))
216                         ((< (- h′₂ h′₁) (- float-pi))
217                          (+ (- h′₂ h′₁) (* 2 float-pi)))))
218              (ΔH′ (* 2 (sqrt (* C′₁ C′₂)) (sin (/ Δh′ 2.0))))
219              (L̄′ (/ (+ L₁ L₂) 2.0))
220              (C̄′ (/ (+ C′₁ C′₂) 2.0))
221              (h̄′ (cond ((= (* C′₁ C′₂) 0)
222                         (+ h′₁ h′₂))
223                        ((<= (abs (- h′₁ h′₂)) float-pi)
224                         (/ (+ h′₁ h′₂) 2.0))
225                        ((< (+ h′₁ h′₂) (* 2 float-pi))
226                         (/ (+ h′₁ h′₂ (* 2 float-pi)) 2.0))
227                        ((>= (+ h′₁ h′₂) (* 2 float-pi))
228                         (/ (+ h′₁ h′₂ (* -2 float-pi)) 2.0))))
229              (T (+ 1
230                    (- (* 0.17 (cos (- h̄′ (degrees-to-radians 30)))))
231                    (* 0.24 (cos (* h̄′ 2)))
232                    (* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
233                    (- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
234              (Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- h̄′ (degrees-to-radians 275)) (degrees-to-radians 25)) 2)))))
235              (Rc (* 2 (sqrt (/ (expt C̄′ 7) (+ (expt C̄′ 7) (expt 25 7))))))
236              (Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2)) (sqrt (+ 20 (expt (- L̄′ 50) 2))))))
237              (Sc (+ 1 (* C̄′ 0.045)))
238              (Sh (+ 1 (* 0.015 C̄′ T)))
239              (Rt (- (* (sin (* Δθ 2)) Rc))))
240         (sqrt (+ (expt (/ ΔL′ (* Sl kL)) 2)
241                  (expt (/ ΔC′ (* Sc kC)) 2)
242                  (expt (/ ΔH′ (* Sh kH)) 2)
243                  (* Rt (/ ΔC′ (* Sc kC)) (/ ΔH′ (* Sh kH)))))))))
244
245 (provide 'color-lab)