forked from ladyada/Adafruit_CircuitPython_Display_Shapes
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathroundrect.py
209 lines (186 loc) · 6.84 KB
/
roundrect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`roundrect`
================================================================================
A slightly modified version of Adafruit_CircuitPython_Display_Shapes that includes
an explicit call to palette.make_opaque() in the fill color setter function.
"""
try:
from typing import Optional
except ImportError:
pass
import displayio
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git"
class RoundRect(displayio.TileGrid):
# pylint: disable=too-many-arguments
"""A round-corner rectangle.
:param int x: The x-position of the top left corner.
:param int y: The y-position of the top left corner.
:param int width: The width of the rounded-corner rectangle.
:param int height: The height of the rounded-corner rectangle.
:param int r: The radius of the rounded corner.
:param int|None fill: The color to fill the rounded-corner rectangle. Can be a hex value
for a color or ``None`` for transparent.
:param int|None outline: The outline of the rounded-corner rectangle. Can be a hex value
for a color or ``None`` for no outline.
:param int stroke: Used for the outline. Will not change the outer bound size set by ``width``
and ``height``.
"""
def __init__(
self,
x: int,
y: int,
width: int,
height: int,
r: int,
*,
fill: Optional[int] = None,
outline: Optional[int] = None,
stroke: int = 1,
) -> None:
if width <= 0 or height <= 0:
raise ValueError("Rectangle dimensions must be larger than 0.")
if r > width / 2 or r > height / 2:
raise ValueError(
"Radius cannot exceed half of the smaller side (width or height)."
)
self._palette = displayio.Palette(3)
self._palette.make_transparent(0)
self._bitmap = displayio.Bitmap(width, height, 3)
for i in range(0, width): # draw the center chunk
for j in range(r, height - r): # draw the center chunk
self._bitmap[i, j] = 2
self._helper(
r,
r,
r,
color=2,
fill=True,
x_offset=width - 2 * r - 1,
y_offset=height - 2 * r - 1,
)
if fill is not None:
self._palette[2] = fill
self._palette.make_opaque(2)
else:
self._palette.make_transparent(2)
self._palette[2] = 0
if outline is not None:
self._palette[1] = outline
# draw flat sides
for w in range(r, width - r):
for line in range(stroke):
self._bitmap[w, line] = 1
self._bitmap[w, height - line - 1] = 1
for _h in range(r, height - r):
for line in range(stroke):
self._bitmap[line, _h] = 1
self._bitmap[width - line - 1, _h] = 1
# draw round corners
self._helper(
r,
r,
r,
color=1,
stroke=stroke,
x_offset=width - 2 * r - 1,
y_offset=height - 2 * r - 1,
)
super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y)
# pylint: disable=invalid-name, too-many-locals, too-many-branches
def _helper(
self,
x0: int,
y0: int,
r: int,
*,
color: int,
x_offset: int = 0,
y_offset: int = 0,
stroke: int = 1,
corner_flags: int = 0xF,
fill: bool = False,
) -> None:
f = 1 - r
ddF_x = 1
ddF_y = -2 * r
x = 0
y = r
while x < y:
if f >= 0:
y -= 1
ddF_y += 2
f += ddF_y
x += 1
ddF_x += 2
f += ddF_x
if corner_flags & 0x8:
if fill:
for w in range(x0 - y, x0 + y + x_offset):
self._bitmap[w, y0 + x + y_offset] = color
for w in range(x0 - x, x0 + x + x_offset):
self._bitmap[w, y0 + y + y_offset] = color
else:
for line in range(stroke):
self._bitmap[x0 - y + line, y0 + x + y_offset] = color
self._bitmap[x0 - x, y0 + y + y_offset - line] = color
if corner_flags & 0x1:
if fill:
for w in range(x0 - y, x0 + y + x_offset):
self._bitmap[w, y0 - x] = color
for w in range(x0 - x, x0 + x + x_offset):
self._bitmap[w, y0 - y] = color
else:
for line in range(stroke):
self._bitmap[x0 - y + line, y0 - x] = color
self._bitmap[x0 - x, y0 - y + line] = color
if corner_flags & 0x4:
for line in range(stroke):
self._bitmap[x0 + x + x_offset, y0 + y + y_offset - line] = color
self._bitmap[x0 + y + x_offset - line, y0 + x + y_offset] = color
if corner_flags & 0x2:
for line in range(stroke):
self._bitmap[x0 + x + x_offset, y0 - y + line] = color
self._bitmap[x0 + y + x_offset - line, y0 - x] = color
# pylint: enable=invalid-name, too-many-locals, too-many-branches
@property
def fill(self) -> Optional[int]:
"""The fill of the rounded-corner rectangle. Can be a hex value for a color or ``None`` for
transparent."""
return self._palette[2]
@fill.setter
def fill(self, color: Optional[int]) -> None:
if color is None:
self._palette[2] = 0
self._palette.make_transparent(2)
else:
self._palette[2] = color
self._palette.make_opaque(2)
@property
def outline(self) -> Optional[int]:
"""The outline of the rounded-corner rectangle. Can be a hex value for a color or ``None``
for no outline."""
return self._palette[1]
@outline.setter
def outline(self, color: Optional[int]) -> None:
if color is None:
self._palette[1] = 0
self._palette.make_transparent(1)
else:
self._palette[1] = color
self._palette.make_opaque(2)
@property
def width(self) -> int:
"""
:return: the width of the rounded rectangle in pixels
"""
return self._bitmap.width
@property
def height(self) -> int:
"""
:return: the height of the rounded rectangle in pixels
"""
return self._bitmap.height