-
-
Notifications
You must be signed in to change notification settings - Fork 198
Add Color.__bytes__
#3547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add Color.__bytes__
#3547
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where docs?
I was thinking docs aren't necessary for this change, since it makes color behave like a normal sequence would. However I do think it would be a good idea to put a comment in the code explaining why it's necessary to have the method explicitly written out. |
I disagree. A |
A user-side implementation could look like: if pygame.version.vernum < (2, 5, 6):
color_bytes = bytes([color.r], [color.g], [color.b], [color.a])
else:
color_bytes = bytes(color) |
Yeah, a Also that snippet doesn't work, here are two ways to convert colors currently: bytes(list(color))
int(color).to_bytes(4) # over 2x faster |
Also I'm +1 for andrews suggestion for adding this in a |
Co-authored-by: Ankith <itsankith26@gmail.com>
…hods (as opposed to through a special struct slot)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a couple of comments, but I'm not so set on these that I can't be convinced otherwise if a good argument gets thrown at me
/** | ||
* While object.__bytes__(self) is listed in the Data Model reference (see: | ||
* https://docs.python.org/3/reference/datamodel.html#object.__bytes__) it | ||
* does not appear to have a PyTypeObject struct analog (see: | ||
* https://docs.python.org/3/c-api/typeobj.html), so we declare it for the | ||
* type as any other custom method. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this giant comment is strictly necessary in the middle of this large array definition
@@ -92,6 +92,11 @@ | |||
:returns: a newly created :class:`Color` object | |||
:rtype: Color | |||
|
|||
.. versionchanged:: 2.5.6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think versionadded
is more appropriate because versionchanged
indicates that we changed behavior on our end, instead of overriding default python behavior by adding a new method.
Fixes #3546