Skip to content

Commit 64c79a9

Browse files
authored
Merge pull request #26 from FoamyGuy/color_properties
adding color properties
2 parents 47b1c90 + db17d14 commit 64c79a9

File tree

2 files changed

+162
-19
lines changed

2 files changed

+162
-19
lines changed

adafruit_button.py

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ def __init__(
9494
self._label = label
9595
self.body = self.fill = self.shadow = None
9696

97-
self.fill_color = _check_color(fill_color)
98-
self.outline_color = _check_color(outline_color)
97+
self._fill_color = _check_color(fill_color)
98+
self._outline_color = _check_color(outline_color)
9999
self._label_color = label_color
100100
self._label_font = label_font
101101
# Selecting inverts the button colors!
102-
self.selected_fill = _check_color(selected_fill)
103-
self.selected_outline = _check_color(selected_outline)
104-
self.selected_label = _check_color(selected_label)
102+
self._selected_fill = _check_color(selected_fill)
103+
self._selected_outline = _check_color(selected_outline)
104+
self._selected_label = _check_color(selected_label)
105105

106106
if self.selected_fill is None and fill_color is not None:
107-
self.selected_fill = (~self.fill_color) & 0xFFFFFF
107+
self.selected_fill = (~self._fill_color) & 0xFFFFFF
108108
if self.selected_outline is None and outline_color is not None:
109-
self.selected_outline = (~self.outline_color) & 0xFFFFFF
109+
self.selected_outline = (~self._outline_color) & 0xFFFFFF
110110

111111
if (outline_color is not None) or (fill_color is not None):
112112
if style == Button.RECT:
@@ -115,8 +115,8 @@ def __init__(
115115
0,
116116
width,
117117
height,
118-
fill=self.fill_color,
119-
outline=self.outline_color,
118+
fill=self._fill_color,
119+
outline=self._outline_color,
120120
)
121121
elif style == Button.ROUNDRECT:
122122
self.body = RoundRect(
@@ -125,8 +125,8 @@ def __init__(
125125
width,
126126
height,
127127
r=10,
128-
fill=self.fill_color,
129-
outline=self.outline_color,
128+
fill=self._fill_color,
129+
outline=self._outline_color,
130130
)
131131
elif style == Button.SHADOWRECT:
132132
self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color)
@@ -135,21 +135,21 @@ def __init__(
135135
0,
136136
width - 2,
137137
height - 2,
138-
fill=self.fill_color,
139-
outline=self.outline_color,
138+
fill=self._fill_color,
139+
outline=self._outline_color,
140140
)
141141
elif style == Button.SHADOWROUNDRECT:
142142
self.shadow = RoundRect(
143-
2, 2, width - 2, height - 2, r=10, fill=self.outline_color
143+
2, 2, width - 2, height - 2, r=10, fill=self._outline_color
144144
)
145145
self.body = RoundRect(
146146
0,
147147
0,
148148
width - 2,
149149
height - 2,
150150
r=10,
151-
fill=self.fill_color,
152-
outline=self.outline_color,
151+
fill=self._fill_color,
152+
outline=self._outline_color,
153153
)
154154
if self.shadow:
155155
self.append(self.shadow)
@@ -200,10 +200,10 @@ def selected(self, value):
200200
new_out = self.selected_outline
201201
new_label = self.selected_label
202202
else:
203-
new_fill = self.fill_color
204-
new_out = self.outline_color
203+
new_fill = self._fill_color
204+
new_out = self._outline_color
205205
new_label = self._label_color
206-
# update all relevant colros!
206+
# update all relevant colors!
207207
if self.body is not None:
208208
self.body.fill = new_fill
209209
self.body.outline = new_out
@@ -228,3 +228,66 @@ def contains(self, point):
228228
return (self.x <= point[0] <= self.x + self.width) and (
229229
self.y <= point[1] <= self.y + self.height
230230
)
231+
232+
@property
233+
def fill_color(self):
234+
"""The fill color of the button body"""
235+
return self._fill_color
236+
237+
@fill_color.setter
238+
def fill_color(self, new_color):
239+
self._fill_color = _check_color(new_color)
240+
if not self.selected:
241+
self.body.fill = self._fill_color
242+
243+
@property
244+
def outline_color(self):
245+
"""The outline color of the button body"""
246+
return self._outline_color
247+
248+
@outline_color.setter
249+
def outline_color(self, new_color):
250+
self._outline_color = _check_color(new_color)
251+
if not self.selected:
252+
self.body.outline = self._outline_color
253+
254+
@property
255+
def selected_fill(self):
256+
"""The fill color of the button body when selected"""
257+
return self._selected_fill
258+
259+
@selected_fill.setter
260+
def selected_fill(self, new_color):
261+
self._selected_fill = _check_color(new_color)
262+
if self.selected:
263+
self.body.fill = self._selected_fill
264+
265+
@property
266+
def selected_outline(self):
267+
"""The outline color of the button body when selected"""
268+
return self._selected_outline
269+
270+
@selected_outline.setter
271+
def selected_outline(self, new_color):
272+
self._selected_outline = _check_color(new_color)
273+
if self.selected:
274+
self.body.outline = self._selected_outline
275+
276+
@property
277+
def selected_label(self):
278+
"""The font color of the button when selected"""
279+
return self._selected_label
280+
281+
@selected_label.setter
282+
def selected_label(self, new_color):
283+
self._selected_label = _check_color(new_color)
284+
285+
@property
286+
def label_color(self):
287+
"""The font color of the button"""
288+
return self._label_color
289+
290+
@label_color.setter
291+
def label_color(self, new_color):
292+
self._label_color = _check_color(new_color)
293+
self._label.color = self._label_color
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
Basic example that illustrates how to set the various color options on the button using
6+
properties after the button has been initialized.
7+
"""
8+
9+
import board
10+
import displayio
11+
import terminalio
12+
import adafruit_touchscreen
13+
from adafruit_button import Button
14+
15+
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
16+
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
17+
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
18+
display = board.DISPLAY
19+
20+
# --| Button Config |-------------------------------------------------
21+
BUTTON_X = 110
22+
BUTTON_Y = 95
23+
BUTTON_WIDTH = 100
24+
BUTTON_HEIGHT = 50
25+
BUTTON_STYLE = Button.ROUNDRECT
26+
BUTTON_FILL_COLOR = 0xAA0000
27+
BUTTON_OUTLINE_COLOR = 0x0000FF
28+
BUTTON_LABEL = "HELLO WORLD"
29+
BUTTON_LABEL_COLOR = 0x000000
30+
# --| Button Config |-------------------------------------------------
31+
32+
# Setup touchscreen (PyPortal)
33+
ts = adafruit_touchscreen.Touchscreen(
34+
board.TOUCH_XL,
35+
board.TOUCH_XR,
36+
board.TOUCH_YD,
37+
board.TOUCH_YU,
38+
calibration=((5200, 59000), (5800, 57000)),
39+
size=(320, 240),
40+
)
41+
42+
# Make the display context
43+
splash = displayio.Group()
44+
display.show(splash)
45+
46+
# Make the button
47+
button = Button(
48+
x=BUTTON_X,
49+
y=BUTTON_Y,
50+
width=BUTTON_WIDTH,
51+
height=BUTTON_HEIGHT,
52+
style=BUTTON_STYLE,
53+
fill_color=BUTTON_FILL_COLOR,
54+
outline_color=BUTTON_OUTLINE_COLOR,
55+
label="HELLO WORLD",
56+
label_font=terminalio.FONT,
57+
label_color=BUTTON_LABEL_COLOR,
58+
)
59+
60+
button.fill_color = 0x00FF00
61+
button.outline_color = 0xFF0000
62+
63+
button.selected_fill = (0, 0, 255)
64+
button.selected_outline = (255, 0, 0)
65+
66+
button.label_color = 0xFF0000
67+
button.selected_label = 0x00FF00
68+
69+
# Add button to the display context
70+
splash.append(button)
71+
72+
# Loop and look for touches
73+
while True:
74+
p = ts.touch_point
75+
if p:
76+
if button.contains(p):
77+
print(p)
78+
button.selected = True
79+
else:
80+
button.selected = False

0 commit comments

Comments
 (0)