-
Notifications
You must be signed in to change notification settings - Fork 24
/
blend.clj
411 lines (342 loc) · 11.6 KB
/
blend.clj
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
408
409
410
411
;; http://www.pegtop.net/delphi/articles/blendmodes/
;; http://www.simplefilter.de/en/basics/mixmods.html
(ns clojure2d.color.blend
"Blending modes functions for colors, gradients and palettes.
Great collection of blending functions for color channel values. Channel values should be from `[0.0,255.0]` range.
Use [[blend-colors]], [[blend-palettes]] and [[blend-gradients]] to blend colors, palettes and gradients.
[[blend-colors]] uses simple alpha blending strategy as described [here](https://www.w3.org/TR/compositing-1/#blending).
Blending can be done separately for each channel."
(:refer-clojure :exclude [or and])
(:require [fastmath.core :as m]
[fastmath.vector :as v]
[clojure2d.color :as c])
(:import [fastmath.vector Vec4]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(defmacro ^:private clamp255 [v] `(m/constrain ~v 0.0 255.0))
(defmacro ^:private mod255 [v] `(mod ~v 256.0))
(def ^:const ^:private ^double r255 (/ 255.0))
(def ^:const ^:private ^double r255-2 (/ (* 255.0 255.0)))
(def ^:const ^:private ^double rsqrt255 (/ (m/sqrt 255)))
(defn normal
"Return second value only."
^double [^double _ ^double b] b)
(defn add
"Add channel values (clamped)."
^double [^double a ^double b]
(clamp255 (+ a b)))
(defn madd
"Add channel values (mod 255)."
^double [^double a ^double b]
(mod255 (+ a b)))
(defn subtract
"Subtract channel values (clamped)."
^double [^double a ^double b]
(clamp255 (- a b)))
(defn msubtract
"Subtract channel values (mod 255)."
^double [^double a ^double b]
(mod255 (- a b)))
(defn linearburn
"Linear burn mode (clamped)."
^double [^double a ^double b]
(clamp255 (- (+ a b) 255.0)))
(defn mlinearburn
"Linear burn mode (mod 255)."
^double [^double a ^double b]
(mod255 (- (+ a b) 255.0)))
(defn darken
"Darken mode."
^double [^double a ^double b]
(min a b))
(defn lighten
"Lighten mode."
^double [^double a ^double b]
(max a b))
(defn multiply
"Multiply channel values."
^double [^double a ^double b]
(* a b r255))
(defn screen
"Screen mode."
^double [^double a ^double b]
(let [ra (- 255.0 a)
rb (- 255.0 b)]
(- 255.0 (* r255 rb ra ))))
(defn dodge
"Dodge mode (clamped)."
^double [^double a ^double b]
(clamp255 (/ (* 255.0 a) (max m/EPSILON (- 255.0 b)))))
(defn mdodge
"Dodge mode (mod 255)."
^double [^double a ^double b]
(mod255 (/ (* 255.0 a) (max m/EPSILON (- 255.0 b)))))
(defn inversedodge
"Inverse dodge mode (clamped)."
^double [^double a ^double b]
(clamp255 (/ (* 255.0 b) (max m/EPSILON (- 255.0 a)))))
(defn minversedodge
"Inverse dodge mode (mod 255)."
^double [^double a ^double b]
(mod255 (/ (* 255.0 b) (max m/EPSILON (- 255.0 a)))))
(defn softdodge
"Soft dodge mode (clamped)."
^double [^double a ^double b]
(if (<= (+ a b) 255.0)
(clamp255 (/ (* 127.5 a) (max m/EPSILON (- 255.0 b))))
(clamp255 (- 255.0 (* 127.5 (/ (- 255.0 b) (max m/EPSILON a)))))))
(defn msoftdodge
"Soft dodge mode (mod 255)."
^double [^double a ^double b]
(if (<= (+ a b) 255.0)
(mod255 (/ (* 127.5 a) (max m/EPSILON (- 255.0 b))))
(mod255 (- 255.0 (* 127.5 (/ (- 255.0 b) (max m/EPSILON a)))))))
(defn burn
"Burn mode (clamped)."
^double [^double a ^double b]
(clamp255 (* 255.0 (- 1.0 (/ (- 255.0 a) (max m/EPSILON b))))))
(defn mburn
"Burn mode (mod 255)."
^double [^double a ^double b]
(mod255 (* 255.0 (- 1.0 (/ (- 255.0 a) (max m/EPSILON b))))))
(defn inverseburn
"Inverse burn mode (clamped)."
^double [^double a ^double b]
(clamp255 (* 255.0 (- 1.0 (/ (- 255.0 b) (max m/EPSILON a))))))
(defn minverseburn
"Inverse burn mode (mod 255)."
^double [^double a ^double b]
(mod255 (* 255.0 (- 1.0 (/ (- 255.0 b) (max m/EPSILON a))))))
(defn softburn
"Soft burn mode (clamped)."
^double [^double a ^double b]
(if (<= (+ a b) 255.0)
(clamp255 (* 127.5 (/ b (max m/EPSILON (- 255.0 a)))))
(clamp255 (* 255.0 (- 0.5 (/ (- 255.0 a) (max m/EPSILON b)))))))
(defn msoftburn
"Soft burn mode (mod 255)."
^double [^double a ^double b]
(if (<= (+ a b) 255.0)
(mod255 (* 127.5 (/ b (max m/EPSILON (- 255.0 a)))))
(mod255 (* 255.0 (- 0.5 (/ (- 255.0 a) (max m/EPSILON b)))))))
(defn hardmix
"Hard mix channel values."
^double [^double a ^double b]
(let [t (- 255.0 b)]
(if (< a t) 0.0 255.0)))
(defn linearlight
"Linear light mode (clamped)."
^double [^double a ^double b]
(clamp255 (- (+ b a a) 255.0)))
(defn mlinearlight
"Linear light mode (mod 255)."
^double [^double a ^double b]
(mod255 (- (+ b a a) 255.0)))
(defn stamp
"Stamp mode (clamped)."
^double [^double a ^double b]
(clamp255 (- (+ b b a) 255.0)))
(defn mstamp
"Stamp mode (mod 255)."
^double [^double a ^double b]
(mod255 (- (+ b b a) 255.0)))
(defn difference
"Difference mode."
^double [^double a ^double b]
(m/abs (- a b)))
(defn divide
"Divide channel values (clamped)."
^double [^double a ^double b]
(clamp255 (* 255.0 (/ a (max m/EPSILON b)))))
(defn mdivide
"Divide channel values (mod 255)."
^double [^double a ^double b]
(mod255 (* 255.0 (/ a (max m/EPSILON b)))))
(defn or
"Bitwise `or` of channel values."
^double [^double a ^double b]
(double (bit-and 0xff (bit-or (unchecked-long a) (unchecked-long b)))))
(defn and
"Bitwise `and` of channel values."
^double [^double a ^double b]
(double (bit-and 0xff (bit-and (unchecked-long a) (unchecked-long b)))))
(defn xor
"Bitwise `xor` of channel values."
^double [^double a ^double b]
(double (bit-and 0xff (bit-xor (unchecked-long a) (unchecked-long b)))))
(defn exclusion
"Exclusion mode."
^double [^double a ^double b]
(let [ab (* a b r255)]
(- (+ a b) (+ ab ab))))
(defn pinlight
"Pin light mode."
^double [^double a ^double b]
(let [c (- (+ a a) 255.0)]
(cond (< b c) c
(clojure.core/and (<= c b) (< b (+ c 255.0))) b
:else (+ c 255.0))))
(defn average
"Average of two channel values."
^double [^double a ^double b]
(/ (+ a b) 2.0))
(defn negation
"Negation mode."
^double [^double a ^double b]
(- 255.0 (m/abs (- 255.0 a b))))
(defn reflect
"Reflect mode (clamped)."
^double [^double a ^double b]
(clamp255 (/ (* a a) (max m/EPSILON (- 255.0 b)))))
(defn mreflect
"Reflec mode (mod 255)."
^double [^double a ^double b]
(mod255 (/ (* a a) (max m/EPSILON (- 255.0 b)))))
(defn glow
"Glow mode (clamped)."
^double [^double a ^double b]
(clamp255 (/ (* b b) (max m/EPSILON (- 255.0 a)))))
(defn mglow
"Glow mode (mod 255)."
^double [^double a ^double b]
(mod255 (/ (* b b) (max m/EPSILON (- 255.0 a)))))
(defn freeze
"Freeze mode (clamped)."
^double [^double a ^double b]
(clamp255 (- 255.0 (/ (m/sq (- 255.0 a)) (max m/EPSILON b)))))
(defn mfreeze
"Freeze mode (mod 255)."
^double [^double a ^double b]
(mod255 (- 255.0 (/ (m/sq (- 255.0 a)) (max m/EPSILON b)))))
(defn heat
"Heat mode (clamped)."
^double [^double a ^double b]
(clamp255 (- 255.0 (/ (m/sq (- 255.0 b)) (max m/EPSILON a)))))
(defn mheat
"Heat mode (mod 255)."
^double [^double a ^double b]
(mod255 (- 255.0 (/ (m/sq (- 255.0 b)) (max m/EPSILON a)))))
(defn overlay
"Overlay mode."
^double [^double a ^double b]
(if (< a 127.5)
(* 2.0 (* a b r255))
(- 255.0 (* 2.0 (* r255 (- 255.0 a) (- 255.0 b))))))
(defn hardlight
"Hard light mode."
^double [^double a ^double b]
(if (< b 127.5)
(* 2.0 (* a b r255))
(- 255.0 (* 2.0 (* r255 (- 255.0 a) (- 255.0 b))))))
(defn softlight
"Soft light mode."
^double [^double a ^double b]
(if (< b 127.5)
(+ a (* (+ b b -255.0) (* a (- 255.0 a) r255-2)))
(+ a (* (+ b b -255.0) (- (* (m/sqrt a) rsqrt255)
(* a r255))))))
(defn pegtoplight
"Pegtop soft light mode."
^double [^double a ^double b]
(+ (* 2.0 a b r255-2 (- 255.0 a))
(* a a r255)))
(defn vividlight
"Vivid light mode (clamped)."
^double [^double a ^double b]
(if (< b 127.5)
(clamp255 (- 255.0 (/ (* 255.0 (- 255.0 a)) (max m/EPSILON (+ b b)))))
(clamp255 (/ (* 255.0 a) (max m/EPSILON (* 2.0 (- 255.0 b)))))))
(defn mvividlight
"Vivid light mode (mod 255)."
^double [^double a ^double b]
(if (< b 127.5)
(mod255 (- 255.0 (/ (* 255.0 (- 255.0 a)) (max m/EPSILON (+ b b)))))
(mod255 (/ (* 255.0 a) (max m/EPSILON (* 2.0 (- 255.0 b)))))))
(def blends {:normal normal
:add add
:madd madd
:subtract subtract
:msubtract msubtract
:linearburn linearburn
:mlinearburn mlinearburn
:darken darken
:lighten lighten
:multiply multiply
:screen screen
:dodge dodge
:mdodge mdodge
:inversedodge inversedodge
:minversedodge minversedodge
:softdodge softdodge
:msoftdodge msoftdodge
:burn burn
:mburn mburn
:inverseburn inverseburn
:minverseburn minverseburn
:softburn softburn
:msoftburn msoftburn
:hardmix hardmix
:linearlight linearlight
:mlinearlight mlinearlight
:stamp stamp
:mstamp mstamp
:difference difference
:divide divide
:mdivide mdivide
:or or
:and and
:xor xor
:exclusion exclusion
:pinlight pinlight
:average average
:negation negation
:reflect reflect
:mreflect mreflect
:glow glow
:mglow mglow
:freeze freeze
:mfreeze mfreeze
:heat heat
:mheat mheat
:overlay overlay
:hardlight hardlight
:softlight softlight
:pegtoplight pegtoplight
:vividlight vividlight
:mvividlight mvividlight})
(def ^{:doc "List of all blending functions."} blends-list (sort (keys blends)))
(defn blend-colors
"Blend two colors using simple alpha composing.
Each channel can be blended using different function.
Formula from [w3.org spec](https://www.w3.org/TR/compositing-1/#blending)."
(^Vec4 [blend-fn cb cs] (blend-colors blend-fn blend-fn blend-fn cb cs))
(^Vec4 [blend-fn1 blend-fn2 blend-fn3 cb cs]
(let [cb (c/to-color cb)
cs (c/to-color cs)
^double br (blend-fn1 (.x cb) (.x cs))
^double bg (blend-fn2 (.y cb) (.y cs))
^double bb (blend-fn3 (.z cb) (.z cs))]
(if (clojure.core/and (>= (.w cb) 255.0)
(>= (.w cs) 255.0))
(Vec4. br bg bb 255.0)
(let [ab (* r255 (.w cb))
as (* r255 (.w cs))
a1 (* as (- 1.0 ab))
a2 (* as ab)
a3 (* (- 1.0 as) ab)
a0 (+ as a3)]
(Vec4. (/ (+ (* a1 (.x cs)) (* a2 br) (* a3 (.x cb))) a0)
(/ (+ (* a1 (.y cs)) (* a2 bg) (* a3 (.y cb))) a0)
(/ (+ (* a1 (.z cs)) (* a2 bb) (* a3 (.z cb))) a0)
(* 255.0 a0)))))))
(defn blend-palettes
"Blend two palettes.
Each channel can be blended using different function."
([blend-fn pal1 pal2] (blend-palettes blend-fn blend-fn blend-fn pal1 pal2))
([blend-fn1 blend-fn2 blend-fn3 pal1 pal2] (mapv (partial blend-colors blend-fn1 blend-fn2 blend-fn3) pal1 pal2)))
(defn blend-gradients
"Blend two gradients.
Each channel can be blended using different function."
([blend-fn g1 g2] (blend-gradients blend-fn blend-fn blend-fn g1 g2))
([blend-fn1 blend-fn2 blend-fn3 g1 g2] (fn [^double t] (blend-colors blend-fn1 blend-fn2 blend-fn3 (g1 t) (g2 t)))))