Skip to content

Instantly share code, notes, and snippets.

@TG-Techie
Created February 6, 2021 19:00
Show Gist options
  • Save TG-Techie/44548a0a20c23841ca89bf19d0c4119f to your computer and use it in GitHub Desktop.
Save TG-Techie/44548a0a20c23841ca89bf19d0c4119f to your computer and use it in GitHub Desktop.
CircuitPython displayio simple round rect using vectorio
# The MIT License (MIT)
#
# Copyright (c) 2021 Jonah Yolles-Murphy (TG-Techie)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import displayio
class SimpleRoundRect(displayio.Group):
def __init__(self, x, y, width, height, radius=0, fill=0xFF0000):
radius = min(radius, width // 2, height // 2)
p = displayio.Palette(2)
s = displayio.Shape(width, height)
super().__init__(x=x, y=y, max_size=6)
p.make_transparent(0)
p[1] = fill
self._palette = p
self._radius = radius
self._x = x
self._y = y
self._width = width
self._height = height
right_rad_x = width - radius
bottom_rad_y = height - radius
# is_slot = bool(r*2 >= min(width, height))
self._top_left = top_left = vectorio.VectorShape(
shape=vectorio.Circle(radius),
pixel_shader=p,
x=radius,
y=radius,
)
self._top_right = top_right = vectorio.VectorShape(
shape=vectorio.Circle(radius),
pixel_shader=p,
x=right_rad_x,
y=radius,
)
self._bottom_left = bottom_left = vectorio.VectorShape(
shape=vectorio.Circle(radius),
pixel_shader=p,
x=radius,
y=bottom_rad_y,
)
self._bottom_right = bottom_right = vectorio.VectorShape(
shape=vectorio.Circle(radius),
pixel_shader=p,
x=right_rad_x,
y=bottom_rad_y,
)
w = width - 2 * radius
if w > 0:
self._veritcal_fill = vfill = vectorio.VectorShape(
shape=vectorio.Rectangle(w, height),
pixel_shader=p,
x=radius,
y=0,
)
self.append(vfill)
h = height - 2 * radius
if h > 0:
self._horizontal_fill = hfill = vectorio.VectorShape(
shape=vectorio.Rectangle(width, h),
pixel_shader=p,
x=0,
y=radius,
)
self.append(hfill)
self.append(top_left)
self.append(top_right)
self.append(bottom_left)
self.append(bottom_right)
@property
def fill(self) -> int:
return self._palette[1]
@fill.setter
def fill(self, value: int):
self._palette[1] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment