35e75d435030b7fbdd6d170f472cae86e4c90de2
[gnus] / lisp / color-lab.el
1 ;;; color-lab.el --- Color manipulation laboratory routines -*- coding: utf-8; -*-
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 (eval-and-compile
31   (unless (boundp 'float-pi)
32     (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...).")))
33
34 (defun rgb->hsv (red green blue)
35   "Convert RED GREEN BLUE values to HSV representation.
36 Hue is in radian. Saturation and values are between 0 and 1."
37    (let* ((r (float red))
38          (g (float green))
39          (b (float blue))
40          (max (max r g b))
41          (min (min r g b)))
42      (list
43       (/ (* 2 float-pi
44             (cond ((and (= r g) (= g b)) 0)
45                   ((and (= r max)
46                         (>= g b))
47                    (* 60 (/ (- g b) (- max min))))
48                   ((and (= r max)
49                         (< g b))
50                    (+ 360 (* 60 (/ (- g b) (- max min)))))
51                   ((= max g)
52                    (+ 120 (* 60 (/ (- b r) (- max min)))))
53                   ((= max b)
54                        (+ 240 (* 60 (/ (- r g) (- max min)))))))
55          360)
56       (if (= max 0)
57           0
58         (- 1 (/ min max)))
59       (/ max 255.0))))
60
61 (defun rgb->hsl (red green blue)
62   "Convert RED GREEN BLUE colors to their HSL representation.
63 RED, GREEN and BLUE must be between 0 and 255."
64   (let* ((r (/ red 255.0))
65          (g (/ green 255.0))
66          (b (/ blue 255.0))
67          (max (max r g b))
68          (min (min r g b))
69          (delta (- max min))
70          (l (/ (+ max min) 2.0)))
71     (list
72      (if (= max min)
73          0
74        (* 2 float-pi
75           (/ (cond ((= max r)
76                     (+ (/ (- g b) delta) (if (< g b) 6 0)))
77                    ((= max g)
78                  (+ (/ (- b r) delta) 2))
79                    (t
80                     (+ (/ (- r g) delta) 4)))
81              6)))
82      (if (= max min)
83          0
84        (if (> l 0.5)
85            (/ delta (- 2 (+ max min)))
86          (/ delta (+ max min))))
87      l)))
88
89 (defun rgb->xyz (red green blue)
90   "Converts RED GREEN BLUE colors to CIE XYZ representation.
91 RED, BLUE and GREEN must be between 0 and 1."
92   (let ((r (if (<= red 0.04045)
93                (/ red 12.95)
94              (expt (/ (+ red 0.055) 1.055) 2.4)))
95         (g (if (<= green 0.04045)
96                (/ green 12.95)
97              (expt (/ (+ green 0.055) 1.055) 2.4)))
98         (b (if (<= blue 0.04045)
99                (/ blue 12.95)
100              (expt (/ (+ blue 0.055) 1.055) 2.4))))
101     (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))
102           (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
103           (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b)))))
104
105 (defun xyz->rgb (X Y Z)
106   "Converts CIE XYZ colors to RGB."
107   (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z)))
108         (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z)))
109         (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z))))
110     (list (if (<= r 0.0031308)
111               (* 12.92 r)
112             (- (* 1.055 (expt r (/ 1 2.4))) 0.055))
113           (if (<= g 0.0031308)
114               (* 12.92 g)
115             (- (* 1.055 (expt g (/ 1 2.4))) 0.055))
116           (if (<= b 0.0031308)
117               (* 12.92 b)
118             (- (* 1.055 (expt b (/ 1 2.4))) 0.055)))))
119
120 (defconst color-lab-d65-xyz '(0.950455 1.0 1.088753)
121   "D65 white point in CIE XYZ.")
122
123 (defconst color-lab-ε (/ 216 24389.0))
124 (defconst color-lab-κ (/ 24389 27.0))
125
126 (defun xyz->lab (X Y Z &optional white-point)
127   "Converts CIE XYZ to CIE L*a*b*.
128 WHITE-POINT can be specified as (X Y Z) white point to use. If
129 none is set, `color-lab-d65-xyz' is used."
130   (destructuring-bind (Xr Yr Zr) (or white-point color-lab-d65-xyz)
131       (let* ((xr (/ X Xr))
132              (yr (/ Y Yr))
133              (zr (/ Z Zr))
134              (fx (if (> xr color-lab-ε)
135                      (expt xr (/ 1 3.0))
136                    (/ (+ (* color-lab-κ xr) 16) 116.0)))
137              (fy (if (> yr color-lab-ε)
138                      (expt yr (/ 1 3.0))
139                    (/ (+ (* color-lab-κ yr) 16) 116.0)))
140              (fz (if (> zr color-lab-ε)
141                      (expt zr (/ 1 3.0))
142                    (/ (+ (* color-lab-κ zr) 16) 116.0))))
143         (list
144          (- (* 116 fy) 16)                  ; L
145          (* 500 (- fx fy))                  ; a
146          (* 200 (- fy fz))))))              ; b
147
148 (defun lab->xyz (L a b &optional white-point)
149   "Converts CIE L*a*b* to CIE XYZ.
150 WHITE-POINT can be specified as (X Y Z) white point to use. If
151 none is set, `color-lab-d65-xyz' is used."
152   (destructuring-bind (Xr Yr Zr) (or white-point color-lab-d65-xyz)
153       (let* ((fy (/ (+ L 16) 116.0))
154              (fz (- fy (/ b 200.0)))
155              (fx (+ (/ a 500.0) fy))
156              (xr (if (> (expt fx 3.0) color-lab-ε)
157                      (expt fx 3.0)
158                (/ (- (* fx 116) 16) color-lab-κ)))
159              (yr (if (> L (* color-lab-κ color-lab-ε))
160                      (expt (/ (+ L 16) 116.0) 3.0)
161                    (/ L color-lab-κ)))
162              (zr (if (> (expt fz 3) color-lab-ε)
163                      (expt fz 3.0)
164                    (/ (- (* 116 fz) 16) color-lab-κ))))
165         (list (* xr Xr)                 ; X
166               (* yr Yr)                 ; Y
167               (* zr Zr)))))             ; Z
168
169 (defun rgb->lab (red green blue)
170   "Converts RGB to CIE L*a*b*."
171   (apply 'xyz->lab (rgb->xyz red green blue)))
172
173 (defun rgb->normalize (color)
174   "Normalize a RGB color to values between [0,1]."
175   (mapcar (lambda (x) (/ x 65535.0)) (x-color-values color)))
176
177 (defun lab->rgb (L a b)
178   "Converts CIE L*a*b* to RGB."
179   (apply 'xyz->rgb (lab->xyz L a b)))
180
181 (defun color-lab-ciede2000 (color1 color2 &optional kL kC kH)
182   "Computes the CIEDE2000 color distance between COLOR1 and COLOR2.
183 Colors must be in CIE L*a*b* format."
184   (destructuring-bind (L₁ a₁ b₁) color1
185     (destructuring-bind (L₂ a₂ b₂) color2
186       (let* ((kL (or kL 1))
187              (kC (or kC 1))
188              (kH (or kH 1))
189              (C₁ (sqrt (+ (expt a₁ 2.0) (expt b₁ 2.0))))
190              (C₂ (sqrt (+ (expt a₂ 2.0) (expt b₂ 2.0))))
191              (C̄ (/ (+ C₁ C₂) 2.0))
192              (G (* 0.5 (- 1 (sqrt (/ (expt C̄ 7.0) (+ (expt C̄ 7.0) (expt 25 7.0)))))))
193              (a′₁ (* (+ 1 G) a₁))
194              (a′₂ (* (+ 1 G) a₂))
195              (C′₁ (sqrt (+ (expt a′₁ 2.0) (expt b₁ 2.0))))
196              (C′₂ (sqrt (+ (expt a′₂ 2.0) (expt b₂ 2.0))))
197              (h′₁ (if (and (= b₁ 0) (= a′₁ 0))
198                       0
199                     (let ((v (atan b₁ a′₁)))
200                       (if (< v 0)
201                           (+ v (* 2 float-pi))
202                         v))))
203              (h′₂ (if (and (= b₂ 0) (= a′₂ 0))
204                       0
205                     (let ((v (atan b₂ a′₂)))
206                       (if (< v 0)
207                           (+ v (* 2 float-pi))
208                         v))))
209              (ΔL′ (- L₂ L₁))
210              (ΔC′ (- C′₂ C′₁))
211              (Δh′ (cond ((= (* C′₁ C′₂) 0)
212                          0)
213                         ((<= (abs (- h′₂ h′₁)) float-pi)
214                          (- h′₂ h′₁))
215                         ((> (- h′₂ h′₁) float-pi)
216                          (- (- h′₂ h′₁) (* 2 float-pi)))
217                         ((< (- h′₂ h′₁) (- float-pi))
218                          (+ (- h′₂ h′₁) (* 2 float-pi)))))
219              (ΔH′ (* 2 (sqrt (* C′₁ C′₂)) (sin (/ Δh′ 2.0))))
220              (L̄′ (/ (+ L₁ L₂) 2.0))
221              (C̄′ (/ (+ C′₁ C′₂) 2.0))
222              (h̄′ (cond ((= (* C′₁ C′₂) 0)
223                         (+ h′₁ h′₂))
224                        ((<= (abs (- h′₁ h′₂)) float-pi)
225                         (/ (+ h′₁ h′₂) 2.0))
226                        ((< (+ h′₁ h′₂) (* 2 float-pi))
227                         (/ (+ h′₁ h′₂ (* 2 float-pi)) 2.0))
228                        ((>= (+ h′₁ h′₂) (* 2 float-pi))
229                         (/ (+ h′₁ h′₂ (* -2 float-pi)) 2.0))))
230              (T (+ 1
231                    (- (* 0.17 (cos (- h̄′ (degrees-to-radians 30)))))
232                    (* 0.24 (cos (* h̄′ 2)))
233                    (* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
234                    (- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
235              (Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- h̄′ (degrees-to-radians 275)) (degrees-to-radians 25)) 2.0)))))
236              (Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
237              (Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0)) (sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
238              (Sc (+ 1 (* C̄′ 0.045)))
239              (Sh (+ 1 (* 0.015 C̄′ T)))
240              (Rt (- (* (sin (* Δθ 2)) Rc))))
241         (sqrt (+ (expt (/ ΔL′ (* Sl kL)) 2.0)
242                  (expt (/ ΔC′ (* Sc kC)) 2.0)
243                  (expt (/ ΔH′ (* Sh kH)) 2.0)
244                  (* Rt (/ ΔC′ (* Sc kC)) (/ ΔH′ (* Sh kH)))))))))
245
246 (provide 'color-lab)
247
248 ;;; color-lab.el ends here