@@ -2905,6 +2905,234 @@ def test_to_surface__area_output(self):
2905
2905
for x , value in enumerate (row ):
2906
2906
self .assertEqual (surface .get_at ((x , y )), value )
2907
2907
2908
+ def create_surface_from_array (
2909
+ self , size : tuple [int , int ], array : list [list [pygame .Color ]], * args , ** kwargs
2910
+ ) -> pygame .Surface :
2911
+ assert (len (array ), len (array [0 ])) == size
2912
+
2913
+ surface = pygame .Surface (size , * args , ** kwargs )
2914
+ for y , row in enumerate (array ):
2915
+ for x , color in enumerate (row ):
2916
+ surface .set_at ((x , y ), color )
2917
+
2918
+ return surface
2919
+
2920
+ def assert_surface_matches_array (
2921
+ self , surface : pygame .Surface , array : list [list [pygame .Color ]]
2922
+ ) -> None :
2923
+ assert (len (array ), len (array [0 ])) == surface .get_size ()
2924
+
2925
+ for y , row in enumerate (array ):
2926
+ for x , color in enumerate (row ):
2927
+ with self .subTest (x = x , y = y , color = color ):
2928
+ self .assertEqual (color , surface .get_at ((x , y )))
2929
+
2930
+ def test_to_surface__area_with_setsurface_create_new (self ):
2931
+ c_1 = pygame .Color ("red" )
2932
+ c_2 = pygame .Color ("green" )
2933
+ size = (4 , 4 )
2934
+
2935
+ mask = pygame .Mask (size , fill = True )
2936
+
2937
+ set_surface = self .create_surface_from_array (
2938
+ size = size ,
2939
+ array = [
2940
+ [c_1 , c_1 , c_2 , c_2 ],
2941
+ [c_1 , c_1 , c_2 , c_2 ],
2942
+ [c_1 , c_1 , c_1 , c_1 ],
2943
+ [c_1 , c_1 , c_1 , c_1 ],
2944
+ ],
2945
+ flags = SRCALPHA ,
2946
+ )
2947
+
2948
+ area = pygame .Rect (0 , 0 , 2 , 2 )
2949
+ expected_pixels = [
2950
+ [c_1 , c_1 ],
2951
+ [c_1 , c_1 ],
2952
+ ]
2953
+ surface = mask .to_surface (area = area , setsurface = set_surface )
2954
+ self .assert_surface_matches_array (surface , expected_pixels )
2955
+
2956
+ area = pygame .Rect (1 , 1 , 2 , 2 )
2957
+ expected_pixels = [
2958
+ [c_1 , c_2 ],
2959
+ [c_1 , c_1 ],
2960
+ ]
2961
+ surface = mask .to_surface (area = area , setsurface = set_surface )
2962
+ self .assert_surface_matches_array (surface , expected_pixels )
2963
+
2964
+ area = pygame .Rect (2 , 1 , 2 , 2 )
2965
+ expected_pixels = [
2966
+ [c_2 , c_2 ],
2967
+ [c_1 , c_1 ],
2968
+ ]
2969
+ surface = mask .to_surface (area = area , setsurface = set_surface )
2970
+ self .assert_surface_matches_array (surface , expected_pixels )
2971
+
2972
+ def test_to_surface__area_with_unsetsurface_create_new (self ):
2973
+ c_1 = pygame .Color ("red" )
2974
+ c_2 = pygame .Color ("green" )
2975
+ size = (4 , 4 )
2976
+
2977
+ mask = pygame .Mask (size , fill = False )
2978
+
2979
+ unset_surface = self .create_surface_from_array (
2980
+ size = size ,
2981
+ array = [
2982
+ [c_1 , c_1 , c_2 , c_2 ],
2983
+ [c_1 , c_1 , c_2 , c_2 ],
2984
+ [c_1 , c_1 , c_1 , c_1 ],
2985
+ [c_1 , c_1 , c_1 , c_1 ],
2986
+ ],
2987
+ flags = SRCALPHA ,
2988
+ )
2989
+
2990
+ area = pygame .Rect (0 , 0 , 2 , 2 )
2991
+ expected_pixels = [
2992
+ [c_1 , c_1 ],
2993
+ [c_1 , c_1 ],
2994
+ ]
2995
+ surface = mask .to_surface (area = area , unsetsurface = unset_surface )
2996
+ self .assert_surface_matches_array (surface , expected_pixels )
2997
+
2998
+ area = pygame .Rect (1 , 1 , 2 , 2 )
2999
+ expected_pixels = [
3000
+ [c_1 , c_2 ],
3001
+ [c_1 , c_1 ],
3002
+ ]
3003
+ surface = mask .to_surface (area = area , unsetsurface = unset_surface )
3004
+ self .assert_surface_matches_array (surface , expected_pixels )
3005
+
3006
+ area = pygame .Rect (2 , 1 , 2 , 2 )
3007
+ expected_pixels = [
3008
+ [c_2 , c_2 ],
3009
+ [c_1 , c_1 ],
3010
+ ]
3011
+ surface = mask .to_surface (area = area , unsetsurface = unset_surface )
3012
+ self .assert_surface_matches_array (surface , expected_pixels )
3013
+
3014
+ def test_to_surface__area_with_setsurface_to_destination (self ):
3015
+ c_1 = pygame .Color ("red" )
3016
+ c_2 = pygame .Color ("green" )
3017
+ c_3 = pygame .Color ("blue" )
3018
+ size = (4 , 4 )
3019
+
3020
+ mask = pygame .Mask (size , fill = True )
3021
+
3022
+ set_surface = self .create_surface_from_array (
3023
+ size = size ,
3024
+ array = [
3025
+ [c_1 , c_1 , c_2 , c_2 ],
3026
+ [c_1 , c_1 , c_2 , c_2 ],
3027
+ [c_1 , c_1 , c_1 , c_1 ],
3028
+ [c_1 , c_1 , c_1 , c_1 ],
3029
+ ],
3030
+ )
3031
+
3032
+ area = pygame .Rect (0 , 0 , 2 , 2 )
3033
+ expected_pixels = [
3034
+ [c_1 , c_1 , c_3 , c_3 ],
3035
+ [c_1 , c_1 , c_3 , c_3 ],
3036
+ [c_3 , c_3 , c_3 , c_3 ],
3037
+ [c_3 , c_3 , c_3 , c_3 ],
3038
+ ]
3039
+ surface = pygame .Surface (size )
3040
+ surface .fill (c_3 )
3041
+ surface = mask .to_surface (surface = surface , area = area , setsurface = set_surface )
3042
+ self .assert_surface_matches_array (surface , expected_pixels )
3043
+
3044
+ area = pygame .Rect (1 , 1 , 2 , 2 )
3045
+ dest = (2 , 0 )
3046
+ expected_pixels = [
3047
+ [c_3 , c_3 , c_1 , c_2 ],
3048
+ [c_3 , c_3 , c_1 , c_1 ],
3049
+ [c_3 , c_3 , c_3 , c_3 ],
3050
+ [c_3 , c_3 , c_3 , c_3 ],
3051
+ ]
3052
+ surface = pygame .Surface (size )
3053
+ surface .fill (c_3 )
3054
+ surface = mask .to_surface (
3055
+ surface = surface , dest = dest , area = area , setsurface = set_surface
3056
+ )
3057
+ self .assert_surface_matches_array (surface , expected_pixels )
3058
+
3059
+ area = pygame .Rect (2 , 1 , 2 , 2 )
3060
+ dest = (1 , 1 )
3061
+ expected_pixels = [
3062
+ [c_3 , c_3 , c_3 , c_3 ],
3063
+ [c_3 , c_2 , c_2 , c_3 ],
3064
+ [c_3 , c_1 , c_1 , c_3 ],
3065
+ [c_3 , c_3 , c_3 , c_3 ],
3066
+ ]
3067
+ surface = pygame .Surface (size )
3068
+ surface .fill (c_3 )
3069
+ surface = mask .to_surface (
3070
+ surface = surface , dest = dest , area = area , setsurface = set_surface
3071
+ )
3072
+ self .assert_surface_matches_array (surface , expected_pixels )
3073
+
3074
+ def test_to_surface__area_with_unsetsurface_to_destination (self ):
3075
+ c_1 = pygame .Color ("red" )
3076
+ c_2 = pygame .Color ("green" )
3077
+ c_3 = pygame .Color ("blue" )
3078
+ size = (4 , 4 )
3079
+
3080
+ mask = pygame .Mask (size , fill = False )
3081
+
3082
+ unset_surface = self .create_surface_from_array (
3083
+ size = size ,
3084
+ array = [
3085
+ [c_1 , c_1 , c_2 , c_2 ],
3086
+ [c_1 , c_1 , c_2 , c_2 ],
3087
+ [c_1 , c_1 , c_1 , c_1 ],
3088
+ [c_1 , c_1 , c_1 , c_1 ],
3089
+ ],
3090
+ )
3091
+
3092
+ area = pygame .Rect (0 , 0 , 2 , 2 )
3093
+ expected_pixels = [
3094
+ [c_1 , c_1 , c_3 , c_3 ],
3095
+ [c_1 , c_1 , c_3 , c_3 ],
3096
+ [c_3 , c_3 , c_3 , c_3 ],
3097
+ [c_3 , c_3 , c_3 , c_3 ],
3098
+ ]
3099
+ surface = pygame .Surface (size )
3100
+ surface .fill (c_3 )
3101
+ surface = mask .to_surface (
3102
+ surface = surface , area = area , unsetsurface = unset_surface
3103
+ )
3104
+ self .assert_surface_matches_array (surface , expected_pixels )
3105
+
3106
+ area = pygame .Rect (1 , 1 , 2 , 2 )
3107
+ dest = (2 , 0 )
3108
+ expected_pixels = [
3109
+ [c_3 , c_3 , c_1 , c_2 ],
3110
+ [c_3 , c_3 , c_1 , c_1 ],
3111
+ [c_3 , c_3 , c_3 , c_3 ],
3112
+ [c_3 , c_3 , c_3 , c_3 ],
3113
+ ]
3114
+ surface = pygame .Surface (size )
3115
+ surface .fill (c_3 )
3116
+ surface = mask .to_surface (
3117
+ surface = surface , dest = dest , area = area , unsetsurface = unset_surface
3118
+ )
3119
+ self .assert_surface_matches_array (surface , expected_pixels )
3120
+
3121
+ area = pygame .Rect (2 , 1 , 2 , 2 )
3122
+ dest = (1 , 1 )
3123
+ expected_pixels = [
3124
+ [c_3 , c_3 , c_3 , c_3 ],
3125
+ [c_3 , c_2 , c_2 , c_3 ],
3126
+ [c_3 , c_1 , c_1 , c_3 ],
3127
+ [c_3 , c_3 , c_3 , c_3 ],
3128
+ ]
3129
+ surface = pygame .Surface (size )
3130
+ surface .fill (c_3 )
3131
+ surface = mask .to_surface (
3132
+ surface = surface , dest = dest , area = area , unsetsurface = unset_surface
3133
+ )
3134
+ self .assert_surface_matches_array (surface , expected_pixels )
3135
+
2908
3136
def test_to_surface__area_default (self ):
2909
3137
"""Ensures the default area is correct."""
2910
3138
expected_color = pygame .Color ("white" )
0 commit comments