Skip to content

Adding background_color #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 27 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=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:
Expand All @@ -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
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.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()
Expand Down Expand Up @@ -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."""
Expand Down
25 changes: 25 additions & 0 deletions examples/display_text_background_color.py
Original file line number Diff line number Diff line change
@@ -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)
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


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