Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for transparency #162

Merged
merged 11 commits into from
May 26, 2022
Prev Previous commit
Next Next commit
remove const generic
superhawk610 committed May 15, 2022
commit abd0b3f63338bdb7b59061abedd2424ce2a3adab
23 changes: 12 additions & 11 deletions src/helper.rs
Original file line number Diff line number Diff line change
@@ -61,19 +61,20 @@ pub fn interpolate_angle(a: Scalar, b: Scalar, fraction: Fraction) -> Scalar {
// MaxPrecision::<3>::wrap(0.5004) //=> 0.500
// MaxPrecision::<3>::wrap(0.5005) //=> 0.501
//
pub struct MaxPrecision<const N: u32> {
pub struct MaxPrecision {
precision: u32,
inner: f64,
}

impl<const N: u32> MaxPrecision<N> {
pub fn wrap(inner: f64) -> Self {
Self { inner }
impl MaxPrecision {
pub fn wrap(precision: u32, inner: f64) -> Self {
Self { precision, inner }
}
}

impl<const N: u32> Display for MaxPrecision<N> {
impl Display for MaxPrecision {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let pow_10 = 10u32.pow(N) as f64;
let pow_10 = 10u32.pow(self.precision) as f64;
let rounded = (self.inner * pow_10).round() / pow_10;
write!(f, "{}", rounded)
}
@@ -97,9 +98,9 @@ fn test_interpolate_angle() {

#[test]
fn test_max_precision() {
assert_eq!(format!("{}", MaxPrecision::<3>::wrap(0.5)), "0.5");
assert_eq!(format!("{}", MaxPrecision::<3>::wrap(0.51)), "0.51");
assert_eq!(format!("{}", MaxPrecision::<3>::wrap(0.512)), "0.512");
assert_eq!(format!("{}", MaxPrecision::<3>::wrap(0.5124)), "0.512");
assert_eq!(format!("{}", MaxPrecision::<3>::wrap(0.5125)), "0.513");
assert_eq!(format!("{}", MaxPrecision::wrap(3, 0.5)), "0.5");
assert_eq!(format!("{}", MaxPrecision::wrap(3, 0.51)), "0.51");
assert_eq!(format!("{}", MaxPrecision::wrap(3, 0.512)), "0.512");
assert_eq!(format!("{}", MaxPrecision::wrap(3, 0.5124)), "0.512");
assert_eq!(format!("{}", MaxPrecision::wrap(3, 0.5125)), "0.513");
}
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ impl Color {
"a",
format!(
",{space}{alpha}",
alpha = MaxPrecision::<3>::wrap(self.alpha),
alpha = MaxPrecision::wrap(3, self.alpha),
space = space
),
)
@@ -186,7 +186,7 @@ impl Color {
"a",
format!(
",{space}{alpha}",
alpha = MaxPrecision::<3>::wrap(rgba.alpha),
alpha = MaxPrecision::wrap(3, rgba.alpha),
space = space
),
)
@@ -233,7 +233,7 @@ impl Color {
"a",
format!(
",{space}{alpha}",
alpha = MaxPrecision::<3>::wrap(rgba.alpha),
alpha = MaxPrecision::wrap(3, rgba.alpha),
space = space
),
)
@@ -319,7 +319,7 @@ impl Color {
} else {
format!(
",{space}{alpha}",
alpha = MaxPrecision::<3>::wrap(self.alpha),
alpha = MaxPrecision::wrap(3, self.alpha),
space = space
)
}
@@ -349,7 +349,7 @@ impl Color {
} else {
format!(
",{space}{alpha}",
alpha = MaxPrecision::<3>::wrap(self.alpha),
alpha = MaxPrecision::wrap(3, self.alpha),
space = space
)
}