color.el (color-name-to-rgb): Use the white color to find the max color component...
[bpt/emacs.git] / lisp / color.el
1 ;;; color.el --- Color manipulation library -*- coding: utf-8; -*-
2
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
4
5 ;; Authors: Julien Danjou <julien@danjou.info>
6 ;; Drew Adams <drew.adams@oracle.com>
7 ;; Keywords: lisp, faces, color, hex, rgb, hsv, hsl, cie-lab, background
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package provides functions for manipulating colors, including
27 ;; converting between color representations, computing color
28 ;; complements, and computing CIEDE2000 color distances.
29 ;;
30 ;; Supported color representations include RGB (red, green, blue), HSV
31 ;; (hue, saturation, value), HSL (hue, saturation, luminance), sRGB,
32 ;; CIE XYZ, and CIE L*a*b* color components.
33
34 ;;; Code:
35
36 (eval-when-compile
37 (require 'cl))
38
39 ;; Emacs < 23.3
40 (eval-and-compile
41 (unless (boundp 'float-pi)
42 (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...).")))
43
44 ;;;###autoload
45 (defun color-name-to-rgb (color &optional frame)
46 "Convert COLOR string to a list of normalized RGB components.
47 COLOR should be a color name (e.g. \"white\") or an RGB triplet
48 string (e.g. \"#ff12ec\").
49
50 Normally the return value is a list of three floating-point
51 numbers, (RED GREEN BLUE), each between 0.0 and 1.0 inclusive.
52
53 Optional arg FRAME specifies the frame where the color is to be
54 displayed. If FRAME is omitted or nil, use the selected frame.
55 If FRAME cannot display COLOR, return nil."
56 ;; `colors-values' maximum value is either 65535 or 65280 depending on the
57 ;; display system. So we use a white conversion to get the max value.
58 (let ((valmax (float (car (color-values "#ffffff")))))
59 (mapcar (lambda (x) (/ x valmax)) (color-values color frame))))
60
61 (defun color-rgb-to-hex (red green blue)
62 "Return hexadecimal notation for the color RED GREEN BLUE.
63 RED GREEN BLUE must be numbers between 0.0 and 1.0 inclusive."
64 (format "#%02x%02x%02x"
65 (* red 255) (* green 255) (* blue 255)))
66
67 (defun color-complement (color-name)
68 "Return the color that is the complement of COLOR-NAME.
69 COLOR-NAME should be a string naming a color (e.g. \"white\"), or
70 a string specifying a color's RGB components (e.g. \"#ff12ec\")."
71 (let ((color (color-name-to-rgb color-name)))
72 (list (- 1.0 (car color))
73 (- 1.0 (cadr color))
74 (- 1.0 (caddr color)))))
75
76 (defun color-gradient (start stop step-number)
77 "Return a list with STEP-NUMBER colors from START to STOP.
78 The color list builds a color gradient starting at color START to
79 color STOP. It does not include the START and STOP color in the
80 resulting list."
81 (let* ((r (nth 0 start))
82 (g (nth 1 start))
83 (b (nth 2 start))
84 (r-step (/ (- (nth 0 stop) r) (1+ step-number)))
85 (g-step (/ (- (nth 1 stop) g) (1+ step-number)))
86 (b-step (/ (- (nth 2 stop) b) (1+ step-number)))
87 result)
88 (dotimes (n step-number)
89 (push (list (setq r (+ r r-step))
90 (setq g (+ g g-step))
91 (setq b (+ b b-step)))
92 result))
93 (nreverse result)))
94
95 (defun color-complement-hex (color)
96 "Return the color that is the complement of COLOR, in hexadecimal format."
97 (apply 'color-rgb-to-hex (color-complement color)))
98
99 (defun color-rgb-to-hsv (red green blue)
100 "Convert RED, GREEN, and BLUE color components to HSV.
101 RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,
102 inclusive. Return a list (HUE, SATURATION, VALUE), where HUE is
103 in radians and both SATURATION and VALUE are between 0.0 and 1.0,
104 inclusive."
105 (let* ((r (float red))
106 (g (float green))
107 (b (float blue))
108 (max (max r g b))
109 (min (min r g b)))
110 (if (< (- max min) 1e-8)
111 (list 0.0 0.0 0.0)
112 (list
113 (/ (* 2 float-pi
114 (cond ((and (= r g) (= g b)) 0)
115 ((and (= r max)
116 (>= g b))
117 (* 60 (/ (- g b) (- max min))))
118 ((and (= r max)
119 (< g b))
120 (+ 360 (* 60 (/ (- g b) (- max min)))))
121 ((= max g)
122 (+ 120 (* 60 (/ (- b r) (- max min)))))
123 ((= max b)
124 (+ 240 (* 60 (/ (- r g) (- max min)))))))
125 360)
126 (if (= max 0) 0 (- 1 (/ min max)))
127 (/ max 255.0)))))
128
129 (defun color-rgb-to-hsl (red green blue)
130 "Convert RED GREEN BLUE colors to their HSL representation.
131 RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,
132 inclusive.
133
134 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
135 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
136 inclusive."
137 (let* ((r red)
138 (g green)
139 (b blue)
140 (max (max r g b))
141 (min (min r g b))
142 (delta (- max min))
143 (l (/ (+ max min) 2.0)))
144 (list
145 (if (< (- max min) 1e-8)
146 0
147 (* 2 float-pi
148 (/ (cond ((= max r)
149 (+ (/ (- g b) delta) (if (< g b) 6 0)))
150 ((= max g)
151 (+ (/ (- b r) delta) 2))
152 (t
153 (+ (/ (- r g) delta) 4)))
154 6)))
155 (if (= max min)
156 0
157 (if (> l 0.5)
158 (/ delta (- 2 (+ max min)))
159 (/ delta (+ max min))))
160 l)))
161
162 (defun color-srgb-to-xyz (red green blue)
163 "Convert RED GREEN BLUE colors from the sRGB color space to CIE XYZ.
164 RED, BLUE and GREEN must be between 0 and 1, inclusive."
165 (let ((r (if (<= red 0.04045)
166 (/ red 12.95)
167 (expt (/ (+ red 0.055) 1.055) 2.4)))
168 (g (if (<= green 0.04045)
169 (/ green 12.95)
170 (expt (/ (+ green 0.055) 1.055) 2.4)))
171 (b (if (<= blue 0.04045)
172 (/ blue 12.95)
173 (expt (/ (+ blue 0.055) 1.055) 2.4))))
174 (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))
175 (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
176 (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b)))))
177
178 (defun color-xyz-to-srgb (X Y Z)
179 "Convert CIE X Y Z colors to sRGB color space."
180 (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z)))
181 (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z)))
182 (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z))))
183 (list (if (<= r 0.0031308)
184 (* 12.92 r)
185 (- (* 1.055 (expt r (/ 1 2.4))) 0.055))
186 (if (<= g 0.0031308)
187 (* 12.92 g)
188 (- (* 1.055 (expt g (/ 1 2.4))) 0.055))
189 (if (<= b 0.0031308)
190 (* 12.92 b)
191 (- (* 1.055 (expt b (/ 1 2.4))) 0.055)))))
192
193 (defconst color-d65-xyz '(0.950455 1.0 1.088753)
194 "D65 white point in CIE XYZ.")
195
196 (defconst color-cie-ε (/ 216 24389.0))
197 (defconst color-cie-κ (/ 24389 27.0))
198
199 (defun color-xyz-to-lab (X Y Z &optional white-point)
200 "Convert CIE XYZ to CIE L*a*b*.
201 WHITE-POINT specifies the (X Y Z) white point for the
202 conversion. If omitted or nil, use `color-d65-xyz'."
203 (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
204 (let* ((xr (/ X Xr))
205 (yr (/ Y Yr))
206 (zr (/ Z Zr))
207 (fx (if (> xr color-cie-ε)
208 (expt xr (/ 1 3.0))
209 (/ (+ (* color-cie-κ xr) 16) 116.0)))
210 (fy (if (> yr color-cie-ε)
211 (expt yr (/ 1 3.0))
212 (/ (+ (* color-cie-κ yr) 16) 116.0)))
213 (fz (if (> zr color-cie-ε)
214 (expt zr (/ 1 3.0))
215 (/ (+ (* color-cie-κ zr) 16) 116.0))))
216 (list
217 (- (* 116 fy) 16) ; L
218 (* 500 (- fx fy)) ; a
219 (* 200 (- fy fz)))))) ; b
220
221 (defun color-lab-to-xyz (L a b &optional white-point)
222 "Convert CIE L*a*b* to CIE XYZ.
223 WHITE-POINT specifies the (X Y Z) white point for the
224 conversion. If omitted or nil, use `color-d65-xyz'."
225 (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
226 (let* ((fy (/ (+ L 16) 116.0))
227 (fz (- fy (/ b 200.0)))
228 (fx (+ (/ a 500.0) fy))
229 (xr (if (> (expt fx 3.0) color-cie-ε)
230 (expt fx 3.0)
231 (/ (- (* fx 116) 16) color-cie-κ)))
232 (yr (if (> L (* color-cie-κ color-cie-ε))
233 (expt (/ (+ L 16) 116.0) 3.0)
234 (/ L color-cie-κ)))
235 (zr (if (> (expt fz 3) color-cie-ε)
236 (expt fz 3.0)
237 (/ (- (* 116 fz) 16) color-cie-κ))))
238 (list (* xr Xr) ; X
239 (* yr Yr) ; Y
240 (* zr Zr))))) ; Z
241
242 (defun color-srgb-to-lab (red green blue)
243 "Convert RGB to CIE L*a*b*."
244 (apply 'color-xyz-to-lab (color-srgb-to-xyz red green blue)))
245
246 (defun color-lab-to-srgb (L a b)
247 "Convert CIE L*a*b* to RGB."
248 (apply 'color-xyz-to-srgb (color-lab-to-xyz L a b)))
249
250 (defun color-cie-de2000 (color1 color2 &optional kL kC kH)
251 "Return the CIEDE2000 color distance between COLOR1 and COLOR2.
252 Both COLOR1 and COLOR2 should be in CIE L*a*b* format, as
253 returned by `color-srgb-to-lab' or `color-xyz-to-lab'."
254 (destructuring-bind (L₁ a₁ b₁) color1
255 (destructuring-bind (L₂ a₂ b₂) color2
256 (let* ((kL (or kL 1))
257 (kC (or kC 1))
258 (kH (or kH 1))
259 (C(sqrt (+ (expt a₁ 2.0) (expt b₁ 2.0))))
260 (C(sqrt (+ (expt a₂ 2.0) (expt b₂ 2.0))))
261 (C̄ (/ (+ C₁ C₂) 2.0))
262 (G (* 0.5 (- 1 (sqrt (/ (expt7.0) (+ (expt7.0) (expt 25 7.0)))))))
263 (a′₁ (* (+ 1 G) a₁))
264 (a′₂ (* (+ 1 G) a₂))
265 (C′₁ (sqrt (+ (expt a′₁ 2.0) (expt b₁ 2.0))))
266 (C′₂ (sqrt (+ (expt a′₂ 2.0) (expt b₂ 2.0))))
267 (h′₁ (if (and (= b₁ 0) (= a′₁ 0))
268 0
269 (let ((v (atan b₁ a′₁)))
270 (if (< v 0)
271 (+ v (* 2 float-pi))
272 v))))
273 (h′₂ (if (and (= b₂ 0) (= a′₂ 0))
274 0
275 (let ((v (atan b₂ a′₂)))
276 (if (< v 0)
277 (+ v (* 2 float-pi))
278 v))))
279 (ΔL′ (- L₂ L₁))
280 (ΔC′ (- C′₂ C′₁))
281 (Δh′ (cond ((= (* C′₁ C′₂) 0)
282 0)
283 ((<= (abs (- h′₂ h′₁)) float-pi)
284 (- h′₂ h′₁))
285 ((> (- h′₂ h′₁) float-pi)
286 (- (- h′₂ h′₁) (* 2 float-pi)))
287 ((< (- h′₂ h′₁) (- float-pi))
288 (+ (- h′₂ h′₁) (* 2 float-pi)))))
289 (ΔH′ (* 2 (sqrt (* C′₁ C′₂)) (sin (/ Δh′ 2.0))))
290 (L̄′ (/ (+ L₁ L₂) 2.0))
291 (C̄′ (/ (+ C′₁ C′₂) 2.0))
292 (h̄′ (cond ((= (* C′₁ C′₂) 0)
293 (+ h′₁ h′₂))
294 ((<= (abs (- h′₁ h′₂)) float-pi)
295 (/ (+ h′₁ h′₂) 2.0))
296 ((< (+ h′₁ h′₂) (* 2 float-pi))
297 (/ (+ h′₁ h′₂ (* 2 float-pi)) 2.0))
298 ((>= (+ h′₁ h′₂) (* 2 float-pi))
299 (/ (+ h′₁ h′₂ (* -2 float-pi)) 2.0))))
300 (T (+ 1
301 (- (* 0.17 (cos (- h̄′ (degrees-to-radians 30)))))
302 (* 0.24 (cos (* h̄′ 2)))
303 (* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
304 (- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
305 (Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- h̄′ (degrees-to-radians 275)) (degrees-to-radians 25)) 2.0)))))
306 (Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
307 (Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0)) (sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
308 (Sc (+ 1 (* C̄′ 0.045)))
309 (Sh (+ 1 (* 0.015 C̄′ T)))
310 (Rt (- (* (sin (* Δθ 2)) Rc))))
311 (sqrt (+ (expt (/ ΔL′ (* Sl kL)) 2.0)
312 (expt (/ ΔC′ (* Sc kC)) 2.0)
313 (expt (/ ΔH′ (* Sh kH)) 2.0)
314 (* Rt (/ ΔC′ (* Sc kC)) (/ ΔH′ (* Sh kH)))))))))
315
316 (provide 'color)
317
318 ;;; color.el ends here