* test/automated/Makefile.in (all): Fix typo.
[emacs.git] / lisp / color.el
blob6ccf9a79494f81e22bc7b37388805f93597e9198
1 ;;; color.el --- Color manipulation library -*- coding: utf-8; -*-
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
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
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; This package provides functions for manipulating colors, including
27 ;; converting between color representations, computing color
28 ;; complements, and computing CIEDE2000 color distances.
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.
34 ;;; Code:
36 (eval-when-compile
37 (require 'cl))
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...).")))
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\").
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.
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))))
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)))
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)))))
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)))
95 (defun color-hue-to-rgb (v1 v2 h)
96 "Compute hue from V1 and V2 H. Internally used by
97 `color-hsl-to-rgb'."
98 (cond
99 ((< h (/ 1.0 6)) (+ v1 (* (- v2 v1) h 6.0)))
100 ((< h 0.5) v2)
101 ((< h (/ 2.0 3)) (+ v1 (* (- v2 v1) (- (/ 2.0 3) h) 6.0)))
102 (t v1)))
104 (defun color-hsl-to-rgb (H S L)
105 "Convert H S L (HUE, SATURATION, LUMINANCE) , where HUE is in
106 radians and both SATURATION and LUMINANCE are between 0.0 and
107 1.0, inclusive to their RGB representation.
109 Return a list (RED, GREEN, BLUE) which each be numbers between
110 0.0 and 1.0, inclusive."
112 (if (= S 0.0)
113 (list L L L)
114 (let* ((m2 (if (<= L 0.5)
115 (* L (+ 1.0 S))
116 (- (+ L S) (* L S))))
117 (m1 (- (* 2.0 L) m2)))
118 (list
119 (color-hue-to-rgb m1 m2 (+ H (/ 1.0 3)))
120 (color-hue-to-rgb m1 m2 H)
121 (color-hue-to-rgb m1 m2 (- H (/ 1.0 3)))))))
123 (defun color-complement-hex (color)
124 "Return the color that is the complement of COLOR, in hexadecimal format."
125 (apply 'color-rgb-to-hex (color-complement color)))
127 (defun color-rgb-to-hsv (red green blue)
128 "Convert RED, GREEN, and BLUE color components to HSV.
129 RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,
130 inclusive. Return a list (HUE, SATURATION, VALUE), where HUE is
131 in radians and both SATURATION and VALUE are between 0.0 and 1.0,
132 inclusive."
133 (let* ((r (float red))
134 (g (float green))
135 (b (float blue))
136 (max (max r g b))
137 (min (min r g b)))
138 (if (< (- max min) 1e-8)
139 (list 0.0 0.0 0.0)
140 (list
141 (/ (* 2 float-pi
142 (cond ((and (= r g) (= g b)) 0)
143 ((and (= r max)
144 (>= g b))
145 (* 60 (/ (- g b) (- max min))))
146 ((and (= r max)
147 (< g b))
148 (+ 360 (* 60 (/ (- g b) (- max min)))))
149 ((= max g)
150 (+ 120 (* 60 (/ (- b r) (- max min)))))
151 ((= max b)
152 (+ 240 (* 60 (/ (- r g) (- max min)))))))
153 360)
154 (if (= max 0) 0 (- 1 (/ min max)))
155 (/ max 255.0)))))
157 (defun color-rgb-to-hsl (red green blue)
158 "Convert RED GREEN BLUE colors to their HSL representation.
159 RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,
160 inclusive.
162 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
163 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
164 inclusive."
165 (let* ((r red)
166 (g green)
167 (b blue)
168 (max (max r g b))
169 (min (min r g b))
170 (delta (- max min))
171 (l (/ (+ max min) 2.0)))
172 (if (= delta 0)
173 (list 0.0 0.0 l)
174 (let* ((s (if (<= l 0.5) (/ delta (+ max min))
175 (/ delta (- 2.0 max min))))
176 (rc (/ (- max r) delta))
177 (gc (/ (- max g) delta))
178 (bc (/ (- max b) delta))
179 (h (mod
181 (cond
182 ((= r max) (- bc gc))
183 ((= g max) (+ 2.0 rc (- bc)))
184 (t (+ 4.0 gc (- rc))))
185 6.0) 1.0)))
186 (list h s l)))))
188 (defun color-srgb-to-xyz (red green blue)
189 "Convert RED GREEN BLUE colors from the sRGB color space to CIE XYZ.
190 RED, BLUE and GREEN must be between 0 and 1, inclusive."
191 (let ((r (if (<= red 0.04045)
192 (/ red 12.95)
193 (expt (/ (+ red 0.055) 1.055) 2.4)))
194 (g (if (<= green 0.04045)
195 (/ green 12.95)
196 (expt (/ (+ green 0.055) 1.055) 2.4)))
197 (b (if (<= blue 0.04045)
198 (/ blue 12.95)
199 (expt (/ (+ blue 0.055) 1.055) 2.4))))
200 (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))
201 (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
202 (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b)))))
204 (defun color-xyz-to-srgb (X Y Z)
205 "Convert CIE X Y Z colors to sRGB color space."
206 (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z)))
207 (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z)))
208 (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z))))
209 (list (if (<= r 0.0031308)
210 (* 12.92 r)
211 (- (* 1.055 (expt r (/ 1 2.4))) 0.055))
212 (if (<= g 0.0031308)
213 (* 12.92 g)
214 (- (* 1.055 (expt g (/ 1 2.4))) 0.055))
215 (if (<= b 0.0031308)
216 (* 12.92 b)
217 (- (* 1.055 (expt b (/ 1 2.4))) 0.055)))))
219 (defconst color-d65-xyz '(0.950455 1.0 1.088753)
220 "D65 white point in CIE XYZ.")
222 (defconst color-cie-ε (/ 216 24389.0))
223 (defconst color-cie-κ (/ 24389 27.0))
225 (defun color-xyz-to-lab (X Y Z &optional white-point)
226 "Convert CIE XYZ to CIE L*a*b*.
227 WHITE-POINT specifies the (X Y Z) white point for the
228 conversion. If omitted or nil, use `color-d65-xyz'."
229 (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
230 (let* ((xr (/ X Xr))
231 (yr (/ Y Yr))
232 (zr (/ Z Zr))
233 (fx (if (> xr color-cie-ε)
234 (expt xr (/ 1 3.0))
235 (/ (+ (* color-cie-κ xr) 16) 116.0)))
236 (fy (if (> yr color-cie-ε)
237 (expt yr (/ 1 3.0))
238 (/ (+ (* color-cie-κ yr) 16) 116.0)))
239 (fz (if (> zr color-cie-ε)
240 (expt zr (/ 1 3.0))
241 (/ (+ (* color-cie-κ zr) 16) 116.0))))
242 (list
243 (- (* 116 fy) 16) ; L
244 (* 500 (- fx fy)) ; a
245 (* 200 (- fy fz)))))) ; b
247 (defun color-lab-to-xyz (L a b &optional white-point)
248 "Convert CIE L*a*b* to CIE XYZ.
249 WHITE-POINT specifies the (X Y Z) white point for the
250 conversion. If omitted or nil, use `color-d65-xyz'."
251 (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
252 (let* ((fy (/ (+ L 16) 116.0))
253 (fz (- fy (/ b 200.0)))
254 (fx (+ (/ a 500.0) fy))
255 (xr (if (> (expt fx 3.0) color-cie-ε)
256 (expt fx 3.0)
257 (/ (- (* fx 116) 16) color-cie-κ)))
258 (yr (if (> L (* color-cie-κ color-cie-ε))
259 (expt (/ (+ L 16) 116.0) 3.0)
260 (/ L color-cie-κ)))
261 (zr (if (> (expt fz 3) color-cie-ε)
262 (expt fz 3.0)
263 (/ (- (* 116 fz) 16) color-cie-κ))))
264 (list (* xr Xr) ; X
265 (* yr Yr) ; Y
266 (* zr Zr))))) ; Z
268 (defun color-srgb-to-lab (red green blue)
269 "Convert RGB to CIE L*a*b*."
270 (apply 'color-xyz-to-lab (color-srgb-to-xyz red green blue)))
272 (defun color-lab-to-srgb (L a b)
273 "Convert CIE L*a*b* to RGB."
274 (apply 'color-xyz-to-srgb (color-lab-to-xyz L a b)))
276 (defun color-cie-de2000 (color1 color2 &optional kL kC kH)
277 "Return the CIEDE2000 color distance between COLOR1 and COLOR2.
278 Both COLOR1 and COLOR2 should be in CIE L*a*b* format, as
279 returned by `color-srgb-to-lab' or `color-xyz-to-lab'."
280 (destructuring-bind (L₁ a₁ b₁) color1
281 (destructuring-bind (L₂ a₂ b₂) color2
282 (let* ((kL (or kL 1))
283 (kC (or kC 1))
284 (kH (or kH 1))
285 (C(sqrt (+ (expt a₁ 2.0) (expt b₁ 2.0))))
286 (C(sqrt (+ (expt a₂ 2.0) (expt b₂ 2.0))))
287 (C̄ (/ (+ C₁ C₂) 2.0))
288 (G (* 0.5 (- 1 (sqrt (/ (expt7.0) (+ (expt7.0) (expt 25 7.0)))))))
289 (a′₁ (* (+ 1 G) a₁))
290 (a′₂ (* (+ 1 G) a₂))
291 (C′₁ (sqrt (+ (expt a′₁ 2.0) (expt b₁ 2.0))))
292 (C′₂ (sqrt (+ (expt a′₂ 2.0) (expt b₂ 2.0))))
293 (h′₁ (if (and (= b₁ 0) (= a′₁ 0))
295 (let ((v (atan b₁ a′₁)))
296 (if (< v 0)
297 (+ v (* 2 float-pi))
298 v))))
299 (h′₂ (if (and (= b₂ 0) (= a′₂ 0))
301 (let ((v (atan b₂ a′₂)))
302 (if (< v 0)
303 (+ v (* 2 float-pi))
304 v))))
305 (ΔL′ (- L₂ L₁))
306 (ΔC′ (- C′₂ C′₁))
307 (Δh′ (cond ((= (* C′₁ C′₂) 0)
309 ((<= (abs (- h′₂ h′₁)) float-pi)
310 (- h′₂ h′₁))
311 ((> (- h′₂ h′₁) float-pi)
312 (- (- h′₂ h′₁) (* 2 float-pi)))
313 ((< (- h′₂ h′₁) (- float-pi))
314 (+ (- h′₂ h′₁) (* 2 float-pi)))))
315 (ΔH′ (* 2 (sqrt (* C′₁ C′₂)) (sin (/ Δh′ 2.0))))
316 (L̄′ (/ (+ L₁ L₂) 2.0))
317 (C̄′ (/ (+ C′₁ C′₂) 2.0))
318 (h̄′ (cond ((= (* C′₁ C′₂) 0)
319 (+ h′₁ h′₂))
320 ((<= (abs (- h′₁ h′₂)) float-pi)
321 (/ (+ h′₁ h′₂) 2.0))
322 ((< (+ h′₁ h′₂) (* 2 float-pi))
323 (/ (+ h′₁ h′₂ (* 2 float-pi)) 2.0))
324 ((>= (+ h′₁ h′₂) (* 2 float-pi))
325 (/ (+ h′₁ h′₂ (* -2 float-pi)) 2.0))))
326 (T (+ 1
327 (- (* 0.17 (cos (- h̄′ (degrees-to-radians 30)))))
328 (* 0.24 (cos (* h̄′ 2)))
329 (* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
330 (- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
331 (Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- h̄′ (degrees-to-radians 275)) (degrees-to-radians 25)) 2.0)))))
332 (Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
333 (Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0)) (sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
334 (Sc (+ 1 (* C̄′ 0.045)))
335 (Sh (+ 1 (* 0.015 C̄′ T)))
336 (Rt (- (* (sin (* Δθ 2)) Rc))))
337 (sqrt (+ (expt (/ ΔL′ (* Sl kL)) 2.0)
338 (expt (/ ΔC′ (* Sc kC)) 2.0)
339 (expt (/ ΔH′ (* Sh kH)) 2.0)
340 (* Rt (/ ΔC′ (* Sc kC)) (/ ΔH′ (* Sh kH)))))))))
342 (defun color-clamp (value)
343 "Make sure VALUE is a number between 0.0 and 1.0 inclusive."
344 (min 1.0 (max 0.0 value)))
346 (defun color-saturate-hsl (H S L percent)
347 "Return a color PERCENT more saturated than the one defined in
348 H S L color-space.
350 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
351 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
352 inclusive."
353 (list H (color-clamp (+ S (/ percent 100.0))) L))
355 (defun color-saturate-name (name percent)
356 "Short hand to saturate COLOR by PERCENT.
358 See `color-saturate-hsl'."
359 (apply 'color-rgb-to-hex
360 (apply 'color-hsl-to-rgb
361 (apply 'color-saturate-hsl
362 (append
363 (apply 'color-rgb-to-hsl
364 (color-name-to-rgb name))
365 (list percent))))))
367 (defun color-desaturate-hsl (H S L percent)
368 "Return a color PERCENT less saturated than the one defined in
369 H S L color-space.
371 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
372 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
373 inclusive."
374 (color-saturate-hsl H S L (- percent)))
376 (defun color-desaturate-name (name percent)
377 "Short hand to desaturate COLOR by PERCENT.
379 See `color-desaturate-hsl'."
380 (color-saturate-name name (- percent)))
382 (defun color-lighten-hsl (H S L percent)
383 "Return a color PERCENT lighter than the one defined in
384 H S L color-space.
386 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
387 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
388 inclusive."
389 (list H S (color-clamp (+ L (/ percent 100.0)))))
391 (defun color-lighten-name (name percent)
392 "Short hand to saturate COLOR by PERCENT.
394 See `color-lighten-hsl'."
395 (apply 'color-rgb-to-hex
396 (apply 'color-hsl-to-rgb
397 (apply 'color-lighten-hsl
398 (append
399 (apply 'color-rgb-to-hsl
400 (color-name-to-rgb name))
401 (list percent))))))
403 (defun color-darken-hsl (H S L percent)
404 "Return a color PERCENT darker than the one defined in
405 H S L color-space.
407 Return a list (HUE, SATURATION, LUMINANCE), where HUE is in radians
408 and both SATURATION and LUMINANCE are between 0.0 and 1.0,
409 inclusive."
410 (color-lighten-hsl H S L (- percent)))
412 (defun color-darken-name (name percent)
413 "Short hand to saturate COLOR by PERCENT.
415 See `color-darken-hsl'."
416 (color-lighten-name name (- percent)))
418 (provide 'color)
420 ;;; color.el ends here