From 42ed86cdfdd513a1a1b1b26f7166c1df055ac960 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Tue, 14 Nov 2023 10:16:22 -0800 Subject: [PATCH] Lerp/PercentBetween fixes Asserting condition on PercentBetween, Color lerping now works correctly according to testing with gray shades, but due to rounding errors, no unit test is being checked in at the moment. --- src/animation.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/animation.rs b/src/animation.rs index 7a55f08..30cc1c0 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -779,6 +779,8 @@ macro_rules! impl_percent_between { ($type:ident, $float:ident) => { impl PercentBetween for $type { fn percent_between(&self, min: &Self, max: &Self) -> ZeroToOne { + assert!(min <= max, "percent_between requires min <= max"); + let range = *max - *min; ZeroToOne::from(*self as $float / range as $float) } @@ -812,10 +814,13 @@ impl PercentBetween for Color { func(value).percent_between(&func(min), &func(max)) } - channel_percent(*self, *min, *max, Color::red) - * channel_percent(*self, *min, *max, Color::green) - * channel_percent(*self, *min, *max, Color::blue) - * channel_percent(*self, *min, *max, Color::alpha) + ZeroToOne::new( + (*channel_percent(*self, *min, *max, Color::red) + + *channel_percent(*self, *min, *max, Color::green) + + *channel_percent(*self, *min, *max, Color::blue) + + *channel_percent(*self, *min, *max, Color::alpha)) + / 4., + ) } }