Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Black color has these maskings.
- Regular surface:
- ```
- surface = regular_surface()
- surface.fill((0, 0, 0, 255))
- === Channel Masks ===
- R: 16711680 (0x00FF0000)
- G: 65280 (0x0000FF00)
- B: 255 (0x000000FF)
- A: 0 (0x00000000)
- === 32-bit Color ===
- ARGB Value: 4278190080
- Hex: 0xFF000000
- ```
- Opposite of Amask is 0xFFFFFFFF. Apply AND to 0xFF000000 you get 0xFF000000. Alpha is 255 - opaque.
- ```
- 1111 1111 1111 1111 1111 1111 1111 1111
- 1111 1111 0000 0000 0000 0000 0000 0000
- =
- 1111 1111 0000 0000 0000 0000 0000 0000
- ```
- SRCALPHA surface:
- ```
- surface = alpha_surface()
- surface.fill((0, 0, 0, 255))
- === Channel Masks ===
- R: 16711680 (0x00FF0000)
- G: 65280 (0x0000FF00)
- B: 255 (0x000000FF)
- A: 4278190080 (0xFF000000)
- === 32-bit Color ===
- ARGB Value: 4278190080
- Hex: 0xFF000000
- ```
- Opposite of Amask is 0x00FFFFFF. Apply AND to 0xFF000000 you get 0x00000000. Alpha is 0 - transparent.
- ```
- 0000 0000 1111 1111 1111 1111 1111 1111
- 1111 1111 0000 0000 0000 0000 0000 0000
- =
- 0000 0000 0000 0000 0000 0000 0000 0000
- ```
- Here is the full code so you can compare apples to apples. Only change 'regular' to True/False and the color:
- ```
- import pygame
- def regular_surface() -> pygame.Surface:
- return pygame.Surface((50, 50))
- def alpha_surface() -> pygame.Surface:
- return pygame.Surface((50, 50), flags=pygame.SRCALPHA)
- def get_32_bit_argb(color: pygame.Color) -> int:
- bits = 8
- return color.a << (bits * 3) | color.r << (bits * 2) | color.g << (bits * 1) | color.b << (bits * 0)
- def apply_alpha_masking(the_32_bit: int, a_mask: int) -> int:
- all_32_bits = 0xFFFFFFFF
- return the_32_bit & (~a_mask & all_32_bits)
- def display_information(surface: pygame.Surface) -> None:
- color = surface.get_at((0, 0))
- r_mask, g_mask, b_mask, a_mask = surface.get_masks()
- the_32_bit = get_32_bit_argb(color)
- masked = apply_alpha_masking(the_32_bit, a_mask)
- r, g, b, a = color
- thirty_two_bit_width = 10
- eight_bit_width = 3
- sections = [
- ("Surface Information", [
- f"Surface Object: {surface}",
- f"Dimensions: {surface.get_width():>4}x{surface.get_height()}"
- ]),
- ("Pixel Color at (0,0)", [
- f"RGBA: ({r:>{eight_bit_width}}, {g:>{eight_bit_width}}, {b:>{eight_bit_width}}, {a:>{eight_bit_width}})",
- f"R: {r:>{eight_bit_width}} (0x{r:02X})",
- f"G: {g:>{eight_bit_width}} (0x{g:02X})",
- f"B: {b:>{eight_bit_width}} (0x{b:02X})",
- f"A: {a:>{eight_bit_width}} (0x{a:02X})"
- ]),
- ("Channel Masks", [
- f"R: {r_mask:>{thirty_two_bit_width}} (0x{r_mask:08X})",
- f"G: {g_mask:>{thirty_two_bit_width}} (0x{g_mask:08X})",
- f"B: {b_mask:>{thirty_two_bit_width}} (0x{b_mask:08X})",
- f"A: {a_mask:>{thirty_two_bit_width}} (0x{a_mask:08X})"
- ]),
- ("32-bit Color", [
- f"ARGB Value: {the_32_bit:>{thirty_two_bit_width}}",
- f"Hex: {f'0x{the_32_bit:08X}':>{thirty_two_bit_width}}"
- ]),
- ("Alpha-masked Color", [
- f"Masked Value: {masked:>{thirty_two_bit_width}}",
- f"Hex: {f'0x{masked:08X}':>{thirty_two_bit_width}}"
- ])
- ]
- for header, content in sections:
- print(f"\n=== {header} ===")
- for line in content:
- print(f" {line}")
- def main():
- regular = False
- if regular:
- surface = regular_surface()
- else:
- surface = alpha_surface()
- surface.fill((0, 0, 0, 255))
- display_information(surface)
- display = pygame.display.set_mode((500, 500))
- clock = pygame.Clock()
- running = True
- angle = 0
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- display.fill("grey")
- rotated_surface = pygame.transform.rotate(surface, angle)
- rect = rotated_surface.get_rect(center=(250, 250))
- display.blit(rotated_surface, rect)
- pygame.draw.rect(display, "white", rect, 1)
- pygame.display.flip()
- clock.tick(60)
- angle += 1
- if __name__ == '__main__':
- main()
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement