Skip to content

Commit b506a0a

Browse files
authored
Merge pull request adafruit#35 from FoamyGuy/master
Adding background_color
2 parents 3fa0b2c + 72c5b62 commit b506a0a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

adafruit_display_text/label.py

Lines changed: 27 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-
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:
@@ -71,7 +71,14 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
7171
self.y = y
7272

7373
self.palette = displayio.Palette(2)
74-
self.palette.make_transparent(0)
74+
if background_color is not None:
75+
self.palette[0] = background_color
76+
self.palette.make_opaque(0)
77+
self._transparent_background = False
78+
else:
79+
self.palette[0] = 0
80+
self.palette.make_transparent(0)
81+
self._transparent_background = True
7582
self.palette[1] = color
7683

7784
bounds = self.font.get_bounding_box()
@@ -168,6 +175,24 @@ def color(self):
168175
def color(self, new_color):
169176
self.palette[1] = new_color
170177

178+
@property
179+
def background_color(self):
180+
"""Color of the background as an RGB hex number."""
181+
if not self._transparent_background:
182+
return self.palette[0]
183+
return None
184+
185+
@background_color.setter
186+
def background_color(self, new_color):
187+
if new_color is not None:
188+
self.palette[0] = new_color
189+
self.palette.make_opaque(0)
190+
self._transparent_background = False
191+
else:
192+
self.palette[0] = 0
193+
self.palette.make_transparent(0)
194+
self._transparent_background = True
195+
171196
@property
172197
def text(self):
173198
"""Text to display."""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
This examples shows the use color and background_color
3+
"""
4+
import time
5+
import board
6+
import terminalio
7+
from adafruit_display_text import label
8+
9+
text = " Color Background Hello world"
10+
text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, background_color=0xFFAA00)
11+
text_area.x = 10
12+
text_area.y = 10
13+
14+
print("background color is {:06x}".format(text_area.background_color))
15+
16+
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))
24+
while True:
25+
pass

0 commit comments

Comments
 (0)