Commit 3dad4c3 1 parent 0b90da4 commit 3dad4c3 Copy full SHA for 3dad4c3
File tree 1 file changed +5
-10
lines changed
1 file changed +5
-10
lines changed Original file line number Diff line number Diff line change @@ -389,25 +389,20 @@ impl<T: Primitive> FromPrimitive<T> for T {
389
389
// NaN however always maps to NaN therefore we have to force it towards some value.
390
390
// 1.0 (white) was picked as firefox and chrome choose to map NaN to that.
391
391
#[ inline]
392
- fn sanitize_nan ( i : f32 ) -> f32 {
393
- if i. is_nan ( ) {
394
- 1.0
395
- } else {
396
- i
397
- }
392
+ fn normalize_float ( float : f32 , max : f32 ) -> f32 {
393
+ let clamped = if !( float < 1.0 ) { 1.0 } else { float. max ( 0.0 ) } ;
394
+ ( clamped * max) . round ( )
398
395
}
399
396
400
397
impl FromPrimitive < f32 > for u8 {
401
398
fn from_primitive ( float : f32 ) -> Self {
402
- let inner = ( sanitize_nan ( float) . clamp ( 0.0 , 1.0 ) * u8:: MAX as f32 ) . round ( ) ;
403
- NumCast :: from ( inner) . unwrap ( )
399
+ NumCast :: from ( normalize_float ( float, u8:: MAX as f32 ) ) . unwrap ( )
404
400
}
405
401
}
406
402
407
403
impl FromPrimitive < f32 > for u16 {
408
404
fn from_primitive ( float : f32 ) -> Self {
409
- let inner = ( sanitize_nan ( float) . clamp ( 0.0 , 1.0 ) * u16:: MAX as f32 ) . round ( ) ;
410
- NumCast :: from ( inner) . unwrap ( )
405
+ NumCast :: from ( normalize_float ( float, u16:: MAX as f32 ) ) . unwrap ( )
411
406
}
412
407
}
413
408
You can’t perform that action at this time.
0 commit comments