forked from ralph-schleicher/rs-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.lisp
407 lines (353 loc) · 12.6 KB
/
utilities.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;;; utilities.lisp --- utility definitions.
;; Copyright (C) 2014 Ralph Schleicher
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;;
;; * Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in
;; the documentation and/or other materials provided with the
;; distribution.
;;
;; * Neither the name of the copyright holder nor the names of its
;; contributors may be used to endorse or promote products derived
;; from this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
;; COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
;; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
;; ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.
;;; Code:
(in-package :rs-colors-internal)
;;;; Data and Control Flow
(export 'defconst)
(defmacro defconst (name value &optional doc)
"Define a constant variable.
This is like ‘defconstant’ except that the initially set value
is reused when the ‘defconst’ form is evaluated again."
`(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
,@(when doc (list doc))))
(export 'defsubst)
(defmacro defsubst (name arg-list &body body)
"Define an inline function.
This is like ‘defun’ except that the function is globally marked
for inline expansion by the compiler."
`(progn
(declaim (inline ,name))
(defun ,name ,arg-list
,@body)))
;;;; Conditions
(export 'ensure-type)
(defun ensure-type (object type)
"Signal a type error if OBJECT is not of the type TYPE.
Otherwise, return OBJECT."
(if (typep object type)
object
(error (make-condition 'type-error :datum object :expected-type type))))
;;;; Exponential Functions
(export 'cube)
(defun cube (z)
"Return Z cubed, that is Z raised to the power three.
Argument Z has to be a real number."
(declare (type real z))
(cond ((= z 0)
0)
((= z 1)
1)
(t
(* z z z))))
(export 'cube-root)
(defun cube-root (z)
"Return the cube root of Z.
Argument Z has to be a real number.
If argument Z is zero, value is zero. If argument Z is
a real number, value is the real cube root of Z.
The `cube-root' function attempts to propagate the type
of the argument Z to its value."
(declare (type real z))
(cond ((= z 0)
0)
((= z 1)
1)
(t
(let ((f (* (signum z) (expt (abs z) 1/3))))
(if (rationalp z)
(let ((r (rationalize f)))
(if (or (= r f) (= (cube r) z)) r f))
f)))))
;;;; Quantities
(export 'radian-from-degree)
(defsubst radian-from-degree (deg)
"Convert a plane angle from degree to radian.
Argument DEG is the angle given in degree.
Value is the corresponding angle given in radian."
(declare (type real deg))
(* (/ deg 180) pi))
(export 'degree-from-radian)
(defsubst degree-from-radian (rad)
"Convert a plane angle from radian to degree.
Argument RAD is the angle given in radian.
Value is the corresponding angle given in degree."
(declare (type real rad))
(* (/ rad pi) 180))
;;;; Linear Algebra
(export 'make-vector)
(defun make-vector (&optional (x1 0) (x2 0) (x3 0))
"Create a vector."
(let ((x (make-array '(3) :element-type 'real :initial-element 0)))
(setf (svref x 0) x1
(svref x 1) x2
(svref x 2) x3)
x))
(export 'copy-vector)
(defun copy-vector (x)
"Return a copy of vector X."
(make-vector (svref x 0)
(svref x 1)
(svref x 2)))
(export 'make-matrix)
(defun make-matrix (&optional (a11 0) (a12 0) (a13 0)
(a21 0) (a22 0) (a23 0)
(a31 0) (a32 0) (a33 0))
"Create a matrix."
(let ((a (make-array '(3 3) :element-type 'real :initial-element 0)))
(setf (aref a 0 0) a11 (aref a 0 1) a12 (aref a 0 2) a13
(aref a 1 0) a21 (aref a 1 1) a22 (aref a 1 2) a23
(aref a 2 0) a31 (aref a 2 1) a32 (aref a 2 2) a33)
a))
(export 'copy-matrix)
(defun copy-matrix (a)
"Return a copy of matrix A."
(make-matrix (aref a 0 0) (aref a 0 1) (aref a 0 2)
(aref a 1 0) (aref a 1 1) (aref a 1 2)
(aref a 2 0) (aref a 2 1) (aref a 2 2)))
(defun det (a)
"Return the determinant of matrix A."
(+ (* (aref a 0 0) (- (* (aref a 1 1) (aref a 2 2))
(* (aref a 1 2) (aref a 2 1))))
(* (aref a 0 1) (- (* (aref a 1 2) (aref a 2 0))
(* (aref a 1 0) (aref a 2 2))))
(* (aref a 0 2) (- (* (aref a 1 0) (aref a 2 1))
(* (aref a 1 1) (aref a 2 0))))))
(defun matrix-transpose (a)
"Transpose matrix A in place."
(rotatef (aref a 1 0) (aref a 0 1))
(rotatef (aref a 2 0) (aref a 0 2))
(rotatef (aref a 2 1) (aref a 1 2))
a)
(defun matrix-cofactors (a)
"Calculate matrix of cofactors of matrix A in place."
(psetf (aref a 0 0) (- (* (aref a 1 1) (aref a 2 2))
(* (aref a 1 2) (aref a 2 1)))
(aref a 0 1) (- (* (aref a 1 2) (aref a 2 0))
(* (aref a 1 0) (aref a 2 2)))
(aref a 0 2) (- (* (aref a 1 0) (aref a 2 1))
(* (aref a 1 1) (aref a 2 0)))
(aref a 1 0) (- (* (aref a 0 2) (aref a 2 1))
(* (aref a 0 1) (aref a 2 2)))
(aref a 1 1) (- (* (aref a 0 0) (aref a 2 2))
(* (aref a 0 2) (aref a 2 0)))
(aref a 1 2) (- (* (aref a 0 1) (aref a 2 0))
(* (aref a 0 0) (aref a 2 1)))
(aref a 2 0) (- (* (aref a 0 1) (aref a 1 2))
(* (aref a 0 2) (aref a 1 1)))
(aref a 2 1) (- (* (aref a 0 2) (aref a 1 0))
(* (aref a 0 0) (aref a 1 2)))
(aref a 2 2) (- (* (aref a 0 0) (aref a 1 1))
(* (aref a 0 1) (aref a 1 0))))
a)
(defun matrix-adjugate (a)
"Calculate adjugate matrix of matrix A in place."
(matrix-transpose (matrix-cofactors a)))
(export 'matrix-inverse)
(defun matrix-inverse (a)
"Calculate inverse matrix of matrix A in place."
(let ((det (det a)))
(when (zerop det)
(error (make-condition 'division-by-zero :operation 'matrix-inverse :operands (list a))))
(matrix-adjugate a)
(setf (aref a 0 0) (/ (aref a 0 0) det)
(aref a 0 1) (/ (aref a 0 1) det)
(aref a 0 2) (/ (aref a 0 2) det)
(aref a 1 0) (/ (aref a 1 0) det)
(aref a 1 1) (/ (aref a 1 1) det)
(aref a 1 2) (/ (aref a 1 2) det)
(aref a 2 0) (/ (aref a 2 0) det)
(aref a 2 1) (/ (aref a 2 1) det)
(aref a 2 2) (/ (aref a 2 2) det)))
a)
(defun gemv (a x &optional (y (make-vector)))
"General matrix/vector multiplication."
(psetf (svref y 0) (+ (* (aref a 0 0) (svref x 0))
(* (aref a 0 1) (svref x 1))
(* (aref a 0 2) (svref x 2)))
(svref y 1) (+ (* (aref a 1 0) (svref x 0))
(* (aref a 1 1) (svref x 1))
(* (aref a 1 2) (svref x 2)))
(svref y 2) (+ (* (aref a 2 0) (svref x 0))
(* (aref a 2 1) (svref x 1))
(* (aref a 2 2) (svref x 2))))
y)
(defun gemm (a b &optional (c (make-matrix)))
"General matrix/matrix multiplication."
(psetf (aref c 0 0) (+ (* (aref a 0 0) (aref b 0 0))
(* (aref a 0 1) (aref b 1 0))
(* (aref a 0 2) (aref b 2 0)))
(aref c 0 1) (+ (* (aref a 0 0) (aref b 0 1))
(* (aref a 0 1) (aref b 1 1))
(* (aref a 0 2) (aref b 2 1)))
(aref c 0 2) (+ (* (aref a 0 0) (aref b 0 2))
(* (aref a 0 1) (aref b 1 2))
(* (aref a 0 2) (aref b 2 2)))
(aref c 1 0) (+ (* (aref a 1 0) (aref b 0 0))
(* (aref a 1 1) (aref b 1 0))
(* (aref a 1 2) (aref b 2 0)))
(aref c 1 1) (+ (* (aref a 1 0) (aref b 0 1))
(* (aref a 1 1) (aref b 1 1))
(* (aref a 1 2) (aref b 2 1)))
(aref c 1 2) (+ (* (aref a 1 0) (aref b 0 2))
(* (aref a 1 1) (aref b 1 2))
(* (aref a 1 2) (aref b 2 2)))
(aref c 2 0) (+ (* (aref a 2 0) (aref b 0 0))
(* (aref a 2 1) (aref b 1 0))
(* (aref a 2 2) (aref b 2 0)))
(aref c 2 1) (+ (* (aref a 2 0) (aref b 0 1))
(* (aref a 2 1) (aref b 1 1))
(* (aref a 2 2) (aref b 2 1)))
(aref c 2 2) (+ (* (aref a 2 0) (aref b 0 2))
(* (aref a 2 1) (aref b 1 2))
(* (aref a 2 2) (aref b 2 2))))
c)
(export 'float-array)
(defun float-array (a &optional (prototype 1F0))
"Convert elements of a numeric array to floating-point numbers."
(iter (for k :from 0 :below (array-total-size a))
(setf (row-major-aref a k) (float (row-major-aref a k) prototype)))
a)
;; GEMV with multiple values.
(export 'linear-transformation)
(defun linear-transformation (a x1 x2 x3)
"Perform a linear transformation."
(values (+ (* (aref a 0 0) x1)
(* (aref a 0 1) x2)
(* (aref a 0 2) x3))
(+ (* (aref a 1 0) x1)
(* (aref a 1 1) x2)
(* (aref a 1 2) x3))
(+ (* (aref a 2 0) x1)
(* (aref a 2 1) x2)
(* (aref a 2 2) x3))))
;;;; Tuple
(export 'encode-triple)
(defun encode-triple (a b c &optional (byte-size 8))
(let ((s (expt 2 byte-size)))
(+ (* (+ (* a s) b) s) c)))
(export 'decode-triple)
(defun decode-triple (value &optional (byte-size 8))
(let ((s (expt 2 byte-size))
(v value)
(a 0)
(b 0)
(c 0))
(multiple-value-setq (v c)
(truncate v s))
(multiple-value-setq (a b)
(truncate v s))
(values a b c)))
(export 'encode-quadruple)
(defun encode-quadruple (a b c d &optional (byte-size 8))
(let ((s (expt 2 byte-size)))
(+ (* (+ (* (+ (* a s) b) s) c) s) d)))
(export 'decode-quadruple)
(defun decode-quadruple (value &optional (byte-size 8))
(let ((s (expt 2 byte-size))
(v value)
(a 0)
(b 0)
(c 0)
(d 0))
(multiple-value-setq (v d)
(truncate v s))
(multiple-value-setq (v c)
(truncate v s))
(multiple-value-setq (a b)
(truncate v s))
(values a b c d)))
;;;; CIE Color Spaces
(export 'cie-L*-from-Y/Yn)
(defun cie-L*-from-Y/Yn (Y/Yn)
"Map relative luminance Y/Yn to lightness L*."
(if (> Y/Yn 216/24389)
(- (* 116 (cube-root Y/Yn)) 16)
(* 24389/27 Y/Yn)))
(export 'cie-Y/Yn-from-L*)
(defun cie-Y/Yn-from-L* (L*)
"Map lightness L* to relative luminance Y/Yn."
(if (> L* 8)
(cube (/ (+ L* 16) 116))
(* 27/24389 L*)))
;;;; RGB Color Spaces
(export 'rgb-transformation-matrices)
(defun rgb-transformation-matrices (red green blue white)
"Return the transformation matrix for converting CIE XYZ color space
coordinates into RGB color space coordinates and vice versa.
Arguments RED, GREEN, BLUE, and WHITE are the chromaticity coordinates
of the red primary, the green primary, the blue primary, and the white
point respectively.
First value is the transformation matrix for converting CIE XYZ color
space coordinates into RGB color space coordinates. Second value is
the inverse matrix."
(labels ((xyz-from-xy (v)
(let ((x (elt v 0))
(y (elt v 1)))
(make-vector x y (- 1 x y)))))
;; See <http://docs-hoffmann.de/ciexyz29082000.pdf>, §11.4, page 15.
(let* ((r (xyz-from-xy red))
(g (xyz-from-xy green))
(b (xyz-from-xy blue))
(w (xyz-from-xy white))
;; Scale white point in advance.
(w* (let ((y (svref w 1)))
(make-vector (/ (svref w 0) y) 1 (/ (svref w 2) y))))
(p (make-matrix (svref r 0) (svref g 0) (svref b 0)
(svref r 1) (svref g 1) (svref b 1)
(svref r 2) (svref g 2) (svref b 2)))
(s (gemv (matrix-inverse (copy-matrix p)) w*))
(d (make-matrix (svref s 0) 0 0
0 (svref s 1) 0
0 0 (svref s 2)))
(c (gemm p d)))
(values (matrix-inverse (copy-matrix c)) c))))
(export 'make-rgb-color)
(defun make-rgb-color (color-type red green blue &optional byte-size)
"Create a new color in an RGB color space."
(let (r g b)
(if (not byte-size)
(setf r (ensure-type red '(real 0 1))
g (ensure-type green '(real 0 1))
b (ensure-type blue '(real 0 1)))
(let ((s (1- (expt 2 (ensure-type byte-size '(integer 1))))))
(setf r (/ (ensure-type red `(integer 0 ,s)) s)
g (/ (ensure-type green `(integer 0 ,s)) s)
b (/ (ensure-type blue `(integer 0 ,s)) s))))
(make-instance color-type :red r :green g :blue b)))
(export 'make-rgb-color-from-number)
(defun make-rgb-color-from-number (color-type value &optional (byte-size 8))
"Create a new color in an RGB color space."
(ensure-type value '(integer 0))
(ensure-type byte-size '(integer 1))
(multiple-value-bind (red green blue)
(decode-triple value byte-size)
(make-rgb-color color-type red green blue byte-size)))
;;; utilities.lisp ends here