Ogeon / palette

A Rust library for linear color calculations and conversion
Apache License 2.0
749 stars 60 forks source link

Issue with Angle Conversion from `f32`,`f64` to `u8` #385

Closed aristotle9 closed 5 months ago

aristotle9 commented 5 months ago

Issue with Angle Conversion from f32,f64 to u8

https://github.com/Ogeon/palette/blob/c54efbd43c03267713da337bd72005c9d0390598/palette/src/angle.rs#L162-L174

How To Reproduce

use palette::RgbHue;

let h = RgbHue::new(128_u8);
println!("{:?}", h);
let h = h.into_format::<f32>();
println!("{:?}", h);
assert_eq!(h, RgbHue::new(180_f32));

println!("====");

let h = RgbHue::new(180_f32);
println!("{:?}", h);
let h = h.into_format::<u8>();
println!("{:?}", h);
assert_eq!(h, RgbHue::new(128_u8));

Expected Outcome

RgbHue(128)
RgbHue(180.0)
====
RgbHue(180.0)
RgbHue(128)

Actual Outcome

RgbHue(128)
RgbHue(180.0)
====
RgbHue(180.0)
RgbHue(1)
thread 'main' panicked at examples/color.rs:16:5:
assertion `left == right` failed
  left: RgbHue(1)
 right: RgbHue(128)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Additional Details

Ogeon commented 5 months ago

Ah, it seems to be missing a multiplication by 255. 😬 Thanks for reporting.