Skip to content

Add Color.hex #3379

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

Merged
merged 5 commits into from
Apr 27, 2025
Merged

Add Color.hex #3379

merged 5 commits into from
Apr 27, 2025

Conversation

damusss
Copy link
Member

@damusss damusss commented Mar 11, 2025

Followed the New API routine for this property. FAQ below.

Why, if it can be done in one line?

That's not a reason not to add a shortcut. Other color shortcuts like Color.normalized can be achieved in one line aswell, plus, how to get the Color hex with one line is not very straight forward.

Can't you use the built in hex function?

That function wasn't made for correct representation of hex colors, so Color(hex(Color)) can easily error, for example in the following cases: hex(Color(1, 2, 3)) -> "0x10203ff" (no trailing zero; ValueError) or hex(Color(0, 0, 0, 255)) -> "'0xff" (not long enough; ValueError).

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame color pygame.color labels Mar 11, 2025
@damusss damusss requested a review from a team as a code owner March 11, 2025 14:55
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! 💯

As it is new API, requesting atleast 2 more approvals in addition to mine, having more eyes on this PR will be better.

Copy link
Member

@Matiiss Matiiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a couple comments, but nothing that would necessarily have to block this PR

@damusss
Copy link
Member Author

damusss commented Mar 24, 2025

all suggestions are good I'll fix them today

@ankith26
Copy link
Member

The latest commit broke formatting of a test file.

This little maneuver took 15 hours! on my Android
@aatle
Copy link
Contributor

aatle commented Apr 1, 2025

Checks seem to be failing because of a bug in package pyproject-metadata, see pypa/pyproject-metadata#239: fixed but affects version 0.9.1.
I think this issue can be fixed by requiring "pyproject-metadata!=0.9.1".

@Starbuck5
Copy link
Member

I don't really see this why should be added.

The FAQ in the PR description seems to assume this is a thing people want to do, but I can't think of why I would want to generate a hex string from a color object.

@aatle
Copy link
Contributor

aatle commented Apr 7, 2025

The primary reason is for displaying and inputting for user, not really for internal code. The hex rgb representation is the standard way to display, port, input colors; every color picker supports this format. Therefore it makes sense to add a shortcut.

If a pygame program wanted to allow the user to pick any color for something, this would be useful for both labelling and inputting the color.

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add a corresponding classmethod from_hex(), in accordance with every other settable color format. So that it's possible to use the setter in an expression.

@Matiiss
Copy link
Member

Matiiss commented Apr 7, 2025

You need to add a corresponding classmethod from_hex(), in accordance with every other settable color format. So that it's possible to use the setter in an expression.

Not necessary, it's already possible like so

pygame.Color("#hexstr")

@aatle
Copy link
Contributor

aatle commented Apr 7, 2025

I still think it could be beneficial to add it. It is explicit and could differentiate hex strings from color name strings (useful for input), plus more consistency.
Otherwise I think a note should be added, it's easy to miss the hex overload in the mass of constructor overloads.

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this, I suppose. Aatle's stated use case makes sense.

I don't see a need to add from_hex in this PR.

@Starbuck5
Copy link
Member

Also IDK what's up with the tests, but maybe if you rebase @damusss it would rerun and be green and pretty?

@Matiiss Matiiss mentioned this pull request Apr 27, 2025
@Matiiss Matiiss merged commit 32b0cb3 into pygame-community:main Apr 27, 2025
27 checks passed
@ankith26 ankith26 added this to the 2.5.4 milestone Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
color pygame.color New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants