Skip to content

Commit a486f77

Browse files
committed
Add common blend mode functions
1 parent 5b02b8f commit a486f77

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

adafruit_pycamera/imageprocessing.py

+120
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,123 @@ def emboss_greyscale(bitmap, mask=None):
8282
def ironbow(bitmap, mask=None):
8383
"""Convert an image to false color using the 'ironbow palette'"""
8484
return bitmapfilter.false_color(bitmap, ironbow_palette, mask=mask)
85+
86+
87+
# pylint: disable=invalid-name
88+
def alphablend_func_factory(frac, nfrac=None):
89+
"""Create an alpha-blending function for a specific fractional value
90+
91+
The resulting function can be used with ``bitmapfilter.blend`` and
92+
``bitmapfilter.blend_precompute``.
93+
"""
94+
if nfrac is None:
95+
nfrac = 1 - frac
96+
97+
def inner(a, b):
98+
return frac * a + nfrac * b
99+
100+
return inner
101+
102+
103+
def screen_func(a, b):
104+
"""The 'screen' blend mode.
105+
106+
This function can be used with ``bitmapfilter.blend`` and
107+
``bitmapfilter.blend_precompute``."""
108+
return 1 - (1 - a) * (1 - b)
109+
110+
111+
def overlay_func(a, b):
112+
"""The 'overlay' blend mode.
113+
114+
This function can be used with ``bitmapfilter.blend`` and
115+
``bitmapfilter.blend_precompute``."""
116+
return 2 * a * b if a < 0.5 else 1 - 2 * (1 - a) * (1 - b)
117+
118+
119+
def hard_light_func(a, b):
120+
"""The 'hard light' blend mode.
121+
122+
This function can be used with ``bitmapfilter.blend`` and
123+
``bitmapfilter.blend_precompute``."""
124+
return 2 * a * b if b < 0.5 else 1 - 2 * (1 - a) * (1 - b)
125+
126+
127+
# illusions.hu formula version
128+
def soft_light_func(a, b):
129+
"""The 'soft light' blend mode.
130+
131+
There are various soft light blend functions. The "illusions.hu" variant of
132+
soft light is used.
133+
134+
This function can be used with ``bitmapfilter.blend`` and
135+
``bitmapfilter.blend_precompute``."""
136+
return a ** (2 ** (2 * 0.5 - b))
137+
138+
139+
def color_dodge_func(a, b):
140+
"""The 'color dodge' blend mode.
141+
142+
This function can be used with ``bitmapfilter.blend`` and
143+
``bitmapfilter.blend_precompute``."""
144+
return a / (1 - b) if b != 1 else 1
145+
146+
147+
def linear_dodge_func(a, b):
148+
"""The 'linear dodge' blend mode.
149+
150+
This function can be used with ``bitmapfilter.blend`` and
151+
``bitmapfilter.blend_precompute``."""
152+
return a + b
153+
154+
155+
def divide_func(a, b):
156+
"""The 'divide' blend mode.
157+
158+
This function can be used with ``bitmapfilter.blend`` and
159+
``bitmapfilter.blend_precompute``."""
160+
return a / b if b else 1
161+
162+
163+
def multiply_func(a, b):
164+
"""The 'multiply' blend mode.
165+
166+
This function can be used with ``bitmapfilter.blend`` and
167+
``bitmapfilter.blend_precompute``."""
168+
return a * b
169+
170+
171+
def subtract_func(a, b):
172+
"""The 'subtract' blend mode.
173+
174+
This function can be used with ``bitmapfilter.blend`` and
175+
``bitmapfilter.blend_precompute``."""
176+
return a - b
177+
178+
179+
def color_burn_func(a, b):
180+
"""The 'color burn' blend mode.
181+
182+
This function can be used with ``bitmapfilter.blend`` and
183+
``bitmapfilter.blend_precompute``."""
184+
return a * (1 - b)
185+
186+
187+
def linear_burn_func(a, b):
188+
"""The 'linear burn' blend mode.
189+
190+
This function can be used with ``bitmapfilter.blend`` and
191+
``bitmapfilter.blend_precompute``."""
192+
return a + b - 1
193+
194+
195+
darken_only_func = min
196+
"""The 'darken only' blend mode.
197+
198+
This function can be used with ``bitmapfilter.blend`` and
199+
``bitmapfilter.blend_precompute``."""
200+
lighten_only_func = max
201+
"""The 'screen' blend mode.
202+
203+
This function can be used with ``bitmapfilter.blend`` and
204+
``bitmapfilter.blend_precompute``."""

0 commit comments

Comments
 (0)