Skip to content

Commit cefd454

Browse files
committed
adding background_color and usage example
1 parent 2086944 commit cefd454

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

adafruit_display_text/label.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
__version__ = "0.0.0-auto.0"
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
4646

47+
4748
class Label(displayio.Group):
4849
"""A label displaying a string of text. The origin point set by ``x`` and ``y``
4950
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):
5657
:param int max_glyphs: The largest quantity of glyphs we will display
5758
:param int color: Color of all text in RGB hex
5859
:param double line_spacing: Line spacing of text to display"""
60+
5961
def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff,
60-
line_spacing=1.25, **kwargs):
62+
backgroud_color=False, line_spacing=1.25, **kwargs):
6163
if not max_glyphs and not text:
6264
raise RuntimeError("Please provide a max size, or initial text")
6365
if not max_glyphs:
@@ -71,7 +73,10 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
7173
self.y = y
7274

7375
self.palette = displayio.Palette(2)
74-
self.palette.make_transparent(0)
76+
if not backgroud_color:
77+
self.palette.make_transparent(0)
78+
else:
79+
self.palette[0] = backgroud_color
7580
self.palette[1] = color
7681

7782
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
8287
if text is not None:
8388
self._update_text(str(text))
8489

85-
86-
def _update_text(self, new_text): # pylint: disable=too-many-locals
90+
def _update_text(self, new_text): # pylint: disable=too-many-locals
8791
x = 0
8892
y = 0
8993
i = 0
9094
old_c = 0
9195
y_offset = int((self.font.get_glyph(ord('M')).height -
9296
new_text.count('\n') * self.height * self.line_spacing) / 2)
93-
#print("y offset from baseline", y_offset)
97+
# print("y offset from baseline", y_offset)
9498
left = right = top = bottom = 0
9599
for character in new_text:
96100
if character == '\n':
@@ -100,10 +104,10 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
100104
glyph = self.font.get_glyph(ord(character))
101105
if not glyph:
102106
continue
103-
right = max(right, x+glyph.width)
104-
if y == 0: # first line, find the Ascender height
105-
top = min(top, -glyph.height+y_offset)
106-
bottom = max(bottom, y-glyph.dy+y_offset)
107+
right = max(right, x + glyph.width)
108+
if y == 0: # first line, find the Ascender height
109+
top = min(top, -glyph.height + y_offset)
110+
bottom = max(bottom, y - glyph.dy + y_offset)
107111
position_y = y - glyph.height - glyph.dy + y_offset
108112
position_x = x + glyph.dx
109113
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
141145
while len(self) > i:
142146
self.pop()
143147
self._text = new_text
144-
self._boundingbox = (left, top, left+right, bottom-top)
148+
self._boundingbox = (left, top, left + right, bottom - top)
145149

146150
@property
147151
def bounding_box(self):
@@ -192,10 +196,10 @@ def anchor_point(self, new_anchor_point):
192196
def anchored_position(self):
193197
"""Position relative to the anchor_point. Tuple containing x,y
194198
pixel coordinates."""
195-
return (self.x-self._boundingbox[2]*self._anchor_point[0],
196-
self.y-self._boundingbox[3]*self._anchor_point[1])
199+
return (self.x - self._boundingbox[2] * self._anchor_point[0],
200+
self.y - self._boundingbox[3] * self._anchor_point[1])
197201

198202
@anchored_position.setter
199203
def anchored_position(self, new_position):
200-
self.x = int(new_position[0]-(self._boundingbox[2]*self._anchor_point[0]))
201-
self.y = int(new_position[1]-(self._boundingbox[3]*self._anchor_point[1]))
204+
self.x = int(new_position[0] - (self._boundingbox[2] * self._anchor_point[0]))
205+
self.y = int(new_position[1] - (self._boundingbox[3] * self._anchor_point[1]))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
This examples shows the use color and background_color
3+
"""
4+
import board
5+
import terminalio
6+
from adafruit_display_text import label
7+
8+
9+
text = " Color Background Hello world"
10+
text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, backgroud_color=0xFFAA00)
11+
text_area.x = 10
12+
text_area.y = 10
13+
board.DISPLAY.show(text_area)
14+
while True:
15+
pass

0 commit comments

Comments
 (0)