Skip to content

Commit 5dfc291

Browse files
committed
fix setting background back to transparent. add logic to track transparency and return None when background is transparent. add more functionality to example.
1 parent fb70ae4 commit 5dfc291

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

adafruit_display_text/label.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Label(displayio.Group):
5757
:param int color: Color of all text in RGB hex
5858
:param double line_spacing: Line spacing of text to display"""
5959
def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff,
60-
background_color=False, line_spacing=1.25, **kwargs):
60+
background_color=None, line_spacing=1.25, **kwargs):
6161
if not max_glyphs and not text:
6262
raise RuntimeError("Please provide a max size, or initial text")
6363
if not max_glyphs:
@@ -72,8 +72,10 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
7272

7373
self.palette = displayio.Palette(2)
7474
if not background_color:
75+
self.transparent_background = True
7576
self.palette.make_transparent(0)
7677
else:
78+
self.transparent_background = False
7779
self.palette[0] = background_color
7880
self.palette[1] = color
7981

@@ -174,15 +176,21 @@ def color(self, new_color):
174176
@property
175177
def background_color(self):
176178
"""Color of the background as an RGB hex number."""
177-
return self.palette[0]
179+
if not self.transparent_background:
180+
return self.palette[0]
181+
else:
182+
return None
178183

179184
@background_color.setter
180185
def background_color(self, new_color):
181186
if new_color:
182187
self.palette[0] = new_color
183188
self.palette.make_opaque(0)
189+
self.transparent_background = False
184190
else:
191+
self.palette[0] = 0
185192
self.palette.make_transparent(0)
193+
self.transparent_background = True
186194

187195
@property
188196
def text(self):
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
"""
22
This examples shows the use color and background_color
33
"""
4+
import time
45
import board
56
import terminalio
67
from adafruit_display_text import label
78

8-
99
text = " Color Background Hello world"
1010
text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, background_color=0xFFAA00)
1111
text_area.x = 10
1212
text_area.y = 10
13+
14+
print("background color is {:06x}".format(text_area.background_color))
15+
1316
board.DISPLAY.show(text_area)
17+
18+
time.sleep(2)
19+
text_area.background_color = 0xFF0000
20+
print("background color is {:06x}".format(text_area.background_color))
21+
time.sleep(2)
22+
text_area.background_color = None
23+
print("background color is {}".format(text_area.background_color))
1424
while True:
1525
pass

0 commit comments

Comments
 (0)