-
Hi everyone, I'm new to The data I'm trying to manipulate is in the form of an unsigned int. Ex: What I want to do is parse this value as a color I can manipulate with Here's the code I wrote : let raw_value: i32 = -7589836;
let color = Srgba::from_u32::<Rgba>(raw_value as u32);
println!("{:?}", color); //Alpha { color: Rgb { red: 255, green: 140, blue: 48, standard: PhantomData }, alpha: 52 }
let hsla_color: Hsla = color.into_color(); I get the following errors :
I have tried multiple variations of the following code, including trying to call the various into_* methods (without success), importing all the traits the doc would refer to, ... I also tried reading Thanks in advance for any help you can provide, Nic0w |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi, you seem to be hitting an unfortunate case with the error messages, but the solution should be relatively simple. Let me start by annotating your example with bit more explicit types to know what we are talking about: let raw_value: i32 = -7589836;
let color = Srgba::from_u32::<Rgba>(raw_value as u32); // This is Srgba<u8> which is Alpha<Rgb<Srgb, u8>, u8>
println!("{:?}", color); //Alpha { color: Rgb { red: 255, green: 140, blue: 48, standard: PhantomData }, alpha: 52 }
let hsla_color: Hsla = color.into_color(); // Hsla defaults to Hsla<Srgb, f32>, which is Alpha<Hsl<Srgb, f32>, f32> Rust has this quirk where if you have a type with default type parameters, the defaults become strict requirements when used in a impl<C1: WithAlpha<T>, C2, T: Component> FromColorUnclamped<C1> for Alpha<C2, T> where
C1::Color: IntoColorUnclamped<C2>, It then proceeds to tell you how to fulfill But for your case, all you need to do is to convert your RGBA color to let raw_value: i32 = -7589836;
let color = Srgba::from_u32::<Rgba>(raw_value as u32);
println!("{:?}", color); //Alpha { color: Rgb { red: 255, green: 140, blue: 48, standard: PhantomData }, alpha: 52 }
let hsla_color: Hsla = color.into_format::<f32>().into_color(); // <-- notice the added into_format for changing the component type The extra Also, I hope this won't scare you away. The conversion code may be weird by necessity, since there are so many combinations, but the errors are usually much more straight forward. I'll make an issue for trying to improve the situation and see if the documentation can be improved to show a case like this. |
Beta Was this translation helpful? Give feedback.
Hi, you seem to be hitting an unfortunate case with the error messages, but the solution should be relatively simple. Let me start by annotating your example with bit more explicit types to know what we are talking about:
Rust has this quirk where if you have a type with default type parameters, the defaults bec…