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
Strange conversion results #187
Comments
Hsl, Hsv, and Hwb are in Linear space in palette. This is similar to #160 and is probably unexpected behavior. The documentation lists it as "Linear HSL color space", but for users it should probably be gamma-corrected. The spaces themselves are ambiguous about their linearity. If you convert to LinSrgba with those spaces, it will produce the desired results. use palette::{Hsla, LinSrgba, Srgba};
fn main() {
let t1: Hsla = Hsla::new(0.0, 0.0, 0.03, 1.0);
println!("Srgba: {:?}", Srgba::from(t1));
println!("LinSrgba: {:?}", LinSrgba::from(t1));
} resulting in Srgba: Alpha { color: Rgb { red: 0.18974826, green: 0.18974826, blue: 0.18974826, standard: PhantomData }, alpha: 1.0 }
LinSrgba: Alpha { color: Rgb { red: 0.03, green: 0.03, blue: 0.03, standard: PhantomData }, alpha: 1.0 } |
Yes, exactly, this is the assumption in the current implementation of what I like to call "the RGB family". It will convert to linear RGB first, and then to sRGB, but you expected a direct conversion to sRGB. It should be possible to lift this restriction and make them all able to represent both linear and non-linear colors, just like with the For example let t1 : palette::Hsla = color::Hsla::new(0.0,0.0,0.03,1.0);
// Linear RGB -> plain (r, g, b, a) tuple -> sRGB
let srgb1 = palette::Srgba::from_components(palette::LinRgba::from(t1).into_components());
println!("{:?}", srgb1); |
@okaneco @Ogeon thank you so much for the replies. I believe this is unfortunate that it is not protected somehow on type level. So to better understand that, it means that if I write |
That's right. It will not match that tool with the current implementation if the tool doesn't convert via linear RGB. And it's because palette uses the wrong model. Or rather a model that's incomplete. It's locked to linear RGB. This will be fixed before the next release. It's an old mistake that hasn't been in focus until very recently. And it's named just |
I took some time to fix it today. This will work after #188 is merged and the next release is out: let output = Srgb::from_color(Hsl::new(0.0, 0.0, 0.3));
let expected = Srgb::new(0.3, 0.3, 0.3);
assert_eq!(output, expected); // or assert_relative_eq in case of float errors |
Hi consider this code:
The result we got is:
Which is of course wrong. It doesn't look like there is a space error, as
Hsla
is defined withSrgb
space. Moreover, palette is type safe, so I'm just cannot get what is wrong here. The correct result of convertinghsla(0,0,0.03,1.0)
is aroundrgba(0.03, 0.03, 0.03)
. What I'm doing wrong here?The text was updated successfully, but these errors were encountered: