-
-
Notifications
You must be signed in to change notification settings - Fork 198
Description
The below is written by @Matiiss, @Starbuck5 helped with opening the ticket
import pygame
c = pygame.Color(1, 2, 3)
print(bytes(c))
Does not exactly produce the output one might expect, Python treats the Color
as an integer object (it is sort of) and creates a bytes
object of that length filled with 0s, more explanation here: https://docs.python.org/3/library/stdtypes.html#bytes
Full conversation on discord: https://discord.com/channels/772505616680878080/772940667231928360/1401287406635843784
Copied here for persistence
Big Whoop — Yesterday at 22:35
hey whats up with this:
from pygame import Color
c = Color(1, 2, 3)
b = bytes(c)
b
?
Matiiss — Yesterday at 23:05
curiously the length of those bytes translates to the color values
from itertools import batched
from pygame import Color
c = Color(5, 2, 3, 7)
b = bytes(c)
length = len(b)
# 0b...
length_in_binary = bin(length)
# 0b... -> [2:] -> ...
length_in_binary_no_prefix = length_in_binary[2:]
length_in_binary_no_prefix_padded = length_in_binary_no_prefix.zfill(32)
print(c)
print(length)
print(length_in_binary)
print(length_in_binary_no_prefix_padded)
print(["".join(batch) for batch in batched(length_in_binary_no_prefix_padded, 8)])
print([int("".join(batch), base=2) for batch in batched(length_in_binary_no_prefix_padded, 8)])
pygame-ce 2.5.3 (SDL 2.30.12, Python 3.12.8)
Color(5, 2, 3, 7)
84017927
0b101000000100000001100000111
00000101000000100000001100000111
['00000101', '00000010', '00000011', '00000111']
[5, 2, 3, 7]
NotMEE12 — Yesterday at 23:15
length of the bytes has the data you need...
Okay but why not let the data be in bytes?
Starbuck — Yesterday at 23:19
Holy shit
Big Whoop — Yesterday at 23:24
yeah fun times
Andrew — Yesterday at 23:58
Output? I’m on mobile right now lol
Starbuck — Yesterday at 23:59
16909311 bytes of 0
Andrew — Yesterday at 23:59
Wtf
Matiiss — 00:05
I think it's because of int
we don't implement a __bytes__
, so it might be defaulting to __int__
???
https://docs.python.org/3/library/stdtypes.html#bytes
yikes

Starbuck — 00:11
Yeah you're right
>>> int(c)
16909311
>>> c.__index__()
16909311