From cefd4545f5b16a0f044778da0f3f264650abbab1 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 14 Feb 2020 18:22:20 -0600 Subject: [PATCH 01/12] adding background_color and usage example --- adafruit_display_text/label.py | 32 +++++++++++++---------- examples/display_text_background_color.py | 15 +++++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 examples/display_text_background_color.py diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 93674e2..799781e 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -44,6 +44,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git" + class Label(displayio.Group): """A label displaying a string of text. The origin point set by ``x`` and ``y`` properties will be the left edge of the bounding box, and in the center of a M @@ -56,8 +57,9 @@ class Label(displayio.Group): :param int max_glyphs: The largest quantity of glyphs we will display :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): + backgroud_color=False, 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 +73,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 backgroud_color: + self.palette.make_transparent(0) + else: + self.palette[0] = backgroud_color self.palette[1] = color bounds = self.font.get_bounding_box() @@ -82,15 +87,14 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff if text is not None: self._update_text(str(text)) - - def _update_text(self, new_text): # pylint: disable=too-many-locals + def _update_text(self, new_text): # pylint: disable=too-many-locals x = 0 y = 0 i = 0 old_c = 0 y_offset = int((self.font.get_glyph(ord('M')).height - new_text.count('\n') * self.height * self.line_spacing) / 2) - #print("y offset from baseline", y_offset) + # print("y offset from baseline", y_offset) left = right = top = bottom = 0 for character in new_text: if character == '\n': @@ -100,10 +104,10 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals glyph = self.font.get_glyph(ord(character)) if not glyph: continue - right = max(right, x+glyph.width) - if y == 0: # first line, find the Ascender height - top = min(top, -glyph.height+y_offset) - bottom = max(bottom, y-glyph.dy+y_offset) + right = max(right, x + glyph.width) + if y == 0: # first line, find the Ascender height + top = min(top, -glyph.height + y_offset) + bottom = max(bottom, y - glyph.dy + y_offset) position_y = y - glyph.height - glyph.dy + y_offset position_x = x + glyph.dx if not self._text or old_c >= len(self._text) or character != self._text[old_c]: @@ -141,7 +145,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals while len(self) > i: self.pop() self._text = new_text - self._boundingbox = (left, top, left+right, bottom-top) + self._boundingbox = (left, top, left + right, bottom - top) @property def bounding_box(self): @@ -192,10 +196,10 @@ def anchor_point(self, new_anchor_point): def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" - return (self.x-self._boundingbox[2]*self._anchor_point[0], - self.y-self._boundingbox[3]*self._anchor_point[1]) + return (self.x - self._boundingbox[2] * self._anchor_point[0], + self.y - self._boundingbox[3] * self._anchor_point[1]) @anchored_position.setter def anchored_position(self, new_position): - self.x = int(new_position[0]-(self._boundingbox[2]*self._anchor_point[0])) - self.y = int(new_position[1]-(self._boundingbox[3]*self._anchor_point[1])) + self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0])) + self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1])) diff --git a/examples/display_text_background_color.py b/examples/display_text_background_color.py new file mode 100644 index 0000000..d8ca407 --- /dev/null +++ b/examples/display_text_background_color.py @@ -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, backgroud_color=0xFFAA00) +text_area.x = 10 +text_area.y = 10 +board.DISPLAY.show(text_area) +while True: + pass From 39431ec92d393d492c43d4719f72fa6342dbe8c4 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 14 Feb 2020 18:30:38 -0600 Subject: [PATCH 02/12] undo ide formatting changes --- adafruit_display_text/label.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 799781e..f0f2c39 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -44,7 +44,6 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git" - class Label(displayio.Group): """A label displaying a string of text. The origin point set by ``x`` and ``y`` properties will be the left edge of the bounding box, and in the center of a M @@ -57,7 +56,6 @@ class Label(displayio.Group): :param int max_glyphs: The largest quantity of glyphs we will display :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, backgroud_color=False, line_spacing=1.25, **kwargs): if not max_glyphs and not text: @@ -87,14 +85,15 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff if text is not None: self._update_text(str(text)) - def _update_text(self, new_text): # pylint: disable=too-many-locals + + def _update_text(self, new_text): # pylint: disable=too-many-locals x = 0 y = 0 i = 0 old_c = 0 y_offset = int((self.font.get_glyph(ord('M')).height - new_text.count('\n') * self.height * self.line_spacing) / 2) - # print("y offset from baseline", y_offset) + #print("y offset from baseline", y_offset) left = right = top = bottom = 0 for character in new_text: if character == '\n': @@ -104,10 +103,10 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals glyph = self.font.get_glyph(ord(character)) if not glyph: continue - right = max(right, x + glyph.width) - if y == 0: # first line, find the Ascender height - top = min(top, -glyph.height + y_offset) - bottom = max(bottom, y - glyph.dy + y_offset) + right = max(right, x+glyph.width) + if y == 0: # first line, find the Ascender height + top = min(top, -glyph.height+y_offset) + bottom = max(bottom, y-glyph.dy+y_offset) position_y = y - glyph.height - glyph.dy + y_offset position_x = x + glyph.dx if not self._text or old_c >= len(self._text) or character != self._text[old_c]: @@ -145,7 +144,7 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals while len(self) > i: self.pop() self._text = new_text - self._boundingbox = (left, top, left + right, bottom - top) + self._boundingbox = (left, top, left+right, bottom-top) @property def bounding_box(self): @@ -196,10 +195,10 @@ def anchor_point(self, new_anchor_point): def anchored_position(self): """Position relative to the anchor_point. Tuple containing x,y pixel coordinates.""" - return (self.x - self._boundingbox[2] * self._anchor_point[0], - self.y - self._boundingbox[3] * self._anchor_point[1]) + return (self.x-self._boundingbox[2]*self._anchor_point[0], + self.y-self._boundingbox[3]*self._anchor_point[1]) @anchored_position.setter def anchored_position(self, new_position): - self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0])) - self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1])) + self.x = int(new_position[0]-(self._boundingbox[2]*self._anchor_point[0])) + self.y = int(new_position[1]-(self._boundingbox[3]*self._anchor_point[1])) From eae4cb4f48119cf8498bc45ae404dc928577f2e7 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 14 Feb 2020 18:37:40 -0600 Subject: [PATCH 03/12] adding getter setter properties for background_color --- adafruit_display_text/label.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index f0f2c39..920e2c0 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -171,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] + + @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.""" From 7868b40ae4a6cca47269b68779b288011269b64f Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 14 Feb 2020 20:22:39 -0600 Subject: [PATCH 04/12] fix typo --- adafruit_display_text/label.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 920e2c0..b3a36e3 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, - backgroud_color=False, line_spacing=1.25, **kwargs): + background_color=False, 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,10 +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) - if not backgroud_color: + if not background_color: self.palette.make_transparent(0) else: - self.palette[0] = backgroud_color + self.palette[0] = background_color self.palette[1] = color bounds = self.font.get_bounding_box() From fb70ae4b70e8a57a905446953fd384c84b081466 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Fri, 14 Feb 2020 20:25:01 -0600 Subject: [PATCH 05/12] fix typo in background_color example --- examples/display_text_background_color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/display_text_background_color.py b/examples/display_text_background_color.py index d8ca407..49942f3 100644 --- a/examples/display_text_background_color.py +++ b/examples/display_text_background_color.py @@ -7,7 +7,7 @@ text = " Color Background Hello world" -text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, backgroud_color=0xFFAA00) +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) From 5dfc29176375b6d0fa39df57495fd1a5d0485bd0 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 19:32:58 -0600 Subject: [PATCH 06/12] fix setting background back to transparent. add logic to track transparency and return None when background is transparent. add more functionality to example. --- adafruit_display_text/label.py | 12 ++++++++++-- examples/display_text_background_color.py | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index b3a36e3..77962c9 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, - background_color=False, 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: @@ -72,8 +72,10 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff self.palette = displayio.Palette(2) if not background_color: + self.transparent_background = True self.palette.make_transparent(0) else: + self.transparent_background = False self.palette[0] = background_color self.palette[1] = color @@ -174,15 +176,21 @@ def color(self, new_color): @property def background_color(self): """Color of the background as an RGB hex number.""" - return self.palette[0] + if not self.transparent_background: + return self.palette[0] + else: + return None @background_color.setter def background_color(self, new_color): if new_color: 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): diff --git a/examples/display_text_background_color.py b/examples/display_text_background_color.py index 49942f3..3f48b70 100644 --- a/examples/display_text_background_color.py +++ b/examples/display_text_background_color.py @@ -1,15 +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 From 80ebaa9b0df5b6d4af7aa78f21db6a55faa44195 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 19:35:50 -0600 Subject: [PATCH 07/12] remove unneeded else block --- adafruit_display_text/label.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 77962c9..01daddd 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -178,8 +178,7 @@ def background_color(self): """Color of the background as an RGB hex number.""" if not self.transparent_background: return self.palette[0] - else: - return None + return None @background_color.setter def background_color(self, new_color): From 46b04fea727205c1e4e9c98a1bf9f9ce8602fbde Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 19:43:21 -0600 Subject: [PATCH 08/12] fix for setting background to visible black --- adafruit_display_text/label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 01daddd..57b45ba 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -182,7 +182,7 @@ def background_color(self): @background_color.setter def background_color(self, new_color): - if new_color: + if new_color or new_color == 0x000000: self.palette[0] = new_color self.palette.make_opaque(0) self.transparent_background = False From 870643ae4ecad0514932db242a8954580cac90cc Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 21:26:40 -0600 Subject: [PATCH 09/12] add leading underscore to _transparent_background --- adafruit_display_text/label.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 57b45ba..9fd92e2 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -72,10 +72,10 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff self.palette = displayio.Palette(2) if not background_color: - self.transparent_background = True + self._transparent_background = True self.palette.make_transparent(0) else: - self.transparent_background = False + self._transparent_background = False self.palette[0] = background_color self.palette[1] = color @@ -176,7 +176,7 @@ def color(self, new_color): @property def background_color(self): """Color of the background as an RGB hex number.""" - if not self.transparent_background: + if not self._transparent_background: return self.palette[0] return None @@ -185,11 +185,11 @@ def background_color(self, new_color): if new_color or new_color == 0x000000: self.palette[0] = new_color self.palette.make_opaque(0) - self.transparent_background = False + self._transparent_background = False else: self.palette[0] = 0 self.palette.make_transparent(0) - self.transparent_background = True + self._transparent_background = True @property def text(self): From 0fe25b5307ffdbc240d93330491fba34ee1b421f Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 21:44:52 -0600 Subject: [PATCH 10/12] fix constructor for visible black background color --- adafruit_display_text/label.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 9fd92e2..2d44cf0 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -71,12 +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) - if not background_color: - self._transparent_background = True - self.palette.make_transparent(0) - else: + if background_color or background_color == 0x000000: + self.palette[0] = new_color + self.palette.make_opaque(0) self._transparent_background = False - self.palette[0] = background_color + else: + self.palette[0] = 0 + self.palette.make_transparent(0) + self._transparent_background = True self.palette[1] = color bounds = self.font.get_bounding_box() From 4d65e552736eadcd58a3be7a7f6430aa0e307c09 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 17 Feb 2020 21:45:11 -0600 Subject: [PATCH 11/12] fix constructor for visible black background color --- adafruit_display_text/label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 2d44cf0..24a4298 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -72,7 +72,7 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff self.palette = displayio.Palette(2) if background_color or background_color == 0x000000: - self.palette[0] = new_color + self.palette[0] = background_color self.palette.make_opaque(0) self._transparent_background = False else: From 72c5b627b976ed0f3409a4271585f8f0daebe2d6 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 18 Feb 2020 08:50:29 -0600 Subject: [PATCH 12/12] fix constructor for visible black background color --- adafruit_display_text/label.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_display_text/label.py b/adafruit_display_text/label.py index 24a4298..b188459 100644 --- a/adafruit_display_text/label.py +++ b/adafruit_display_text/label.py @@ -71,7 +71,7 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff self.y = y self.palette = displayio.Palette(2) - if background_color or background_color == 0x000000: + if background_color is not None: self.palette[0] = background_color self.palette.make_opaque(0) self._transparent_background = False @@ -184,7 +184,7 @@ def background_color(self): @background_color.setter def background_color(self, new_color): - if new_color or new_color == 0x000000: + if new_color is not None: self.palette[0] = new_color self.palette.make_opaque(0) self._transparent_background = False