Skip to content

Commit 1d1d951

Browse files
authored
Merge pull request #68 from FoamyGuy/transparency_and_label_bg
transparency in icon image. Background color for label. Reviewed code, looks good to me. Merging.
2 parents da84037 + 6e3862d commit 1d1d951

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

adafruit_displayio_layout/widgets/icon_widget.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class IconWidget(Widget, Control):
4141
:param string icon: the filepath of the bmp image to be used as the icon.
4242
:param boolean on_disk: if True use OnDiskBitmap instead of imageload.
4343
This can be helpful to save memory. Defaults to False
44-
44+
:param Optional[int] transparent_index: if not None this color index will get set to
45+
transparent on the palette of the icon.
46+
:param Optional[int] label_background: if not None this color will be used as a background
47+
for the icon label.
4548
:param int x: x location the icon widget should be placed. Pixel coordinates.
4649
:param int y: y location the icon widget should be placed. Pixel coordinates.
4750
:param anchor_point: (X,Y) values from 0.0 to 1.0 to define the anchor point relative to the
@@ -51,16 +54,30 @@ class IconWidget(Widget, Control):
5154
:type anchored_position: Tuple[int, int]
5255
"""
5356

54-
def __init__(self, label_text, icon, on_disk=False, **kwargs):
57+
# pylint: disable=too-many-arguments
58+
59+
def __init__(
60+
self,
61+
label_text,
62+
icon,
63+
on_disk=False,
64+
transparent_index=None,
65+
label_background=None,
66+
**kwargs
67+
):
5568
super().__init__(**kwargs)
5669

5770
self._icon = icon
5871

5972
if on_disk:
6073
image = OnDiskBitmap(self._icon)
74+
if transparent_index is not None:
75+
image.pixel_shader.make_transparent(transparent_index)
6176
tile_grid = TileGrid(image, pixel_shader=image.pixel_shader)
6277
else:
6378
image, palette = adafruit_imageload.load(icon)
79+
if transparent_index is not None:
80+
palette.make_transparent(transparent_index)
6481
tile_grid = TileGrid(image, pixel_shader=palette)
6582
self.append(tile_grid)
6683
_label = bitmap_label.Label(
@@ -70,6 +87,10 @@ def __init__(self, label_text, icon, on_disk=False, **kwargs):
7087
anchor_point=(0.5, 0),
7188
anchored_position=(image.width // 2, image.height),
7289
)
90+
91+
if label_background is not None:
92+
_label.background_color = label_background
93+
7394
self.append(_label)
7495
self.touch_boundary = (
7596
0,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ adafruit-blinka-displayio
88
adafruit-circuitpython-display-shapes
99
adafruit-circuitpython-imageload
1010
adafruit-circuitpython-display-text
11+
adafruit-circuitpython-bitmap-font

0 commit comments

Comments
 (0)