-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Comments
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. |
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. |
There is actually a crate that handles all CSS color formats, as part of the servo project: cssparser-color. So maybe 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. |
<!-- 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>.
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.
The text was updated successfully, but these errors were encountered: