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 Color::parse #2486

Merged
merged 4 commits into from
Sep 10, 2024
Merged

Add Color::parse #2486

merged 4 commits into from
Sep 10, 2024

Conversation

vladh
Copy link
Contributor

@vladh vladh commented Jun 30, 2024

This function has certain advantages over creating hex colours with color!:

  • It allows parsing of hex color strings
  • It allows opacity to be specified in hex, such as #ffffff00
  • It allows the shorthand #fff notation
  • It has better error handling; you can, for example, do color!(0xfafafa9) and there won't be an error, despite the hex string being invalid

If this PR gets merged, I'll send another patch to have color! use from_hex.

@vladh vladh force-pushed the add-color-from-hex branch 2 times, most recently from b8e57a6 to b49f0b5 Compare June 30, 2024 13:41
@VAWVAW
Copy link
Contributor

VAWVAW commented Jul 12, 2024

You missed the format #abcf in your documentation.

@VAWVAW
Copy link
Contributor

VAWVAW commented Jul 12, 2024

Is there a reason you don't use the more self-explaining #rgba, #rrggbb, ... for documentation?

@vladh
Copy link
Contributor Author

vladh commented Jul 12, 2024

@VAWVAW Good points, thank you! Fixed.

core/src/color.rs Outdated Show resolved Hide resolved
@vladh vladh force-pushed the add-color-from-hex branch 2 times, most recently from 1f30695 to ef179c7 Compare July 12, 2024 11:38
@n1ght-hunter
Copy link
Contributor

@vladh what the advantage of this over just using the color macro

($hex:expr) => {{

@vladh
Copy link
Contributor Author

vladh commented Jul 15, 2024

@n1ght-hunter Oops, I didn't know about color!, thank you. Maybe I should send a patch to better document color!.

That being said, I still think my PR has certain advantages:

  • It allows parsing of hex color strings
  • It allows opacity to be specified in hex, such as #ffffff00
  • It allows the shorthand #fff notation
  • It has better error handling; you can, for example, do color!(0xfafafa9) and there won't be an error, despite the hex string being invalid

If this PR gets merged, I'll send another patch to have color! use from_hex.

@nednoodlehead
Copy link

i would love this. Sort of annoying when I open a hex color editor, pick a color and I can't easily use it to color buttons and whatnot.

@vladh
Copy link
Contributor Author

vladh commented Aug 22, 2024

@hecrj Let me know if you want any changes on this (or just don’t want it which is fine).

@hecrj hecrj added this to the 0.13 milestone Sep 10, 2024
@hecrj hecrj added feature New feature or request addition labels Sep 10, 2024
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

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

Thanks!

I renamed the method to parse, which I believe conveys clearly that there is a runtime cost involved with this helper.

The color! macro should be preferred since it leverages hexadecimal literal notation and arithmetic directly. The compiler is able to optimize most of these operations away in most cases, so no parsing and runtime cost necessary.

I have also extended the macro to support short hand notation and added some debug assertions for good measure.

@hecrj hecrj changed the title Add Color::from_hex Add Color::parse Sep 10, 2024
@hecrj hecrj merged commit aed59ba into iced-rs:master Sep 10, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants