@@ -4038,6 +4038,76 @@ def test_surface_premul_alpha(self):
4038
4038
),
4039
4039
)
4040
4040
4041
+ def test_surface_premul_alpha_ip (self ):
4042
+ """Ensure that .premul_alpha_ip() works correctly"""
4043
+
4044
+ # basic functionality at valid bit depths - 32, 16 & 8
4045
+ s1 = pygame .Surface ((100 , 100 ), pygame .SRCALPHA , 32 )
4046
+ s1 .fill (pygame .Color (255 , 255 , 255 , 100 ))
4047
+ s1 .premul_alpha_ip ()
4048
+ s1_alpha = s1
4049
+ self .assertEqual (s1_alpha .get_at ((50 , 50 )), pygame .Color (100 , 100 , 100 , 100 ))
4050
+
4051
+ # 16-bit color has less precision
4052
+ s2 = pygame .Surface ((100 , 100 ), pygame .SRCALPHA , 16 )
4053
+ s2 .fill (
4054
+ pygame .Color (
4055
+ int (15 / 15 * 255 ),
4056
+ int (15 / 15 * 255 ),
4057
+ int (15 / 15 * 255 ),
4058
+ int (10 / 15 * 255 ),
4059
+ )
4060
+ )
4061
+ s2 .premul_alpha_ip ()
4062
+ s2_alpha = s2
4063
+ self .assertEqual (
4064
+ s2_alpha .get_at ((50 , 50 )),
4065
+ pygame .Color (
4066
+ int (10 / 15 * 255 ),
4067
+ int (10 / 15 * 255 ),
4068
+ int (10 / 15 * 255 ),
4069
+ int (10 / 15 * 255 ),
4070
+ ),
4071
+ )
4072
+
4073
+ # invalid surface - we need alpha to pre-multiply
4074
+ invalid_surf = pygame .Surface ((100 , 100 ), 0 , 32 )
4075
+ invalid_surf .fill (pygame .Color (255 , 255 , 255 , 100 ))
4076
+ with self .assertRaises (ValueError ):
4077
+ invalid_surf .premul_alpha_ip ()
4078
+
4079
+ # churn a bunch of values
4080
+ test_colors = [
4081
+ (200 , 30 , 74 ),
4082
+ (76 , 83 , 24 ),
4083
+ (184 , 21 , 6 ),
4084
+ (74 , 4 , 74 ),
4085
+ (76 , 83 , 24 ),
4086
+ (184 , 21 , 234 ),
4087
+ (160 , 30 , 74 ),
4088
+ (96 , 147 , 204 ),
4089
+ (198 , 201 , 60 ),
4090
+ (132 , 89 , 74 ),
4091
+ (245 , 9 , 224 ),
4092
+ (184 , 112 , 6 ),
4093
+ ]
4094
+
4095
+ for r , g , b in test_colors :
4096
+ for a in range (255 ):
4097
+ with self .subTest (r = r , g = g , b = b , a = a ):
4098
+ surf = pygame .Surface ((10 , 10 ), pygame .SRCALPHA , 32 )
4099
+ surf .fill (pygame .Color (r , g , b , a ))
4100
+ surf .premul_alpha_ip ()
4101
+ self .assertEqual (
4102
+ surf .get_at ((5 , 5 )),
4103
+ Color (
4104
+ ((r + 1 ) * a ) >> 8 ,
4105
+ ((g + 1 ) * a ) >> 8 ,
4106
+ ((b + 1 ) * a ) >> 8 ,
4107
+ a ,
4108
+ ),
4109
+ )
4110
+
4041
4111
4042
4112
class SurfaceSelfBlitTest (unittest .TestCase ):
4043
4113
"""Blit to self tests.
0 commit comments