1
0
Fork 0
forked from wry/wry

tree: update to latest version of wp_fractional_scale

This commit is contained in:
Julian Orth 2022-11-12 15:05:58 +01:00
parent e61d6ab074
commit 5b2eb5855a
22 changed files with 104 additions and 54 deletions

44
src/scale.rs Normal file
View file

@ -0,0 +1,44 @@
use std::fmt::{Debug, Display, Formatter};
const BASE: u32 = 120;
const BASEF: f64 = BASE as f64;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Scale(pub u32);
impl Scale {
pub fn from_int(f: u32) -> Self {
Self(f.saturating_mul(BASE))
}
pub fn from_f64(f: f64) -> Self {
Self((f * BASEF).round() as u32)
}
pub fn to_f64(self) -> f64 {
self.0 as f64 / BASEF
}
pub fn round_up(self) -> u32 {
self.0.saturating_add(BASE - 1) / BASE
}
}
impl PartialEq<u32> for Scale {
fn eq(&self, other: &u32) -> bool {
self.0 == other * BASE
}
}
impl Debug for Scale {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.to_f64(), f)
}
}
impl Display for Scale {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.to_f64(), f)
}
}