Skip to content
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

Add 6 character only as_hex e.g. Color("red").as_hex(format="long") == #ff0000 #66

Closed
3 of 13 tasks
DJRHails opened this issue Jun 21, 2023 · 5 comments · Fixed by #93
Closed
3 of 13 tasks

Add 6 character only as_hex e.g. Color("red").as_hex(format="long") == #ff0000 #66

DJRHails opened this issue Jun 21, 2023 · 5 comments · Fixed by #93

Comments

@DJRHails
Copy link
Contributor

DJRHails commented Jun 21, 2023

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Currently I use a custom function for converting Pydantic Color to hex, to remove the unexpected return value of #f00 when I expect #ff0000 instead.

def as_hex(color: Color) -> str:
    rgb_hex = ''.join(f'{v:02x}' for v in data.signature.brand_color.as_rgb_tuple())
    return f"#{rgb_hex}"

I propose (and happy to contribute), an optional parameter e.g. format="long" to allow non-compressed hex values.

Affected Components

@hramezani
Copy link
Member

Thanks @DJRHails for this feature request.

As you may know, We are working on Pydantic V2, and Color type is deprecated in Pydantic V2.

We moved this and some other extra types to a separate package pydantic-extra-types.

I am moving this issue there.

@hramezani hramezani transferred this issue from pydantic/pydantic Jun 21, 2023
@DJRHails
Copy link
Contributor Author

Appreciated @hramezani!

@DJRHails DJRHails reopened this Jun 21, 2023
@Kludex
Copy link
Member

Kludex commented Aug 25, 2023

PR welcome. 👍

The signature should be def as_hex(self, format: Literal['short', 'long'] = 'short') for backwards compatibility.

@DJRHails
Copy link
Contributor Author

Great I'll get a PR in tonight

@DJRHails
Copy link
Contributor Author

@Kludex - PR #93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants