diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 93674e2..b188459 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -57,7 +57,7 @@ class Label(displayio.Group): :param int color: Color of all text in RGB hex :param double line_spacing: Line spacing of text to display""" def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff, - line_spacing=1.25, **kwargs): + background_color=None, line_spacing=1.25, **kwargs): if not max_glyphs and not text: raise RuntimeError("Please provide a max size, or initial text") if not max_glyphs: @@ -71,7 +71,14 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff self.y = y self.palette = displayio.Palette(2) - self.palette.make_transparent(0) + if background_color is not None: + self.palette[0] = background_color + self.palette.make_opaque(0) + self._transparent_background = False + else: + self.palette[0] = 0 + self.palette.make_transparent(0) + self._transparent_background = True self.palette[1] = color bounds = self.font.get_bounding_box() @@ -168,6 +175,24 @@ def color(self): def color(self, new_color): self.palette[1] = new_color + @property + def background_color(self): + """Color of the background as an RGB hex number.""" + if not self._transparent_background: + return self.palette[0] + return None + + @background_color.setter + def background_color(self, new_color): + if new_color is not None: + self.palette[0] = new_color + self.palette.make_opaque(0) + self._transparent_background = False + else: + self.palette[0] = 0 + self.palette.make_transparent(0) + self._transparent_background = True + @property def text(self): """Text to display.""" diff --git a/examples/display_text_background_color.py b/examples/display_text_background_color.py new file mode 100644 index 0000000..3f48b70 --- /dev/null +++ b/examples/display_text_background_color.py @@ -0,0 +1,25 @@ +""" +This examples shows the use color and background_color +""" +import time +import board +import terminalio +from adafruit_display_text import label + +text = " Color Background Hello world" +text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, background_color=0xFFAA00) +text_area.x = 10 +text_area.y = 10 + +print("background color is {:06x}".format(text_area.background_color)) + +board.DISPLAY.show(text_area) + +time.sleep(2) +text_area.background_color = 0xFF0000 +print("background color is {:06x}".format(text_area.background_color)) +time.sleep(2) +text_area.background_color = None +print("background color is {}".format(text_area.background_color)) +while True: + pass