color.el 15 KB

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