Advertisement
Guest User

Alpha Masking

a guest
Feb 23rd, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. Black color has these maskings.
  2. Regular surface:
  3. ```
  4. surface = regular_surface()
  5. surface.fill((0, 0, 0, 255))
  6.  
  7. === Channel Masks ===
  8. R: 16711680 (0x00FF0000)
  9. G: 65280 (0x0000FF00)
  10. B: 255 (0x000000FF)
  11. A: 0 (0x00000000)
  12.  
  13. === 32-bit Color ===
  14. ARGB Value: 4278190080
  15. Hex: 0xFF000000
  16. ```
  17. Opposite of Amask is 0xFFFFFFFF. Apply AND to 0xFF000000 you get 0xFF000000. Alpha is 255 - opaque.
  18.  
  19. ```
  20. 1111 1111 1111 1111 1111 1111 1111 1111
  21. 1111 1111 0000 0000 0000 0000 0000 0000
  22. =
  23. 1111 1111 0000 0000 0000 0000 0000 0000
  24. ```
  25.  
  26. SRCALPHA surface:
  27. ```
  28. surface = alpha_surface()
  29. surface.fill((0, 0, 0, 255))
  30.  
  31. === Channel Masks ===
  32. R: 16711680 (0x00FF0000)
  33. G: 65280 (0x0000FF00)
  34. B: 255 (0x000000FF)
  35. A: 4278190080 (0xFF000000)
  36.  
  37. === 32-bit Color ===
  38. ARGB Value: 4278190080
  39. Hex: 0xFF000000
  40. ```
  41. Opposite of Amask is 0x00FFFFFF. Apply AND to 0xFF000000 you get 0x00000000. Alpha is 0 - transparent.
  42.  
  43. ```
  44. 0000 0000 1111 1111 1111 1111 1111 1111
  45. 1111 1111 0000 0000 0000 0000 0000 0000
  46. =
  47. 0000 0000 0000 0000 0000 0000 0000 0000
  48. ```
  49.  
  50. Here is the full code so you can compare apples to apples. Only change 'regular' to True/False and the color:
  51. ```
  52. import pygame
  53.  
  54.  
  55. def regular_surface() -> pygame.Surface:
  56. return pygame.Surface((50, 50))
  57.  
  58.  
  59. def alpha_surface() -> pygame.Surface:
  60. return pygame.Surface((50, 50), flags=pygame.SRCALPHA)
  61.  
  62.  
  63. def get_32_bit_argb(color: pygame.Color) -> int:
  64. bits = 8
  65. return color.a << (bits * 3) | color.r << (bits * 2) | color.g << (bits * 1) | color.b << (bits * 0)
  66.  
  67.  
  68. def apply_alpha_masking(the_32_bit: int, a_mask: int) -> int:
  69. all_32_bits = 0xFFFFFFFF
  70. return the_32_bit & (~a_mask & all_32_bits)
  71.  
  72.  
  73. def display_information(surface: pygame.Surface) -> None:
  74. color = surface.get_at((0, 0))
  75. r_mask, g_mask, b_mask, a_mask = surface.get_masks()
  76. the_32_bit = get_32_bit_argb(color)
  77. masked = apply_alpha_masking(the_32_bit, a_mask)
  78. r, g, b, a = color
  79.  
  80. thirty_two_bit_width = 10
  81. eight_bit_width = 3
  82.  
  83. sections = [
  84. ("Surface Information", [
  85. f"Surface Object: {surface}",
  86. f"Dimensions: {surface.get_width():>4}x{surface.get_height()}"
  87. ]),
  88.  
  89. ("Pixel Color at (0,0)", [
  90. f"RGBA: ({r:>{eight_bit_width}}, {g:>{eight_bit_width}}, {b:>{eight_bit_width}}, {a:>{eight_bit_width}})",
  91. f"R: {r:>{eight_bit_width}} (0x{r:02X})",
  92. f"G: {g:>{eight_bit_width}} (0x{g:02X})",
  93. f"B: {b:>{eight_bit_width}} (0x{b:02X})",
  94. f"A: {a:>{eight_bit_width}} (0x{a:02X})"
  95. ]),
  96.  
  97. ("Channel Masks", [
  98. f"R: {r_mask:>{thirty_two_bit_width}} (0x{r_mask:08X})",
  99. f"G: {g_mask:>{thirty_two_bit_width}} (0x{g_mask:08X})",
  100. f"B: {b_mask:>{thirty_two_bit_width}} (0x{b_mask:08X})",
  101. f"A: {a_mask:>{thirty_two_bit_width}} (0x{a_mask:08X})"
  102. ]),
  103.  
  104. ("32-bit Color", [
  105. f"ARGB Value: {the_32_bit:>{thirty_two_bit_width}}",
  106. f"Hex: {f'0x{the_32_bit:08X}':>{thirty_two_bit_width}}"
  107. ]),
  108.  
  109. ("Alpha-masked Color", [
  110. f"Masked Value: {masked:>{thirty_two_bit_width}}",
  111. f"Hex: {f'0x{masked:08X}':>{thirty_two_bit_width}}"
  112. ])
  113. ]
  114.  
  115. for header, content in sections:
  116. print(f"\n=== {header} ===")
  117. for line in content:
  118. print(f" {line}")
  119.  
  120.  
  121. def main():
  122. regular = False
  123.  
  124. if regular:
  125. surface = regular_surface()
  126. else:
  127. surface = alpha_surface()
  128.  
  129. surface.fill((0, 0, 0, 255))
  130. display_information(surface)
  131.  
  132. display = pygame.display.set_mode((500, 500))
  133. clock = pygame.Clock()
  134. running = True
  135. angle = 0
  136.  
  137. while running:
  138. for event in pygame.event.get():
  139. if event.type == pygame.QUIT:
  140. running = False
  141.  
  142. display.fill("grey")
  143.  
  144. rotated_surface = pygame.transform.rotate(surface, angle)
  145. rect = rotated_surface.get_rect(center=(250, 250))
  146. display.blit(rotated_surface, rect)
  147. pygame.draw.rect(display, "white", rect, 1)
  148.  
  149. pygame.display.flip()
  150.  
  151. clock.tick(60)
  152. angle += 1
  153.  
  154.  
  155. if __name__ == '__main__':
  156. main()
  157.  
  158. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement