Document reserved keys
[emacs.git] / lisp / color.el
blob99813549f30c82700850a9e126eb437a0aba4a80
1 ;;; color.el --- Color manipulation library -*- lexical-binding:t -*-
3 ;; Copyright (C) 2010-2018 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 <https://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 ;; Emacs < 23.3
37 (eval-and-compile
38 (unless (boundp 'float-pi)
39 (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...).")))
41 ;;;###autoload
42 (defun color-name-to-rgb (color &optional frame)
43 "Convert COLOR string to a list of normalized RGB components.
44 COLOR should be a color name (e.g. \"white\") or an RGB triplet
45 string (e.g. \"#ffff1122eecc\").
47 Normally the return value is a list of three floating-point
48 numbers, (RED GREEN BLUE), each between 0.0 and 1.0 inclusive.
50 Optional argument FRAME specifies the frame where the color is to be
51 displayed. If FRAME is omitted or nil, use the selected frame.
52 If FRAME cannot display COLOR, return nil."
53 ;; `colors-values' maximum value is either 65535 or 65280 depending on the
54 ;; display system. So we use a white conversion to get the max value.
55 (let ((valmax (float (car (color-values "#ffffffffffff")))))
56 (mapcar (lambda (x) (/ x valmax)) (color-values color frame))))
58 (defun color-rgb-to-hex (red green blue &optional digits-per-component)
59 "Return hexadecimal #RGB notation for the color specified by RED GREEN BLUE.
60 RED, GREEN, and BLUE should be numbers between 0.0 and 1.0, inclusive.
61 Optional argument DIGITS-PER-COMPONENT can be either 4 (the default)
62 or 2; use the latter if you need a 24-bit specification of a color."
63 (or digits-per-component (setq digits-per-component 4))
64 (let* ((maxval (if (= digits-per-component 2) 255 65535))
65 (fmt (if (= digits-per-component 2) "#%02x%02x%02x" "#%04x%04x%04x")))
66 (format fmt (* red maxval) (* green maxval) (* blue maxval))))
68 (defun color-complement (color-name)
69 "Return the color that is the complement of COLOR-NAME.
70 COLOR-NAME should be a string naming a color (e.g. \"white\"), or
71 a string specifying a color's RGB
72 components (e.g. \"#ffff1212ecec\")."
73 (let ((color (color-name-to-rgb color-name)))
74 (list (- 1.0 (nth 0 color))
75 (- 1.0 (nth 1 color))
76 (- 1.0 (nth 2 color)))))
78 (defun color-gradient (start stop step-number)
79 "Return a list with STEP-NUMBER colors from START to STOP.
80 The color list builds a color gradient starting at color START to
81 color STOP. It does not include the START and STOP color in the
82 resulting list."
83 (let* ((r (nth 0 start))
84 (g (nth 1 start))
85 (b (nth 2 start))
86 (interval (float (1+ step-number)))
87 (r-step (/ (- (nth 0 stop) r) interval))
88 (g-step (/ (- (nth 1 stop) g) interval))
89 (b-step (/ (- (nth 2 stop) b) interval))
90 result)
91 (dotimes (_ step-number)
92 (push (list (setq r (+ r r-step))
93 (setq g (+ g g-step))
94 (setq b (+ b b-step)))
95 result))
96 (nreverse result)))
98 (defun color-hue-to-rgb (v1 v2 h)
99 "Compute hue from V1 and V2 H.
100 Used internally by `color-hsl-to-rgb'."
101 (cond
102 ((< h (/ 6.0)) (+ v1 (* (- v2 v1) h 6.0)))
103 ((< h 0.5) v2)
104 ((< h (/ 2.0 3)) (+ v1 (* (- v2 v1) (- (/ 2.0 3) h) 6.0)))
105 (t v1)))
107 (defun color-hsl-to-rgb (H S L)
108 "Convert hue, saturation and luminance to their RGB representation.
109 H, S, and L should each be numbers between 0.0 and 1.0, inclusive.
110 Return a list (RED GREEN BLUE), where each element is between 0.0 and 1.0,
111 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 (mod (+ H (/ 3.0)) 1))
120 (color-hue-to-rgb m1 m2 H)
121 (color-hue-to-rgb m1 m2 (mod (- H (/ 3.0)) 1))))))
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 RGB 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 min)
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))))
157 (defun color-rgb-to-hsl (red green blue)
158 "Convert RGB colors to their HSL representation.
159 RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,
160 inclusive. Return a list (HUE SATURATION LUMINANCE), where
161 each element is between 0.0 and 1.0, inclusive."
162 (let* ((r red)
163 (g green)
164 (b blue)
165 (max (max r g b))
166 (min (min r g b))
167 (delta (- max min))
168 (l (/ (+ max min) 2.0)))
169 (if (= delta 0)
170 (list 0.0 0.0 l)
171 (let* ((s (if (<= l 0.5) (/ delta (+ max min))
172 (/ delta (- 2.0 max min))))
173 (rc (/ (- max r) delta))
174 (gc (/ (- max g) delta))
175 (bc (/ (- max b) delta))
176 (h (mod
178 (cond
179 ((= r max) (- bc gc))
180 ((= g max) (+ 2.0 rc (- bc)))
181 (t (+ 4.0 gc (- rc))))
182 6.0)
183 1.0)))
184 (list h s l)))))
186 (defun color-srgb-to-xyz (red green blue)
187 "Convert RED GREEN BLUE colors from the sRGB color space to CIE XYZ.
188 RED, GREEN and BLUE should be between 0.0 and 1.0, inclusive."
189 (let ((r (if (<= red 0.04045)
190 (/ red 12.95)
191 (expt (/ (+ red 0.055) 1.055) 2.4)))
192 (g (if (<= green 0.04045)
193 (/ green 12.95)
194 (expt (/ (+ green 0.055) 1.055) 2.4)))
195 (b (if (<= blue 0.04045)
196 (/ blue 12.95)
197 (expt (/ (+ blue 0.055) 1.055) 2.4))))
198 (list (+ (* 0.4124564 r) (* 0.3575761 g) (* 0.1804375 b))
199 (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b))
200 (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b)))))
202 (defun color-xyz-to-srgb (X Y Z)
203 "Convert CIE X Y Z colors to sRGB color space."
204 (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z)))
205 (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z)))
206 (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z))))
207 (list (if (<= r 0.0031308)
208 (* 12.92 r)
209 (- (* 1.055 (expt r (/ 2.4))) 0.055))
210 (if (<= g 0.0031308)
211 (* 12.92 g)
212 (- (* 1.055 (expt g (/ 2.4))) 0.055))
213 (if (<= b 0.0031308)
214 (* 12.92 b)
215 (- (* 1.055 (expt b (/ 2.4))) 0.055)))))
217 (defconst color-d75-xyz '(0.9497 1.0 1.2264)
218 "D75 white point in CIE XYZ.")
220 (defconst color-d65-xyz '(0.950455 1.0 1.088753)
221 "D65 white point in CIE XYZ.")
223 (defconst color-d55-xyz '(0.9568 1.0 0.9215)
224 "D55 white point in CIE XYZ.")
226 (defconst color-d50-xyz '(0.9642 1.0 0.8249)
227 "D50 white point in CIE XYZ.")
229 (defconst color-cie-ε (/ 216 24389.0))
230 (defconst color-cie-κ (/ 24389 27.0))
232 (defun color-xyz-to-lab (X Y Z &optional white-point)
233 "Convert CIE XYZ to CIE L*a*b*.
234 WHITE-POINT specifies the (X Y Z) white point for the
235 conversion. If omitted or nil, use `color-d65-xyz'."
236 (pcase-let* ((`(,Xr ,Yr ,Zr) (or white-point color-d65-xyz))
237 (xr (/ X Xr))
238 (yr (/ Y Yr))
239 (zr (/ Z Zr))
240 (fx (if (> xr color-cie-ε)
241 (expt xr (/ 3.0))
242 (/ (+ (* color-cie-κ xr) 16) 116.0)))
243 (fy (if (> yr color-cie-ε)
244 (expt yr (/ 3.0))
245 (/ (+ (* color-cie-κ yr) 16) 116.0)))
246 (fz (if (> zr color-cie-ε)
247 (expt zr (/ 3.0))
248 (/ (+ (* color-cie-κ zr) 16) 116.0))))
249 (list
250 (- (* 116 fy) 16) ; L
251 (* 500 (- fx fy)) ; a
252 (* 200 (- fy fz))))) ; b
254 (defun color-lab-to-xyz (L a b &optional white-point)
255 "Convert CIE L*a*b* to CIE XYZ.
256 WHITE-POINT specifies the (X Y Z) white point for the
257 conversion. If omitted or nil, use `color-d65-xyz'."
258 (pcase-let* ((`(,Xr ,Yr ,Zr) (or white-point color-d65-xyz))
259 (fy (/ (+ L 16) 116.0))
260 (fz (- fy (/ b 200.0)))
261 (fx (+ (/ a 500.0) fy))
262 (xr (if (> (expt fx 3.0) color-cie-ε)
263 (expt fx 3.0)
264 (/ (- (* fx 116) 16) color-cie-κ)))
265 (yr (if (> L (* color-cie-κ color-cie-ε))
266 (expt (/ (+ L 16) 116.0) 3.0)
267 (/ L color-cie-κ)))
268 (zr (if (> (expt fz 3) color-cie-ε)
269 (expt fz 3.0)
270 (/ (- (* 116 fz) 16) color-cie-κ))))
271 (list (* xr Xr) ; X
272 (* yr Yr) ; Y
273 (* zr Zr)))) ; Z
275 (defun color-srgb-to-lab (red green blue)
276 "Convert RGB to CIE L*a*b*."
277 (apply 'color-xyz-to-lab (color-srgb-to-xyz red green blue)))
279 (defun color-lab-to-srgb (L a b)
280 "Convert CIE L*a*b* to RGB."
281 (apply 'color-xyz-to-srgb (color-lab-to-xyz L a b)))
283 (defun color-xyz-to-xyy (X Y Z)
284 "Convert CIE XYZ to xyY."
285 (let ((d (float (+ X Y Z))))
286 (list (/ X d) (/ Y d) Y)))
288 (defun color-xyy-to-xyz (x y Y)
289 "Convert CIE xyY to XYZ."
290 (let ((y (float y)))
291 (list (/ (* Y x) y) Y (/ (* Y (- 1 x y)) y))))
293 (defun color-lab-to-lch (L a b)
294 "Convert CIE L*a*b* to L*C*h*"
295 (list L (sqrt (+ (* a a) (* b b))) (atan b a)))
297 (defun color-lch-to-lab (L C h)
298 "Convert CIE L*a*b* to L*C*h*"
299 (list L (* C (cos h)) (* C (sin h))))
301 (defun color-cie-de2000 (color1 color2 &optional kL kC kH)
302 "Return the CIEDE2000 color distance between COLOR1 and COLOR2.
303 Both COLOR1 and COLOR2 should be in CIE L*a*b* format, as
304 returned by `color-srgb-to-lab' or `color-xyz-to-lab'."
305 (pcase-let*
306 ((`(,L₁ ,a₁ ,b₁) color1)
307 (`(,L₂ ,a₂ ,b₂) color2)
308 (kL (or kL 1))
309 (kC (or kC 1))
310 (kH (or kH 1))
311 (C(sqrt (+ (expt a₁ 2.0) (expt b₁ 2.0))))
312 (C(sqrt (+ (expt a₂ 2.0) (expt b₂ 2.0))))
313 (C̄ (/ (+ C₁ C₂) 2.0))
314 (G (* 0.5 (- 1 (sqrt (/ (expt7.0)
315 (+ (expt7.0) (expt 25 7.0)))))))
316 (a′₁ (* (+ 1 G) a₁))
317 (a′₂ (* (+ 1 G) a₂))
318 (C′₁ (sqrt (+ (expt a′₁ 2.0) (expt b₁ 2.0))))
319 (C′₂ (sqrt (+ (expt a′₂ 2.0) (expt b₂ 2.0))))
320 (h′₁ (if (and (= b₁ 0) (= a′₁ 0))
322 (let ((v (atan b₁ a′₁)))
323 (if (< v 0)
324 (+ v (* 2 float-pi))
325 v))))
326 (h′₂ (if (and (= b₂ 0) (= a′₂ 0))
328 (let ((v (atan b₂ a′₂)))
329 (if (< v 0)
330 (+ v (* 2 float-pi))
331 v))))
332 (ΔL′ (- L₂ L₁))
333 (ΔC′ (- C′₂ C′₁))
334 (Δh′ (cond ((= (* C′₁ C′₂) 0)
336 ((<= (abs (- h′₂ h′₁)) float-pi)
337 (- h′₂ h′₁))
338 ((> (- h′₂ h′₁) float-pi)
339 (- (- h′₂ h′₁) (* 2 float-pi)))
340 ((< (- h′₂ h′₁) (- float-pi))
341 (+ (- h′₂ h′₁) (* 2 float-pi)))))
342 (ΔH′ (* 2 (sqrt (* C′₁ C′₂)) (sin (/ Δh′ 2.0))))
343 (L̄′ (/ (+ L₁ L₂) 2.0))
344 (C̄′ (/ (+ C′₁ C′₂) 2.0))
345 (h̄′ (cond ((= (* C′₁ C′₂) 0)
346 (+ h′₁ h′₂))
347 ((<= (abs (- h′₁ h′₂)) float-pi)
348 (/ (+ h′₁ h′₂) 2.0))
349 ((< (+ h′₁ h′₂) (* 2 float-pi))
350 (/ (+ h′₁ h′₂ (* 2 float-pi)) 2.0))
351 ((>= (+ h′₁ h′₂) (* 2 float-pi))
352 (/ (+ h′₁ h′₂ (* -2 float-pi)) 2.0))))
353 (T (+ 1
354 (- (* 0.17 (cos (- h̄′ (degrees-to-radians 30)))))
355 (* 0.24 (cos (* h̄′ 2)))
356 (* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
357 (- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
358 (Δθ (* (degrees-to-radians 30)
359 (exp (- (expt (/ (- h̄′ (degrees-to-radians 275))
360 (degrees-to-radians 25)) 2.0)))))
361 (Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
362 (Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0))
363 (sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
364 (Sc (+ 1 (* C̄′ 0.045)))
365 (Sh (+ 1 (* 0.015 C̄′ T)))
366 (Rt (- (* (sin (* Δθ 2)) Rc))))
367 (sqrt (+ (expt (/ ΔL′ (* Sl kL)) 2.0)
368 (expt (/ ΔC′ (* Sc kC)) 2.0)
369 (expt (/ ΔH′ (* Sh kH)) 2.0)
370 (* Rt (/ ΔC′ (* Sc kC)) (/ ΔH′ (* Sh kH)))))))
372 (defun color-clamp (value)
373 "Make sure VALUE is a number between 0.0 and 1.0 inclusive."
374 (min 1.0 (max 0.0 value)))
376 (defun color-saturate-hsl (H S L percent)
377 "Make a color more saturated by a specified amount.
378 Given a color defined in terms of hue, saturation, and luminance
379 \(arguments H, S, and L), return a color that is PERCENT more
380 saturated. Returns a list (HUE SATURATION LUMINANCE)."
381 (list H (color-clamp (+ S (/ percent 100.0))) L))
383 (defun color-saturate-name (name percent)
384 "Make a color with a specified NAME more saturated by PERCENT.
385 See `color-saturate-hsl'."
386 (apply 'color-rgb-to-hex
387 (apply 'color-hsl-to-rgb
388 (apply 'color-saturate-hsl
389 (append
390 (apply 'color-rgb-to-hsl
391 (color-name-to-rgb name))
392 (list percent))))))
394 (defun color-desaturate-hsl (H S L percent)
395 "Make a color less saturated by a specified amount.
396 Given a color defined in terms of hue, saturation, and luminance
397 \(arguments H, S, and L), return a color that is PERCENT less
398 saturated. Returns a list (HUE SATURATION LUMINANCE)."
399 (color-saturate-hsl H S L (- percent)))
401 (defun color-desaturate-name (name percent)
402 "Make a color with a specified NAME less saturated by PERCENT.
403 See `color-desaturate-hsl'."
404 (color-saturate-name name (- percent)))
406 (defun color-lighten-hsl (H S L percent)
407 "Make a color lighter by a specified amount.
408 Given a color defined in terms of hue, saturation, and luminance
409 \(arguments H, S, and L), return a color that is PERCENT lighter.
410 Returns a list (HUE SATURATION LUMINANCE)."
411 (list H S (color-clamp (+ L (/ percent 100.0)))))
413 (defun color-lighten-name (name percent)
414 "Make a color with a specified NAME lighter by PERCENT.
415 See `color-lighten-hsl'."
416 (apply 'color-rgb-to-hex
417 (apply 'color-hsl-to-rgb
418 (apply 'color-lighten-hsl
419 (append
420 (apply 'color-rgb-to-hsl
421 (color-name-to-rgb name))
422 (list percent))))))
424 (defun color-darken-hsl (H S L percent)
425 "Make a color darker by a specified amount.
426 Given a color defined in terms of hue, saturation, and luminance
427 \(arguments H, S, and L), return a color that is PERCENT darker.
428 Returns a list (HUE SATURATION LUMINANCE)."
429 (color-lighten-hsl H S L (- percent)))
431 (defun color-darken-name (name percent)
432 "Make a color with a specified NAME darker by PERCENT.
433 See `color-darken-hsl'."
434 (color-lighten-name name (- percent)))
436 (provide 'color)
438 ;;; color.el ends here