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