Skip to content

Commit 1ff6e1a

Browse files
committed
Add namedtuple objects for mix() operation variants
1 parent c8bb1f5 commit 1ff6e1a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

shared-bindings/bitmapfilter/__init__.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <math.h>
2828

2929
#include "py/runtime.h"
30+
#include "py/objnamedtuple.h"
3031
#include "shared-bindings/displayio/Bitmap.h"
3132
#include "shared-bindings/displayio/Palette.h"
3233
#include "shared-bindings/bitmapfilter/__init__.h"
@@ -164,6 +165,81 @@ static mp_float_t float_subscr(mp_obj_t o, int i) {
164165
return mp_obj_get_float(subscr(o, i));
165166
}
166167

168+
//| ChannelScale = namedtuple("ChannelScale", ["r", "g", "b"])
169+
//| """A weight object to use with mix() that scales each channel
170+
//| independently."""
171+
static const mp_obj_namedtuple_type_t bitmapfilter_channel_scale_type = {
172+
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_ChannelScale),
173+
.n_fields = 3,
174+
.fields = {
175+
MP_QSTR_r,
176+
MP_QSTR_g,
177+
MP_QSTR_b,
178+
},
179+
};
180+
//| ChannelScaleOffset = namedtuple("ChannelScale", ["r", "g", "b", "r_add", "g_add", "b_add"])
181+
//| """A weight object to use with mix() that scales each channel
182+
//| independently and adds an offset."""
183+
static const mp_obj_namedtuple_type_t bitmapfilter_channel_scale_offset_type = {
184+
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_ChannelScaleOffset),
185+
.n_fields = 6,
186+
.fields = {
187+
MP_QSTR_r,
188+
MP_QSTR_g,
189+
MP_QSTR_b,
190+
MP_QSTR_r_add,
191+
MP_QSTR_g_add,
192+
MP_QSTR_b_add,
193+
},
194+
};
195+
196+
//| ChannelMixer = namedtuple(
197+
//| "ChannelMixer", ["rr", "rg", "rb", "gr", "gg", "gb", "br", "bg", "bb"]
198+
//| )
199+
//| """A weight object to use with mix() that mixes in portions of each
200+
//| channel into every other channel. For instance ``rg`` gives the portion of the green channel to mix into red."""
201+
static const mp_obj_namedtuple_type_t bitmapfilter_channel_mixer_type = {
202+
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_ChannelMixer),
203+
.n_fields = 9,
204+
.fields = {
205+
MP_QSTR_rr,
206+
MP_QSTR_rg,
207+
MP_QSTR_rb,
208+
MP_QSTR_gr,
209+
MP_QSTR_gg,
210+
MP_QSTR_gb,
211+
MP_QSTR_br,
212+
MP_QSTR_bg,
213+
MP_QSTR_bb,
214+
},
215+
};
216+
217+
//| ChannelMixerOffset = namedtuple(
218+
//| "ChannelMixerOffset",
219+
//| ["rr", "rg", "rb", "r_add", "gr", "gg", "gb", "g_add", "br", "bg", "bb", "b_add"],
220+
//| )
221+
//| """A weight object to use with mix() that mixes in portions of each
222+
//| channel into every other channel and adds an offset. For instance ``rg`` gives the portion of the green channel to mix into red."""
223+
//|
224+
static const mp_obj_namedtuple_type_t bitmapfilter_channel_mixer_offset_type = {
225+
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_ChannelMixerOffset),
226+
.n_fields = 9,
227+
.fields = {
228+
MP_QSTR_rr,
229+
MP_QSTR_rg,
230+
MP_QSTR_rb,
231+
MP_QSTR_r_add,
232+
MP_QSTR_gr,
233+
MP_QSTR_gg,
234+
MP_QSTR_gb,
235+
MP_QSTR_g_add,
236+
MP_QSTR_br,
237+
MP_QSTR_bg,
238+
MP_QSTR_bb,
239+
MP_QSTR_b_add,
240+
},
241+
};
242+
167243
//| def mix(
168244
//| bitmap: displayio.Bitmap, weights: Sequence[int], mask: displayio.Bitmap | None = None
169245
//| ) -> displayio.Bitmap:
@@ -429,6 +505,10 @@ STATIC const mp_rom_map_elem_t bitmapfilter_module_globals_table[] = {
429505
{ MP_ROM_QSTR(MP_QSTR_solarize), MP_ROM_PTR(&bitmapfilter_solarize_obj) },
430506
{ MP_ROM_QSTR(MP_QSTR_false_color), MP_ROM_PTR(&bitmapfilter_false_color_obj) },
431507
{ MP_ROM_QSTR(MP_QSTR_lookup), MP_ROM_PTR(&bitmapfilter_lookup_obj) },
508+
{ MP_ROM_QSTR(MP_QSTR_ChannelScale), MP_ROM_PTR(&bitmapfilter_channel_scale_type) },
509+
{ MP_ROM_QSTR(MP_QSTR_ChannelScaleOffset), MP_ROM_PTR(&bitmapfilter_channel_scale_offset_type) },
510+
{ MP_ROM_QSTR(MP_QSTR_ChannelMixer), MP_ROM_PTR(&bitmapfilter_channel_mixer_type) },
511+
{ MP_ROM_QSTR(MP_QSTR_ChannelMixerOffset), MP_ROM_PTR(&bitmapfilter_channel_mixer_offset_type) },
432512
};
433513
STATIC MP_DEFINE_CONST_DICT(bitmapfilter_module_globals, bitmapfilter_module_globals_table);
434514

0 commit comments

Comments
 (0)