Skip to content

Commit

Permalink
Merge pull request #386 from Ogeon/issue_385_angle_f32_to_u8
Browse files Browse the repository at this point in the history
Fix angle conversion from f32 to u8
  • Loading branch information
Ogeon authored Mar 31, 2024
2 parents bbd6d4b + 9ed7fef commit ccc521b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion palette/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ macro_rules! impl_from_angle_u8 {
#[inline]
fn from_angle(angle: $float_ty) -> Self {
let normalized = angle.normalize_unsigned_angle() / $float_ty::full_rotation();
let rounded = normalized.round();
let rounded = (normalized * 256.0).round();

if rounded > 255.5 {
0
Expand Down Expand Up @@ -201,3 +201,22 @@ impl UnsignedAngle for u8 {
self
}
}

#[cfg(test)]
mod test {
use crate::RgbHue;

#[test]
fn f32_to_u8() {
let hue_f32 = RgbHue::new(180.0f32);
let hue_u8 = hue_f32.into_format::<u8>();
assert_eq!(hue_u8, RgbHue::new(128u8));
}

#[test]
fn u8_to_f32() {
let hue_f32 = RgbHue::new(128u8);
let hue_u8 = hue_f32.into_format::<f32>();
assert_eq!(hue_u8, RgbHue::new(180.0f32));
}
}

0 comments on commit ccc521b

Please sign in to comment.