arcade
arcade copied to clipboard
NinePatchTexture.create_rounded_corners(..)
Make it possible to create rounded corner nine patch images just from a few parameters including a color. This is extremely useful for things like buttons and makes it fast to prototype something quick.
Optionally a method to create a texture with rounded corners can be in the texture module so we don't have to make this extra initializer.
To my understanding, are two ways to go about this:
- Pillow + create a texture
- Render directly into a texture as in the
PerfGraphhttps://github.com/pythonarcade/arcade/blob/8be369966ada1ecdc0153168abac068ecb534f02/arcade/perf_graph.py#L15
@eruvanos Something like this acceptable?
from PIL import Image, ImageDraw
def create_rounded_button(size: int, radius: int, background_color, border_color=None, border_size=0):
im = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw_button = ImageDraw.Draw(im)
if border_color and border_size:
draw_button.rounded_rectangle((0, 0, size-1, size-1), radius=radius, fill=border_color)
draw_button.rounded_rectangle(
(border_size, border_size, size-1-border_size, size-1-border_size),
radius=radius,
fill=background_color,
)
else:
draw_button.rounded_rectangle((0, 0, size-1, size-1), radius=radius, fill=background_color)
return im
button = create_rounded_button(
size=200,
radius=20,
background_color=(64, 64, 64, 255),
border_color=(255, 255, 255, 255),
border_size=2,
)
button.show()
yes, that looks fine to me :)