arcade icon indicating copy to clipboard operation
arcade copied to clipboard

NinePatchTexture.create_rounded_corners(..)

Open einarf opened this issue 1 year ago • 3 comments

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.

einarf avatar Jul 22 '24 14:07 einarf

To my understanding, are two ways to go about this:

  1. Pillow + create a texture
  2. Render directly into a texture as in the PerfGraph https://github.com/pythonarcade/arcade/blob/8be369966ada1ecdc0153168abac068ecb534f02/arcade/perf_graph.py#L15

pushfoo avatar Mar 03 '25 06:03 pushfoo

@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()

einarf avatar Mar 27 '25 22:03 einarf

yes, that looks fine to me :)

eruvanos avatar Mar 28 '25 07:03 eruvanos