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

Hex string to Color32, Color 32 to hex string #3466

Closed
sdasda7777 opened this issue Oct 13, 2023 · 3 comments · Fixed by #3570
Closed

Hex string to Color32, Color 32 to hex string #3466

sdasda7777 opened this issue Oct 13, 2023 · 3 comments · Fixed by #3570

Comments

@sdasda7777
Copy link

Is your feature request related to a problem? Please describe.
I have strings in format "#aabbcc" that I would like to convert to Color32, but there apparently isn't a good way to do this. I think it would be useful to provide methods for conversion to and from some similar string format (perhaps without the "#").

Describe the solution you'd like
I would like to see Color32 implement some method that would allow for parsing of these hex strings, since it is very much a common format and I do not believe I should be manually writing a parser for hex strings when I want to create a UI.

@YgorSouza
Copy link
Contributor

Converting hex strings to colors is already possible via the hex_color macro, with the caveat that it only works with literal strings, and you have to add the color-hex crate as an explicit dependency due to a bug in the macro, reported in #2644.

Writing your own parsing function is relatively easy by leveraging the u32 parser:

fn color_from_hex(hex: &str) -> Option<Color32> {
    let hex = hex.trim_start_matches('#');
    let alpha = match hex.len() {
        6 => false,
        8 => true,
        _ => None?,
    };
    u32::from_str_radix(hex, 16)
        .ok()
        .map(|u| if alpha { u } else { u << 8 | 0xff })
        .map(u32::to_be_bytes)
        .map(|[r, g, b, a]| Color32::from_rgba_unmultiplied(r, g, b, a))
}

Formatting a color as a hex string is even easier:

format!("{:08x}", u32::from_be_bytes(color.to_srgba_unmultiplied()));

But I suppose egui could provide these methods if there is no ambiguity over what they are supposed to do. This also ties into #3284, so it will probably have to be implemented at some point anyway.

@sdasda7777
Copy link
Author

Hi, thanks for reply. I am aware of the macro, unfortunately I am working with strings and not literals, so it's not of much use to me. I know the function is not too complicated to write, but I would imagine this is something lot of people use in some capacity, so I think it would be for the best to have it implemented correctly.

@YgorSouza
Copy link
Contributor

YgorSouza commented Nov 18, 2023

There is actually a crate that handles all CSS color formats, as part of the servo project: cssparser-color. So maybe ecolor could just add it as an optional dependency and impl From<cssparser_color::Color> for Color32 when it is enabled. I'll try to put together a PR to see how it goes.

Edit: actually, the cssparser crate doesn't convert to hex string anyway, so this has to be implemented separately. I decided to make a PR with only the hex part without any new dependencies, and the rest of the CSS could be a separate issue if anyone is interested.

YgorSouza added a commit to YgorSouza/egui that referenced this issue Nov 18, 2023
YgorSouza added a commit to YgorSouza/egui that referenced this issue Jan 6, 2024
emilk pushed a commit that referenced this issue Jan 7, 2024
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

Closes <#3466>.
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.

2 participants