]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/rounded_rect.py
Use kivy instead of pygame
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / rounded_rect.py
1 import pygame
2
3 class RoundedRect:
4 def __init__(self, rect, outer_color, inner_color, linewidth = 2, radius = 0.4):
5 self.rect = pygame.Rect(rect)
6 self.outer_color = pygame.Color(*outer_color)
7 self.inner_color = pygame.Color(*inner_color)
8 self.linewidth = linewidth
9 self.radius = radius
10
11 def surface(self):
12 rectangle = self.filledRoundedRect(self.rect, self.outer_color, self.radius)
13
14 inner_rect = pygame.Rect((
15 self.rect.left + 2 * self.linewidth,
16 self.rect.top + 2 * self.linewidth,
17 self.rect.right - 2 * self.linewidth,
18 self.rect.bottom - 2 * self.linewidth
19 ))
20
21 inner_rectangle = self.filledRoundedRect(inner_rect, self.inner_color, self.radius)
22
23 rectangle.blit(inner_rectangle, (self.linewidth, self.linewidth))
24
25 return rectangle
26
27 def filledRoundedRect(self, rect, color, radius=0.4):
28 """
29 filledRoundedRect(rect,color,radius=0.4)
30
31 rect : rectangle
32 color : rgb or rgba
33 radius : 0 <= radius <= 1
34 """
35
36 alpha = color.a
37 color.a = 0
38 pos = rect.topleft
39 rect.topleft = 0,0
40 rectangle = pygame.Surface(rect.size,pygame.SRCALPHA)
41
42 circle = pygame.Surface([min(rect.size)*3]*2,pygame.SRCALPHA)
43 pygame.draw.ellipse(circle,(0,0,0),circle.get_rect(),0)
44 circle = pygame.transform.smoothscale(circle,[int(min(rect.size)*radius)]*2)
45
46 radius = rectangle.blit(circle,(0,0))
47 radius.bottomright = rect.bottomright
48 rectangle.blit(circle,radius)
49 radius.topright = rect.topright
50 rectangle.blit(circle,radius)
51 radius.bottomleft = rect.bottomleft
52 rectangle.blit(circle,radius)
53
54 rectangle.fill((0,0,0),rect.inflate(-radius.w,0))
55 rectangle.fill((0,0,0),rect.inflate(0,-radius.h))
56
57 rectangle.fill(color,special_flags=pygame.BLEND_RGBA_MAX)
58 rectangle.fill((255,255,255,alpha),special_flags=pygame.BLEND_RGBA_MIN)
59
60 return rectangle
61
62