Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=False, line_spacing=1.25, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the default to None instead and then change your logic to match? You can check for None easily:

if background_color:
    # action if there is a color set
else:
    # action if it's None

if not max_glyphs and not text:
raise RuntimeError("Please provide a max size, or initial text")
if not max_glyphs:
Expand All @@ -71,7 +71,10 @@ 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 not background_color:
self.palette.make_transparent(0)
else:
self.palette[0] = background_color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I totally forgot about needing to accept 0 as a possible valid color. If that were done with above, then it would be treated as setting transparent. How about this?

        if background_color is not None:
            self.palette[0] = background_color
            self.palette.make_opaque(0)
            self.transparent_background = False
        else:
            self.palette.make_transparent(0)
            self.transparent_background = True

Or could maybe do like you did in the setter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but also with the leading underscore)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, thank you. just pushed some commits to fix this in the constructor.

self.palette[1] = color

bounds = self.font.get_bounding_box()
Expand Down Expand Up @@ -168,6 +171,19 @@ 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."""
return self.palette[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if this would return None when there is no background color set.


@background_color.setter
def background_color(self, new_color):
if new_color:
self.palette[0] = new_color
self.palette.make_opaque(0)
else:
self.palette.make_transparent(0)

@property
def text(self):
"""Text to display."""
Expand Down
15 changes: 15 additions & 0 deletions examples/display_text_background_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
This examples shows the use color and background_color
"""
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
board.DISPLAY.show(text_area)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add some lines that show:

  • querying current background color
  • changing back to no background color

while True:
pass