Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rounded rects to surfaces #2695

Open
bilhox opened this issue Jan 27, 2024 · 3 comments
Open

Add support for rounded rects to surfaces #2695

bilhox opened this issue Jan 27, 2024 · 3 comments
Labels
enhancement New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform

Comments

@bilhox
Copy link
Contributor

bilhox commented Jan 27, 2024

Hello,

It would be great to add a way to round surface corners. I believe it wouldn't be difficult to implement it, since we do it manually in pygame.draw.rect. What do you think about adding a new function in transform sub module like pygame.transform.round_corners ?

I'm pretty surprised no one is talking about it, maybe there is already a pretty efficient way to do that ? (Maybe there is even an issue already opened ?) I mean with blendmodes it's possible, but it isn't intuitive at all for beginners for example, and I believe we can optimise the process.

This feature would be helpful for everyone creating gui with buttons with rounded corners + texture.

@Starbuck5 Starbuck5 added New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform labels Jan 30, 2024
@Starbuck5
Copy link
Member

For the purposes of GUI, with scalability in mind, I one technique people often use is 9-patching. https://en.wikipedia.org/wiki/9-slice_scaling.

@Starbuck5
Copy link
Member

Starbuck5 commented Jan 30, 2024

Also I slightly misread your report, so I ended up whipping up an example of how to do this, which I might as well share.

import pygame

surface = pygame.image.load("cool.jpg")

# Poor man's convert alpha without a display, I want this and the
# mock_surface to have the same pixelformat.
standardized = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
standardized.blit(surface, (0,0))
surface = standardized

mock_surface = pygame.Surface(surface.get_size(), pygame.SRCALPHA)
mock_surface.fill((0,0,0,0))
pygame.draw.rect(mock_surface, "white", mock_surface.get_rect(), border_radius=100)

surface.blit(mock_surface, (0,0), special_flags=pygame.BLEND_RGBA_MULT)

pygame.image.save(surface, "out.png")

cool2
out

@bilhox
Copy link
Contributor Author

bilhox commented Jan 30, 2024

I thought about the same solution, I know it works, but I believe we can make it more intuitive for users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform
Projects
None yet
Development

No branches or pull requests

2 participants