-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from 12 commits
2086944
cefd454
39431ec
eae4cb4
7868b40
fb70ae4
5dfc291
80ebaa9
46b04fe
870643a
0fe25b5
4d65e55
72c5b62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 or background_color == 0x000000: | ||
self.palette[0] = background_color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I totally forgot about needing to accept 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (but also with the leading underscore) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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 or new_color == 0x000000: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with this: |
||
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.""" | ||
|
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add some lines that show:
|
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be better expressed as:
if background_color is not None: