Skip to content

Commit

Permalink
Minor simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Jan 4, 2025
1 parent f3cf209 commit 57777b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
13 changes: 3 additions & 10 deletions rsass/src/value/colors/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,20 +480,13 @@ fn write_rgba(
let r = r.format(format);
let g = g.format(format);
let b = b.format(format);
let sep = if format.is_compressed() { "," } else { ", " };
if rgba.is_opaque() {
if format.is_compressed() {
write!(out, "rgb({r},{g},{b})")
} else {
write!(out, "rgb({r}, {g}, {b})")
}
write!(out, "rgb({r}{sep}{g}{sep}{b})")
} else {
let a = Number::from(rgba.alpha);
let a = a.format(format);
if format.is_compressed() {
write!(out, "rgba({r},{g},{b},{a})")
} else {
write!(out, "rgba({r}, {g}, {b}, {a})")
}
write!(out, "rgba({r}{sep}{g}{sep}{b}{sep}{a})")
}
}

Expand Down
33 changes: 18 additions & 15 deletions rsass/src/value/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,39 @@ impl Neg for &Numeric {
impl Div for &Numeric {
type Output = Numeric;
fn div(self, rhs: Self) -> Self::Output {
let value = &self.value / &rhs.value;
let mut unit = &self.unit / &rhs.unit;
let scale = unit.simplify();
Numeric {
value: value * scale,
unit,
value: &self.value / &rhs.value,
unit: &self.unit / &rhs.unit,
}
.simplify()
}
}
impl Numeric {
fn simplify(mut self) -> Self {
let scale = self.unit.simplify();
self.value = (f64::from(self.value) * scale).into();
self
}
}

impl Mul for &Numeric {
type Output = Numeric;
fn mul(self, rhs: Self) -> Self::Output {
let value = &self.value * &rhs.value;
let mut unit = &self.unit * &rhs.unit;
let scale = unit.simplify();
Numeric {
value: value * scale,
unit,
value: &self.value * &rhs.value,
unit: &self.unit * &rhs.unit,
}
.simplify()
}
}

impl Display for Formatted<'_, Numeric> {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
let mut unit = self.value.unit.clone();
let value = &self.value.value * &unit.simplify();
value.format(self.format).fmt(out)?;
if !value.is_finite() && unit.is_pos() {
let t = self.value.clone().simplify();
t.value.format(self.format).fmt(out)?;
if !t.value.is_finite() && t.unit.is_pos() {
out.write_str(" * 1")?;
}
unit.fmt(out)
t.unit.fmt(out)
}
}
4 changes: 2 additions & 2 deletions rsass/src/value/unitset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CssDimension, Dimension, Number, Unit};
use super::{CssDimension, Dimension, Unit};
use std::fmt::{self, Display, Write};
use std::ops::{Div, Mul};

Expand Down Expand Up @@ -115,7 +115,7 @@ impl UnitSet {
}

/// Simplify this unit set, returning a scaling factor.
pub fn simplify(&mut self) -> Number {
pub fn simplify(&mut self) -> f64 {
let mut factor = 1.;
if self.units.len() > 1 {
for i in 1..(self.units.len()) {
Expand Down

0 comments on commit 57777b4

Please sign in to comment.