-
Notifications
You must be signed in to change notification settings - Fork 60
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
Function to parse both hex and named colours from a string #306
Comments
Hi, I see your point and I definitely don't want it to be unnecessarily difficult. The problem is that there are so many ways of representing RGB as a string (CSS syntax, for example) so it's always going to be lacking something. I prefer to provide the parts separately and let the library user compose them as they see fit. That said, do you think an explicit and more visible |
Maybe. Also searching for "parse" on docs.rs should return something relevant.
Maybe the docs can point and some external crates (compatible with |
First, sorry for coming off as negative. It's good that you highlighted that it's inconvenient and hard to find everything that's available. Thank you! The reason why I prefer to keep them separate is that I'm generally trying to move away from options that look like "one size fits all". They tend to end up becoming either lackluster or way too complex. Or both.
Definitely, I think having it as its own method will make cross referencing and attaching aliases more natural. And the named colors are really old code that hasn't yet been updated for better discoverability with newer rustdoc features.
That could be nice, especially if there are some well established options. 🤔 I think people tend to make them quite generic so basically anything should work with some light glue code. Most crates I have seen can communicate in |
Having more dedicated functions like |
That could also be helpful! The combinations are too many (potentially endless) to have exactly all of them but the most common cases can of course still be covered. 🙂 It can also be useful as a bit of a hint towards what's the more preferred or optimal option. For example, it's going to be possible to convert RGB straight from encoded u8 to linear f32 and it's faster than performing each step separately. |
Hi, This my first time interacting with this crate's community and I'm working on a app with two frontends and I use palette for my colors. This is currently how I convert my hex colors into palette's fn hex_to_color(hex: &str) -> Option<Rgb> {
if hex.len() == 7 {
let hash = &hex[0..1];
let red = u8::from_str_radix(&hex[1..3], 16);
let green = u8::from_str_radix(&hex[3..5], 16);
let blue = u8::from_str_radix(&hex[5..7], 16);
return match (hash, red, green, blue) {
("#", Ok(red), Ok(green), Ok(blue)) => Some(Rgb::new(
red as f32 / 255.0,
green as f32 / 255.0,
blue as f32 / 255.0,
)),
_ => None,
};
}
None
} At least hex colors should be natively parsed by the library. What's the state of this issue? |
Hi! You can already parse hex colors via the let example1: Srgb<u8> = hex.parse().ok()?; // .ok() throws the error away and returns Option<Srgb<u8>>
let example2 = Srgb::<u8>::from_str(hex).ok()?; Keep in mind that this doesn't require the I'm keeping this issue open as a reminder to consider adding an explicit |
As a first step toward making it more discoverable, I recently added more visible examples of parsing hex values in the documentation. It's not yet released, so here's a preview: https://ogeon.github.io/palette/palette/rgb/struct.Rgb.html |
Description
There is
named_from_str
for looking up pre-defined colours by names. There is also a hard to findFromStr
implementation for parsing hex colours.I think there should be some easy function that does both - tries to decode a colour by name, then as hex colour and fails only if neither succeeds. This should allow for easier user-friendly colour specification in CLI or config files.
The text was updated successfully, but these errors were encountered: