def test_scale2xraw(self): w, h = 32, 32 s = pygame.Surface((w, h), pygame.SRCALPHA, 32) s.fill((0, 0, 0)) pygame.draw.circle(s, (255, 0, 0), (w // 2, h // 2), (w // 3)) s2 = pygame.transform.scale(s, (w * 2, h * 2)) s2_2 = pygame.transform.scale(s2, (w * 4, h * 4)) s4 = pygame.transform.scale(s, (w * 4, h * 4)) self.assertEqual(s2_2.get_rect().size, (128, 128)) for pt in test_utils.rect_area_pts(s2_2.get_rect()): self.assertEqual(s2_2.get_at(pt), s4.get_at(pt))
def test_threshold_dest_surf_not_change(self): """ the pixels within the threshold. All pixels not within threshold are changed to set_color. So there should be none changed in this test. """ (w, h) = size = (32, 32) threshold = (20, 20, 20, 20) original_color = (25, 25, 25, 25) original_dest_color = (65, 65, 65, 55) threshold_color = (10, 10, 10, 10) set_color = (255, 10, 10, 10) surf = pygame.Surface(size, pygame.SRCALPHA, 32) dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32) search_surf = pygame.Surface(size, pygame.SRCALPHA, 32) surf.fill(original_color) search_surf.fill(threshold_color) dest_surf.fill(original_dest_color) # set_behavior=1, set dest_surface from set_color. # all within threshold of third_surface, so no color is set. THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1 pixels_within_threshold = pygame.transform.threshold( dest_surf=dest_surf, surf=surf, search_color=None, threshold=threshold, set_color=set_color, set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR, search_surf=search_surf, ) # # Return, of pixels within threshold is correct self.assertEqual(w * h, pixels_within_threshold) # # Size of dest surface is correct dest_rect = dest_surf.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is not the change_color specified for every pixel As all # pixels are within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assertNotEqual(dest_surf.get_at(pt), set_color) self.assertEqual(dest_surf.get_at(pt), original_dest_color)
def test_threshold_dest_surf_not_change(self): """ the pixels within the threshold. All pixels not within threshold are changed to set_color. So there should be none changed in this test. """ (w, h) = size = (32, 32) threshold = (20, 20, 20, 20) original_color = (25, 25, 25, 25) original_dest_color = (65, 65, 65, 55) threshold_color = (10, 10, 10, 10) set_color = (255, 10, 10, 10) surf = pygame.Surface(size, pygame.SRCALPHA, 32) dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32) search_surf = pygame.Surface(size, pygame.SRCALPHA, 32) surf.fill(original_color) search_surf.fill(threshold_color) dest_surf.fill(original_dest_color) # set_behavior=1, set dest_surface from set_color. # all within threshold of third_surface, so no color is set. THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1 pixels_within_threshold = pygame.transform.threshold( dest_surf=dest_surf, surf=surf, search_color=None, threshold=threshold, set_color=set_color, set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR, search_surf=search_surf, ) # # Return, of pixels within threshold is correct self.assertEqual(w*h, pixels_within_threshold) # # Size of dest surface is correct dest_rect = dest_surf.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is not the change_color specified for every pixel As all # pixels are within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assertNotEqual(dest_surf.get_at(pt), set_color) self.assertEqual(dest_surf.get_at(pt), original_dest_color)
def test_line(self): # __doc__ (as of 2008-06-25) for pygame.draw.line: # pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect # draw a straight line segment drawn = draw.line(self.surf, self.color, (1, 0), (200, 0)) #(l, t), (l, t) self.assert_(drawn.right == 201, "end point arg should be (or at least was) inclusive" ) #Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(drawn): self.assert_(self.surf.get_at(pt) == self.color) #And not where it shouldn't for pt in test_utils.rect_outer_bounds(drawn): self.assert_(self.surf.get_at(pt) != self.color)
def test_rect__fill(self): # __doc__ (as of 2008-06-25) for pygame.draw.rect: # pygame.draw.rect(Surface, color, Rect, width=0): return Rect # draw a rectangle shape rect = pygame.Rect(10, 10, 25, 20) drawn = draw.rect(self.surf, self.color, rect, 0) self.assertEqual(drawn, rect) # Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(rect): color_at_pt = self.surf.get_at(pt) self.assertEqual(color_at_pt, self.color) # And not where it shouldn't for pt in test_utils.rect_outer_bounds(rect): color_at_pt = self.surf.get_at(pt) self.assertNotEqual(color_at_pt, self.color) # Issue #310: Cannot draw rectangles that are 1 pixel high bgcolor = pygame.Color('black') self.surf.fill(bgcolor) hrect = pygame.Rect(1, 1, self.surf_w - 2, 1) vrect = pygame.Rect(1, 3, 1, self.surf_h - 4) drawn = draw.rect(self.surf, self.color, hrect, 0) self.assertEqual(drawn, hrect) x, y = hrect.topleft w, h = hrect.size self.assertEqual(self.surf.get_at((x - 1, y)), bgcolor) self.assertEqual(self.surf.get_at((x + w, y)), bgcolor) for i in range(x, x + w): self.assertEqual(self.surf.get_at((i, y)), self.color) drawn = draw.rect(self.surf, self.color, vrect, 0) self.assertEqual(drawn, vrect) x, y = vrect.topleft w, h = vrect.size self.assertEqual(self.surf.get_at((x, y - 1)), bgcolor) self.assertEqual(self.surf.get_at((x, y + h)), bgcolor) for i in range(y, y + h): self.assertEqual(self.surf.get_at((x, i)), self.color)
def test_rect__fill(self): # __doc__ (as of 2008-06-25) for pygame.draw.rect: # pygame.draw.rect(Surface, color, Rect, width=0): return Rect # draw a rectangle shape rect = pygame.Rect(10, 10, 25, 20) drawn = draw.rect(self.surf, self.color, rect, 0) self.assert_(drawn == rect) #Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(rect): color_at_pt = self.surf.get_at(pt) self.assert_(color_at_pt == self.color) #And not where it shouldn't for pt in test_utils.rect_outer_bounds(rect): color_at_pt = self.surf.get_at(pt) self.assert_(color_at_pt != self.color) #Issue #310: Cannot draw rectangles that are 1 pixel high bgcolor = pygame.Color('black') self.surf.fill(bgcolor) hrect = pygame.Rect(1, 1, self.surf_w - 2, 1) vrect = pygame.Rect(1, 3, 1, self.surf_h - 4) drawn = draw.rect(self.surf, self.color, hrect, 0) self.assert_(drawn == hrect) x, y = hrect.topleft w, h = hrect.size self.assertEqual(self.surf.get_at((x - 1, y)), bgcolor) self.assertEqual(self.surf.get_at((x + w, y)), bgcolor) for i in range(x, x + w): self.assertEqual(self.surf.get_at((i, y)), self.color) drawn = draw.rect(self.surf, self.color, vrect, 0) self.assertEqual(drawn, vrect) x, y = vrect.topleft w, h = vrect.size self.assertEqual(self.surf.get_at((x, y - 1)), bgcolor) self.assertEqual(self.surf.get_at((x, y + h)), bgcolor) for i in range(y, y + h): self.assertEqual(self.surf.get_at((x, i)), self.color)
def test_rect__fill(self): # __doc__ (as of 2008-06-25) for pygame.draw.rect: # pygame.draw.rect(Surface, color, Rect, width=0): return Rect # draw a rectangle shape rect = pygame.Rect(10, 10, 25, 20) drawn = draw.rect(self.surf, self.color, rect, 0) self.assert_(drawn == rect) #Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(rect): color_at_pt = self.surf.get_at(pt) self.assert_(color_at_pt == self.color) #And not where it shouldn't for pt in test_utils.rect_outer_bounds(rect): color_at_pt = self.surf.get_at(pt) self.assert_(color_at_pt != self.color)
def test_threshold_dest_surf_all_changed(self): """ Lowering the threshold, expecting changed surface """ (w, h) = size = (32, 32) threshold = (20, 20, 20, 20) original_color = (25, 25, 25, 25) original_dest_color = (65, 65, 65, 55) threshold_color = (10, 10, 10, 10) set_color = (255, 10, 10, 10) surf = pygame.Surface(size, pygame.SRCALPHA, 32) dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32) search_surf = pygame.Surface(size, pygame.SRCALPHA, 32) surf.fill(original_color) search_surf.fill(threshold_color) dest_surf.fill(original_dest_color) THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1 pixels_within_threshold = pygame.transform.threshold( dest_surf, surf, search_color=None, set_color=set_color, set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR, search_surf=search_surf, ) self.assertEqual(0, pixels_within_threshold) dest_rect = dest_surf.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is the set_color specified for every pixel As all # pixels are not within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assertEqual(dest_surf.get_at(pt), set_color)
def test_threshold_dest_surf_all_changed(self): """ Lowering the threshold, expecting changed surface """ (w, h) = size = (32, 32) threshold = (20, 20, 20, 20) original_color = (25, 25, 25, 25) original_dest_color = (65, 65, 65, 55) threshold_color = (10, 10, 10, 10) set_color = (255, 10, 10, 10) surf = pygame.Surface(size, pygame.SRCALPHA, 32) dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32) search_surf = pygame.Surface(size, pygame.SRCALPHA, 32) surf.fill(original_color) search_surf.fill(threshold_color) dest_surf.fill(original_dest_color) THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1 pixels_within_threshold = pygame.transform.threshold ( dest_surf, surf, search_color=None, set_color=set_color, set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR, search_surf=search_surf, ) self.assertEqual(0, pixels_within_threshold) dest_rect = dest_surf.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is the set_color specified for every pixel As all # pixels are not within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assertEqual(dest_surf.get_at(pt), set_color)
def test_threshold__honors_third_surface(self): # __doc__ for threshold as of Tue 07/15/2008 # pygame.transform.threshold(DestSurface, Surface, color, threshold = # (0,0,0,0), diff_color = (0,0,0,0), change_return = True, Surface = # None): return num_threshold_pixels # When given the optional third # surface, it would use the colors in that rather than the "color" # specified in the function to check against. # New in pygame 1.8 ################################################################ # Sizes (w, h) = size = (32, 32) # the original_color is within the threshold of the threshold_color threshold = (20, 20, 20, 20) original_color = (25,25,25,25) threshold_color = (10, 10, 10, 10) # Surfaces original_surface = pygame.Surface(size, pygame.SRCALPHA, 32) dest_surface = pygame.Surface(size, pygame.SRCALPHA, 32) # Third surface is used in lieu of 3rd position arg color third_surface = pygame.Surface(size, pygame.SRCALPHA, 32) # Color filling original_surface.fill(original_color) third_surface.fill(threshold_color) ################################################################ # All pixels for color should be within threshold # pixels_within_threshold = pygame.transform.threshold ( dest_surface, original_surface, threshold_color, threshold, 0, # diff_color 0 # change_return ) self.assertEqual(w*h, pixels_within_threshold) ################################################################ # This should respect third_surface colors in place of 3rd arg # color Should be the same as: surface.fill(threshold_color) # all within threshold pixels_within_threshold = pygame.transform.threshold ( dest_surface, original_surface, 0, # color (would fail if honored) threshold, 0, # diff_color 0, # change_return third_surface, ) self.assertEqual(w*h, pixels_within_threshold) ################################################################ # Change dest_surface on return (not expected) change_color = (255, 10, 10, 10) pixels_within_threshold = pygame.transform.threshold ( dest_surface, original_surface, 0, # color threshold, change_color, # diff_color 1, # change_return third_surface, ) # Return, of pixels within threshold is correct self.assertEqual(w*h, pixels_within_threshold) # Size of dest surface is correct dest_rect = dest_surface.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is not the change_color specified for every pixel As all # pixels are within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assert_(dest_surface.get_at(pt) != change_color) ################################################################ # Lowering the threshold, expecting changed surface pixels_within_threshold = pygame.transform.threshold ( dest_surface, original_surface, 0, # color 0, # threshold change_color, # diff_color 1, # change_return third_surface, ) # Return, of pixels within threshold is correct self.assertEqual(0, pixels_within_threshold) # Size of dest surface is correct dest_rect = dest_surface.get_rect() dest_size = dest_rect.size self.assertEqual(size, dest_size) # The color is the change_color specified for every pixel As all # pixels are not within threshold for pt in test_utils.rect_area_pts(dest_rect): self.assertEqual(dest_surface.get_at(pt), change_color)
def test_line(self): # __doc__ (as of 2008-06-25) for pygame.draw.line: # pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect # draw a straight line segment # (l, t), (l, t) drawn = draw.line(self.surf, self.color, (1, 0), (200, 0)) self.assertEqual(drawn.right, 201, "end point arg should be (or at least was) inclusive") # Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(drawn): self.assertEqual(self.surf.get_at(pt), self.color) # And not where it shouldn't for pt in test_utils.rect_outer_bounds(drawn): self.assertNotEqual(self.surf.get_at(pt), self.color) # Line width greater that 1 line_width = 2 offset = 5 a = (offset, offset) b = (self.surf_size[0] - offset, a[1]) c = (a[0], self.surf_size[1] - offset) d = (b[0], c[1]) e = (a[0] + offset, c[1]) f = (b[0], c[0] + 5) lines = [(a, d), (b, c), (c, b), (d, a), (a, b), (b, a), (a, c), (c, a), (a, e), (e, a), (a, f), (f, a), (a, a),] for p1, p2 in lines: msg = "%s - %s" % (p1, p2) if p1[0] <= p2[0]: plow = p1 phigh = p2 else: plow = p2 phigh = p1 self.surf.fill((0, 0, 0)) rec = draw.line(self.surf, (255, 255, 255), p1, p2, line_width) xinc = yinc = 0 if abs(p1[0] - p2[0]) > abs(p1[1] - p2[1]): yinc = 1 else: xinc = 1 for i in range(line_width): p = (p1[0] + xinc * i, p1[1] + yinc * i) self.assertEqual(self.surf.get_at(p), (255, 255, 255), msg) p = (p2[0] + xinc * i, p2[1] + yinc * i) self.assertEqual(self.surf.get_at(p), (255, 255, 255), msg) p = (plow[0] - 1, plow[1]) self.assertEqual(self.surf.get_at(p), (0, 0, 0), msg) p = (plow[0] + xinc * line_width, plow[1] + yinc * line_width) self.assertEqual(self.surf.get_at(p), (0, 0, 0), msg) p = (phigh[0] + xinc * line_width, phigh[1] + yinc * line_width) self.assertEqual(self.surf.get_at(p), (0, 0, 0), msg) if p1[0] < p2[0]: rx = p1[0] else: rx = p2[0] if p1[1] < p2[1]: ry = p1[1] else: ry = p2[1] w = abs(p2[0] - p1[0]) + 1 + xinc * (line_width - 1) h = abs(p2[1] - p1[1]) + 1 + yinc * (line_width - 1) msg += ", %s" % (rec,) self.assertEqual(rec, (rx, ry, w, h), msg)
def test_line(self): # __doc__ (as of 2008-06-25) for pygame.draw.line: # pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect # draw a straight line segment drawn = draw.line(self.surf, self.color, (1, 0), (200, 0)) #(l, t), (l, t) self.assert_(drawn.right == 201, "end point arg should be (or at least was) inclusive" ) #Should be colored where it's supposed to be for pt in test_utils.rect_area_pts(drawn): self.assert_(self.surf.get_at(pt) == self.color) #And not where it shouldn't for pt in test_utils.rect_outer_bounds(drawn): self.assert_(self.surf.get_at(pt) != self.color) #Line width greater that 1 line_width = 2 offset = 5 a = (offset, offset) b = (self.surf_size[0] - offset, a[1]) c = (a[0], self.surf_size[1] - offset) d = (b[0], c[1]) e = (a[0] + offset, c[1]) f = (b[0], c[0] + 5) lines = [(a, d), (b, c), (c, b), (d, a), (a, b), (b, a), (a, c), (c, a), (a, e), (e, a), (a, f), (f, a), (a, a),] for p1, p2 in lines: msg = "%s - %s" % (p1, p2) if p1[0] <= p2[0]: plow = p1 phigh = p2 else: plow = p2 phigh = p1 self.surf.fill((0, 0, 0)) rec = draw.line(self.surf, (255, 255, 255), p1, p2, line_width) xinc = yinc = 0 if abs(p1[0] - p2[0]) > abs(p1[1] - p2[1]): yinc = 1 else: xinc = 1 for i in range(line_width): p = (p1[0] + xinc * i, p1[1] + yinc * i) self.assert_(self.surf.get_at(p) == (255, 255, 255), msg) p = (p2[0] + xinc * i, p2[1] + yinc * i) self.assert_(self.surf.get_at(p) == (255, 255, 255), msg) p = (plow[0] - 1, plow[1]) self.assert_(self.surf.get_at(p) == (0, 0, 0), msg) p = (plow[0] + xinc * line_width, plow[1] + yinc * line_width) self.assert_(self.surf.get_at(p) == (0, 0, 0), msg) p = (phigh[0] + xinc * line_width, phigh[1] + yinc * line_width) self.assert_(self.surf.get_at(p) == (0, 0, 0), msg) if p1[0] < p2[0]: rx = p1[0] else: rx = p2[0] if p1[1] < p2[1]: ry = p1[1] else: ry = p2[1] w = abs(p2[0] - p1[0]) + 1 + xinc * (line_width - 1) h = abs(p2[1] - p1[1]) + 1 + yinc * (line_width - 1) msg += ", %s" % (rec,) self.assert_(rec == (rx, ry, w, h), msg)