Skip to content

Commit a917110

Browse files
Starbuck5ankith26
authored andcommitted
Add tests that transform functions keep palettes
Adds tests for two selected transform functions (scale and invert) to check some palette behavior, most importantly that the output surfaces have the same palettes as the input ones.
1 parent c951503 commit a917110

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/transform_test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,44 @@ def test_scale__alpha(self):
173173
self.assertEqual(s.get_alpha(), s3.get_alpha())
174174
self.assertEqual(s.get_alpha(), s2.get_alpha())
175175

176+
def test_scale__palette(self):
177+
"""see if palette information from source is kept.
178+
179+
Test for newsurf_fromsurf regression reported in
180+
https://github.com/pygame-community/pygame-ce/issues/3463"""
181+
182+
s = pygame.Surface((32, 32), depth=8)
183+
s.fill("red", [0, 0, 32, 16])
184+
s.fill("purple", [0, 16, 32, 16])
185+
self.assertTrue(len(s.get_palette()) > 0)
186+
187+
s2 = pygame.transform.scale(s, (64, 64))
188+
self.assertEqual(s.get_palette()[0], s2.get_palette()[0])
189+
self.assertEqual(s.get_at_mapped((0, 0)), s2.get_at_mapped((0, 0)))
190+
self.assertEqual(s.get_at_mapped((0, 17)), s2.get_at_mapped((0, 35)))
191+
192+
# Also test with a custom palette to make sure the output doesn't just
193+
# have a default palette.
194+
s = pygame.Surface((32, 32), depth=8)
195+
s.set_palette(
196+
[
197+
pygame.Color("red"),
198+
pygame.Color("purple"),
199+
pygame.Color("gold"),
200+
pygame.Color("blue"),
201+
pygame.Color("lightgreen"),
202+
]
203+
)
204+
s.fill("red", [0, 0, 32, 16])
205+
s.fill("purple", [0, 16, 32, 16])
206+
self.assertTrue(len(s.get_palette()) > 0)
207+
208+
s2 = pygame.transform.scale(s, (64, 64))
209+
self.assertEqual(s.get_palette()[0], s2.get_palette()[0])
210+
self.assertEqual(s.get_palette(), s2.get_palette())
211+
self.assertEqual(s.get_at_mapped((0, 0)), s2.get_at_mapped((0, 0)))
212+
self.assertEqual(s.get_at_mapped((0, 17)), s2.get_at_mapped((0, 35)))
213+
176214
def test_scale__destination(self):
177215
"""see if the destination surface can be passed in to use."""
178216

@@ -1727,6 +1765,34 @@ def test_invert(self):
17271765
pygame.Surface((10, 10), depth=8),
17281766
)
17291767

1768+
def test_invert__palette(self):
1769+
"""see if palette information from source is kept.
1770+
1771+
Test for newsurf_fromsurf regression reported in
1772+
https://github.com/pygame-community/pygame-ce/issues/3463"""
1773+
1774+
s = pygame.Surface((32, 32), depth=8)
1775+
s.set_palette([pygame.Color("orange") for _ in range(256)])
1776+
s.set_palette(
1777+
[
1778+
pygame.Color("red"),
1779+
pygame.Color("purple"),
1780+
pygame.Color("gold"),
1781+
pygame.Color("blue"),
1782+
pygame.Color("lightgreen"),
1783+
]
1784+
)
1785+
s.fill("red", [0, 0, 32, 16])
1786+
s.fill("blue", [0, 16, 32, 16])
1787+
self.assertTrue(len(s.get_palette()) > 0)
1788+
1789+
s2 = pygame.transform.invert(s)
1790+
self.assertEqual(s.get_palette()[0], s2.get_palette()[0])
1791+
self.assertEqual(s.get_palette(), s2.get_palette())
1792+
1793+
self.assertEqual(s2.get_at((0, 0)), pygame.Color("lightgreen"))
1794+
self.assertEqual(s2.get_at((0, 17)), pygame.Color("gold"))
1795+
17301796
def test_smoothscale(self):
17311797
"""Tests the stated boundaries, sizing, and color blending of smoothscale function"""
17321798
# __doc__ (as of 2008-08-02) for pygame.transform.smoothscale:

0 commit comments

Comments
 (0)