From be27763f8be0f647cbe17ecee8c782901ce2cede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 18 Jun 2016 22:13:19 +0200 Subject: Move classes to separate file --- helpers/rounded_rect.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 helpers/rounded_rect.py (limited to 'helpers/rounded_rect.py') diff --git a/helpers/rounded_rect.py b/helpers/rounded_rect.py new file mode 100644 index 0000000..f168e0e --- /dev/null +++ b/helpers/rounded_rect.py @@ -0,0 +1,62 @@ +import pygame + +class RoundedRect: + def __init__(self, rect, outer_color, inner_color, linewidth = 2, radius = 0.4): + self.rect = pygame.Rect(rect) + self.outer_color = pygame.Color(*outer_color) + self.inner_color = pygame.Color(*inner_color) + self.linewidth = linewidth + self.radius = radius + + def surface(self): + rectangle = self.filledRoundedRect(self.rect, self.outer_color, self.radius) + + inner_rect = pygame.Rect(( + self.rect.left + 2 * self.linewidth, + self.rect.top + 2 * self.linewidth, + self.rect.right - 2 * self.linewidth, + self.rect.bottom - 2 * self.linewidth + )) + + inner_rectangle = self.filledRoundedRect(inner_rect, self.inner_color, self.radius) + + rectangle.blit(inner_rectangle, (self.linewidth, self.linewidth)) + + return rectangle + + def filledRoundedRect(self, rect, color, radius=0.4): + """ + filledRoundedRect(rect,color,radius=0.4) + + rect : rectangle + color : rgb or rgba + radius : 0 <= radius <= 1 + """ + + alpha = color.a + color.a = 0 + pos = rect.topleft + rect.topleft = 0,0 + rectangle = pygame.Surface(rect.size,pygame.SRCALPHA) + + circle = pygame.Surface([min(rect.size)*3]*2,pygame.SRCALPHA) + pygame.draw.ellipse(circle,(0,0,0),circle.get_rect(),0) + circle = pygame.transform.smoothscale(circle,[int(min(rect.size)*radius)]*2) + + radius = rectangle.blit(circle,(0,0)) + radius.bottomright = rect.bottomright + rectangle.blit(circle,radius) + radius.topright = rect.topright + rectangle.blit(circle,radius) + radius.bottomleft = rect.bottomleft + rectangle.blit(circle,radius) + + rectangle.fill((0,0,0),rect.inflate(-radius.w,0)) + rectangle.fill((0,0,0),rect.inflate(0,-radius.h)) + + rectangle.fill(color,special_flags=pygame.BLEND_RGBA_MAX) + rectangle.fill((255,255,255,alpha),special_flags=pygame.BLEND_RGBA_MIN) + + return rectangle + + -- cgit v1.2.3