Skip to content
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

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

Closed
aristotle9 opened this issue Mar 19, 2024 · 1 comment · Fixed by #386
Closed

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

aristotle9 opened this issue Mar 19, 2024 · 1 comment · Fixed by #386
Labels
defect Something that isn't as or doesn't work as intended

Comments

@aristotle9
Copy link

Issue with Angle Conversion from f32,f64 to u8

impl FromAngle<$float_ty> for u8 {
#[inline]
fn from_angle(angle: $float_ty) -> Self {
let normalized = angle.normalize_unsigned_angle() / $float_ty::full_rotation();
let rounded = normalized.round();
if rounded > 255.5 {
0
} else {
rounded as u8
}
}
}

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

  • Cargo.lock
[[package]]
name = "palette"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebfc23a4b76642983d57e4ad00bb4504eb30a8ce3c70f4aee1f725610e36d97a"
dependencies = [
 "approx",
 "fast-srgb8",
 "palette_derive",
 "phf",
]
  • Rust version(s): rustc 1.75.0 (82e1608df 2023-12-21)
  • Target platform: aarch64-apple-darwin
@aristotle9 aristotle9 added the defect Something that isn't as or doesn't work as intended label Mar 19, 2024
@Ogeon
Copy link
Owner

Ogeon commented Mar 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Something that isn't as or doesn't work as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants