def test_blend_rgba(self):
bitsizes = [16, 32]
blends = ['BLEND_RGBA_ADD',
'BLEND_RGBA_SUB',
'BLEND_RGBA_MULT',
'BLEND_RGBA_MIN',
'BLEND_RGBA_MAX']
for bitsize in bitsizes:
surf = self._make_surface(bitsize, srcalpha=True)
comp = self._make_surface(bitsize, srcalpha=True)
for blend in blends:
self._fill_surface(surf)
self._fill_surface(comp)
comp.blit(surf, (3, 0),
special_flags=getattr(pygame, blend))
surf.blit(surf, (3, 0),
special_flags=getattr(pygame, blend))
self._assert_same(surf, comp)
python类BLEND_RGBA_MIN的实例源码
def setAlpha(srf, a):
"Adjust surface with a minimum transparency "
srf.fill((255,255,255,a), special_flags=pygame.BLEND_RGBA_MIN)
return srf
def todo_test_blit(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.blit:
# Surface.blit(source, dest, area=None, special_flags = 0): return Rect
# draw one image onto another
#
# Draws a source Surface onto this Surface. The draw can be positioned
# with the dest argument. Dest can either be pair of coordinates
# representing the upper left corner of the source. A Rect can also be
# passed as the destination and the topleft corner of the rectangle
# will be used as the position for the blit. The size of the
# destination rectangle does not effect the blit.
#
# An optional area rectangle can be passed as well. This represents a
# smaller portion of the source Surface to draw.
#
# An optional special flags is for passing in new in 1.8.0: BLEND_ADD,
# BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX new in 1.8.1:
# BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN,
# BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT,
# BLEND_RGB_MIN, BLEND_RGB_MAX With other special blitting flags
# perhaps added in the future.
#
# The return rectangle is the area of the affected pixels, excluding
# any pixels outside the destination Surface, or outside the clipping
# area.
#
# Pixel alphas will be ignored when blitting to an 8 bit Surface.
# special_flags new in pygame 1.8.
self.fail()
def test_fill_blend_rgba(self):
destinations = [self._make_surface(8),
self._make_surface(16),
self._make_surface(16, srcalpha=True),
self._make_surface(24),
self._make_surface(32),
self._make_surface(32, srcalpha=True)]
blend = [('BLEND_RGBA_ADD', (0, 25, 100, 255),
lambda a, b: min(a + b, 255)),
('BLEND_RGBA_SUB', (0, 25, 100, 255),
lambda a, b: max(a - b, 0)),
('BLEND_RGBA_MULT', (0, 7, 100, 255),
lambda a, b: (a * b) // 256),
('BLEND_RGBA_MIN', (0, 255, 0, 255), min),
('BLEND_RGBA_MAX', (0, 255, 0, 255), max)]
for dst in destinations:
dst_palette = [dst.unmap_rgb(dst.map_rgb(c))
for c in self._test_palette]
for blend_name, fill_color, op in blend:
fc = dst.unmap_rgb(dst.map_rgb(fill_color))
self._fill_surface(dst)
p = []
for dc in dst_palette:
c = [op(dc[i], fc[i]) for i in range(4)]
if not dst.get_masks()[3]:
c[3] = 255
c = dst.unmap_rgb(dst.map_rgb(c))
p.append(c)
dst.fill(fill_color, special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, ", %s" % blend_name)
def rounded_rect(surface, rect, color, radius=0.4):
"""
rounded_rect(surface,rect,color,radius=0.4)
surface : destination
rect : rectangle
color : rgb or rgba
radius : 0 <= radius <= 1
"""
rect = pygame.Rect(rect)
color = pygame.Color(*color)
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 surface.blit(rectangle, pos)
def filled_rounded_rect(surface,rect,color,radius=0.4):
"""
AAfilledRoundedRect(surface,rect,color,radius=0.4)
surface : destination
rect : rectangle
color : rgb or rgba
radius : 0 <= radius <= 1
"""
rect = pygame.Rect(rect)
color = pygame.Color(*color)
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 surface.blit(rectangle,pos)
def draw_roundrect(surface, rect, color, radius=0.4):
rect = pygame.Rect(rect)
color = pygame.Color(*color)
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 surface.blit(rectangle, pos)
def update(self, dt):
pos = self.visible_position
if pos[0] == -1:
return
player_surface = pygame.Surface(self.rect.size).convert_alpha()
for i in range(220,0,-10):
pygame.draw.circle(player_surface,(0,0,0,i),(int(pos[0]),int(pos[1])),int(i/220.0*10+90))
pygame.draw.circle(player_surface,(0,0,0,0),(int(pos[0]),int(pos[1])),90)
self.image.blit(player_surface,(0,0),special_flags=pygame.BLEND_RGBA_MIN)
pygame.draw.circle(self.visible_area, (255, 0, 0), (int(pos[0]), int(pos[1])), 80)
self.mask = pygame.mask.from_surface(self.visible_area)
def test_fill(self):
pygame.init()
try:
screen = pygame.display.set_mode((640, 480))
# Green and blue test pattern
screen.fill((0, 255, 0), (0, 0, 320, 240))
screen.fill((0, 255, 0), (320, 240, 320, 240))
screen.fill((0, 0, 255), (320, 0, 320, 240))
screen.fill((0, 0, 255), (0, 240, 320, 240))
# Now apply a clip rect, such that only the left side of the
# screen should be effected by blit opperations.
screen.set_clip((0, 0, 320, 480))
# Test fills with each special flag, and additionaly without any.
screen.fill((255, 0, 0, 127), (160, 0, 320, 30), 0)
screen.fill((255, 0, 0, 127), (160, 30, 320, 30), pygame.BLEND_ADD)
screen.fill((0, 127, 127, 127), (160, 60, 320, 30), pygame.BLEND_SUB)
screen.fill((0, 63, 63, 127), (160, 90, 320, 30), pygame.BLEND_MULT)
screen.fill((0, 127, 127, 127), (160, 120, 320, 30), pygame.BLEND_MIN)
screen.fill((127, 0, 0, 127), (160, 150, 320, 30), pygame.BLEND_MAX)
screen.fill((255, 0, 0, 127), (160, 180, 320, 30), pygame.BLEND_RGBA_ADD)
screen.fill((0, 127, 127, 127), (160, 210, 320, 30), pygame.BLEND_RGBA_SUB)
screen.fill((0, 63, 63, 127), (160, 240, 320, 30), pygame.BLEND_RGBA_MULT)
screen.fill((0, 127, 127, 127), (160, 270, 320, 30), pygame.BLEND_RGBA_MIN)
screen.fill((127, 0, 0, 127), (160, 300, 320, 30), pygame.BLEND_RGBA_MAX)
screen.fill((255, 0, 0, 127), (160, 330, 320, 30), pygame.BLEND_RGB_ADD)
screen.fill((0, 127, 127, 127), (160, 360, 320, 30), pygame.BLEND_RGB_SUB)
screen.fill((0, 63, 63, 127), (160, 390, 320, 30), pygame.BLEND_RGB_MULT)
screen.fill((0, 127, 127, 127), (160, 420, 320, 30), pygame.BLEND_RGB_MIN)
screen.fill((255, 0, 0, 127), (160, 450, 320, 30), pygame.BLEND_RGB_MAX)
# Update the display so we can see the results
pygame.display.flip()
# Compare colors on both sides of window
y = 5
while y < 480:
self.assertEquals(screen.get_at((10, y)),
screen.get_at((330, 480 - y)))
y += 10
finally:
pygame.quit()
def __init__(self, board, grid_x=0, grid_y=0, grid_w=1, grid_h=1, value="", color=(0, 0, 0), alpha=False, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.grid_x = grid_x
self.grid_y = grid_y
self.grid_w = grid_w
self.grid_h = grid_h
self.grid_last_x = grid_x
self.grid_last_y = grid_y
self.alpha = alpha
self.board = board
self.initcolor = color
self.color = color
self.decolorable = True
self.locked = False
self.lockable = False
self.value = value
self.speaker_val = value
self.speaker_val_update = True
self.outline = False
self.perm_outline = False
self.perm_outline_color = [255, 0, 0]
self.perm_outline_width = 2
self.hasimg = False
self.draggable = True
self.animable = True
self.keyable = True
self.show_value = True
self.readable = True
self.highlight = True
self.audible = False # use true to enable sounds on unit move
self.outline_highlight = False
self.font_color = (0, 0, 0, 0)
self.align = 0 # align: 0 - centered, 1 - left, 2 - right
self.valign = 0 # align: 0 - centered, 1 - top
self.idx = 0 # position in sequence
self.update_me = True
self.check_display = None # None - none, True - correct, False - wrong
self.checkable = False
# Set height, width, the -1 is to give it some space around for the margin
if self.alpha:
self.image = pygame.Surface([grid_w * board.scale - 1, grid_h * board.scale - 1], flags=pygame.SRCALPHA)
else:
self.image = pygame.Surface([grid_w * board.scale - 1, grid_h * board.scale - 1])
self.image.fill(self.color)
# http://www.pygame.org/docs/ref/surface.html - surface.fill() comment
# self.image = pygame.Surface([grid_w*board.scale-1, grid_h*board.scale-1],flags=pygame.SRCALPHA)
# self.image.fill(self.color,special_flags=pygame.BLEND_RGBA_MIN)
self.painting = self.image
# Make our top-left corner the passed-in location. The +1 is the margin
self.rect = self.image.get_rect()
self.rect.topleft = [grid_x * board.scale + 1, grid_y * board.scale + 1]
# scale font size:
self.font = board.font_sizes[0]
self.text_wrap = True
self.is_door = False