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.
This commit is contained in:
Jonathan Johnson 2023-11-14 10:16:22 -08:00
parent 4c7c3be5ba
commit 42ed86cdfd
No known key found for this signature in database
GPG key ID: A66D6A34D6620579

View file

@ -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.,
)
}
}