-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblend_modes.py
230 lines (167 loc) · 6.93 KB
/
blend_modes.py
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
import numba as nb
import numpy as np
from color import (
RGB_to_HSL,
RGB_to_HSV,
HSL_to_RGB,
HSV_to_RGB,
IMG_to_LCh_D65,
LCh_D65_to_IMG,
IMG_to_LCh_D50,
LCh_D50_to_IMG,
)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_lighten(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.maximum(base, top)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_screen(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return base + top - base * top
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_color_dodge(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.ones_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
t = top[y, x, i]
if t != 1:
result[y, x, i] = min(1, base[y, x, i] / (1 - t))
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_linear_dodge(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.minimum(1, base + top)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_darken(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.minimum(base, top)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_multiply(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return base * top
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_color_burn(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.zeros_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
t = top[y, x, i]
if t != 0:
result[y, x, i] = max(0, 1 - (1 - base[y, x, i]) / t)
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_linear_burn(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.maximum(0, base + top - 1.0)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_overlay(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.empty_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
b = base[y, x, i]
t = top[y, x, i]
result[y, x, i] = (
2 * b * t if b < 0.5 else 2 * b + 2 * t - 2 * b * t - 1
)
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_soft_light(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return (1 - 2 * top) * base**2 + 2 * base * top
@nb.njit
def blend_hard_light(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return blend_overlay(top, base)
@nb.njit(cache=True, fastmath=True)
def vivid_light(b: float, t: float) -> float:
if t < 0.5:
return max(0, 1 - (1 - b) / (2 * t)) if t else 0
else:
return min(1, b / (2 - 2 * t)) if t != 1 else 1
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_vivid_light(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.zeros_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
result[y, x, i] = vivid_light(base[y, x, i], top[y, x, i])
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_linear_light(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.clip(base + 2 * top - 1, 0.0, 1.0)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_pin_light(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.zeros_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
b = base[y, x, i]
t = top[y, x, i]
result[y, x, i] = min(b, 2 * t) if t < 0.5 else max(b, 2 * t - 1)
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_reflect(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.ones_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
t = top[y, x, i]
if t != 1:
result[y, x, i] = min(1, base[y, x, i] ** 2 / (1 - t))
return result
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_difference(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.abs(base - top)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_exclusion(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return base + top - 2 * base * top
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_subtract(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.maximum(0, base - top)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_grain_extract(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.clip(base + top - 0.5, 0, 1)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_grain_merge(base: np.ndarray, top: np.ndarray) -> np.ndarray:
return np.clip(base - top + 0.5, 0, 1)
@nb.njit(cache=True, fastmath=True, parallel=True)
def blend_divide(base: np.ndarray, top: np.ndarray) -> np.ndarray:
height, width = base.shape[:2]
result = np.ones_like(base)
for y in nb.prange(height):
for x in nb.prange(width):
for i in (0, 1, 2):
t = top[y, x, i]
if t != 0:
result[y, x, i] = min(1, base[y, x, i] / t)
return result
@nb.njit
def blend_HSV_Color(base: np.ndarray, top: np.ndarray) -> np.ndarray:
hsv = RGB_to_HSV(base)
hsv[..., 0:2] = RGB_to_HSV(top)[..., 0:2]
return HSV_to_RGB(hsv)
@nb.njit
def blend_HSL_Color(base: np.ndarray, top: np.ndarray) -> np.ndarray:
hsl = RGB_to_HSL(base)
hsl[..., 0:2] = RGB_to_HSL(top)[..., 0:2]
return HSL_to_RGB(hsl)
@nb.njit
def blend_color_lux(base: np.ndarray, top: np.ndarray) -> np.ndarray:
hsv = RGB_to_HSV(base)
hsv[..., 0:2] = RGB_to_HSV(top)[..., 0:2]
return HSL_to_RGB(hsv)
@nb.njit
def blend_color_nox(base: np.ndarray, top: np.ndarray) -> np.ndarray:
hsl = RGB_to_HSL(base)
hsl[..., 0:2] = RGB_to_HSL(top)[..., 0:2]
return HSV_to_RGB(hsl)
@nb.njit
def blend_color_LCh_D65(base: np.ndarray, top: np.ndarray) -> np.ndarray:
lch = IMG_to_LCh_D65(base)
lch[..., 1:3] = IMG_to_LCh_D65(top)[..., 1:3]
return LCh_D65_to_IMG(lch)
@nb.njit
def blend_color_LCh_D50(base: np.ndarray, top: np.ndarray) -> np.ndarray:
lch = IMG_to_LCh_D50(base)
lch[..., 1:3] = IMG_to_LCh_D50(top)[..., 1:3]
return LCh_D50_to_IMG(lch)