use more float literals

This commit is contained in:
Connor Skees 2022-12-27 15:42:54 -05:00
parent e25a9f7b12
commit 012cdbf7cc
7 changed files with 65 additions and 73 deletions

View File

@ -61,8 +61,8 @@ fn hsl_3_args(
Ok(Value::Color(Color::from_hsla_fn(
Number(hue.num().rem_euclid(360.0)),
saturation.num() / Number::from(100),
lightness.num() / Number::from(100),
saturation.num() / Number(100.0),
lightness.num() / Number(100.0),
Number(alpha),
)))
}
@ -223,7 +223,7 @@ fn darken(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value>
num: n,
unit: u,
as_slash: _,
}) => bound!(args, "amount", n, u, 0, 100) / Number::from(100),
}) => bound!(args, "amount", n, u, 0, 100) / Number(100.0),
v => {
return Err((
format!(
@ -260,7 +260,7 @@ fn saturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value
num: n,
unit: u,
as_slash: _,
}) => bound!(args, "amount", n, u, 0, 100) / Number::from(100),
}) => bound!(args, "amount", n, u, 0, 100) / Number(100.0),
v => {
return Err((
format!(
@ -314,7 +314,7 @@ fn desaturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Val
num: n,
unit: u,
as_slash: _,
}) => bound!(args, "amount", n, u, 0, 100) / Number::from(100),
}) => bound!(args, "amount", n, u, 0, 100) / Number(100.0),
v => {
return Err((
format!(
@ -384,7 +384,7 @@ pub(crate) fn invert(mut args: ArgumentResult, visitor: &mut Visitor) -> SassRes
as_slash: _,
}),
..
}) => Some(bound!(args, "weight", n, u, 0, 100) / Number::from(100)),
}) => Some(bound!(args, "weight", n, u, 0, 100) / Number(100.0)),
None => None,
Some(v) => {
return Err((

View File

@ -17,7 +17,7 @@ pub(crate) fn blackness(mut args: ArgumentResult, visitor: &mut Visitor) -> Sass
};
let blackness =
Number::from(1) - (color.red().max(color.green()).max(color.blue()) / Number::from(255));
Number(1.0) - (color.red().max(color.green()).max(color.blue()) / Number(255.0));
Ok(Value::Dimension(SassNumber {
num: (blackness * 100),
@ -33,7 +33,7 @@ pub(crate) fn whiteness(mut args: ArgumentResult, visitor: &mut Visitor) -> Sass
.get_err(0, "color")?
.assert_color_with_name("color", args.span())?;
let whiteness = color.red().min(color.green()).min(color.blue()) / Number::from(255);
let whiteness = color.red().min(color.green()).min(color.blue()) / Number(255.0);
Ok(Value::Dimension(SassNumber {
num: (whiteness * 100),
@ -77,7 +77,7 @@ fn hwb_inner(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Valu
num: n,
unit: Unit::Percent,
..
}) => n / Number::from(100),
}) => n / Number(100.0),
Value::Dimension(SassNumber { num: n, .. }) => n,
v => {
return Err((

View File

@ -25,7 +25,7 @@ macro_rules! opt_hsl {
Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(),
Value::Dimension(SassNumber {
num: n, unit: u, ..
}) => Some(bound!($args, $arg, n, u, $low, $high) / Number::from(100)),
}) => Some(bound!($args, $arg, n, u, $low, $high) / Number(100.0)),
Value::Null => None,
v => {
return Err((
@ -197,7 +197,7 @@ pub(crate) fn scale_color(mut args: ArgumentResult, visitor: &mut Visitor) -> Sa
num: n,
unit: Unit::Percent,
..
}) => Some(bound!($args, $arg, n, Unit::Percent, $low, $high) / Number::from(100)),
}) => Some(bound!($args, $arg, n, Unit::Percent, $low, $high) / Number(100.0)),
v @ Value::Dimension { .. } => {
return Err((
format!(
@ -228,20 +228,16 @@ pub(crate) fn scale_color(mut args: ArgumentResult, visitor: &mut Visitor) -> Sa
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Color::from_rgba(
scale(
color.red(),
red.unwrap_or_else(Number::zero),
Number::from(255),
),
scale(color.red(), red.unwrap_or_else(Number::zero), Number(255.0)),
scale(
color.green(),
green.unwrap_or_else(Number::zero),
Number::from(255),
Number(255.0),
),
scale(
color.blue(),
blue.unwrap_or_else(Number::zero),
Number::from(255),
Number(255.0),
),
scale(
color.alpha(),
@ -258,7 +254,7 @@ pub(crate) fn scale_color(mut args: ArgumentResult, visitor: &mut Visitor) -> Sa
// Color::as_hsla() returns more exact values than Color::hue(), etc.
let (this_hue, this_saturation, this_luminance, this_alpha) = color.as_hsla();
return Ok(Value::Color(Color::from_hsla(
scale(this_hue, Number::zero(), Number::from(360)),
scale(this_hue, Number::zero(), Number(360.0)),
scale(
this_saturation,
saturation.unwrap_or_else(Number::zero),

View File

@ -428,7 +428,7 @@ pub(crate) fn mix(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult
2,
"weight",
Value::Dimension(SassNumber {
num: (Number::from(50)),
num: (Number(50.0)),
unit: Unit::None,
as_slash: None,
}),
@ -438,7 +438,7 @@ pub(crate) fn mix(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult
num: n,
unit: u,
as_slash: _,
}) => bound!(args, "weight", n, u, 0, 100) / Number::from(100),
}) => bound!(args, "weight", n, u, 0, 100) / Number(100.0),
v => {
return Err((
format!(

View File

@ -408,7 +408,7 @@ fn acos(mut args: ArgumentResult, _: &mut Visitor) -> SassResult<Value> {
unit: Unit::None,
..
}) => Value::Dimension(SassNumber {
num: if num > Number::from(1) || num < Number::from(-1) {
num: if num > Number(1.0) || num < Number(-1.0) {
Number(f64::NAN)
} else if num.is_one() {
Number::zero()
@ -448,7 +448,7 @@ fn asin(mut args: ArgumentResult, _: &mut Visitor) -> SassResult<Value> {
unit: Unit::None,
..
}) => {
if num > Number::from(1) || num < Number::from(-1) {
if num > Number(1.0) || num < Number(-1.0) {
return Ok(Value::Dimension(SassNumber {
num: Number(f64::NAN),
unit: Unit::Deg,
@ -629,7 +629,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"e",
Value::Dimension(SassNumber {
num: Number::from(std::f64::consts::E),
num: Number(std::f64::consts::E),
unit: Unit::None,
as_slash: None,
}),
@ -637,7 +637,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"pi",
Value::Dimension(SassNumber {
num: Number::from(std::f64::consts::PI),
num: Number(std::f64::consts::PI),
unit: Unit::None,
as_slash: None,
}),
@ -645,7 +645,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"epsilon",
Value::Dimension(SassNumber {
num: Number::from(std::f64::EPSILON),
num: Number(std::f64::EPSILON),
unit: Unit::None,
as_slash: None,
}),
@ -653,7 +653,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"max-safe-integer",
Value::Dimension(SassNumber {
num: Number::from(9007199254740991.0),
num: Number(9007199254740991.0),
unit: Unit::None,
as_slash: None,
}),
@ -661,7 +661,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"min-safe-integer",
Value::Dimension(SassNumber {
num: Number::from(-9007199254740991.0),
num: Number(-9007199254740991.0),
unit: Unit::None,
as_slash: None,
}),
@ -669,7 +669,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"max-number",
Value::Dimension(SassNumber {
num: Number::from(f64::MAX),
num: Number(f64::MAX),
unit: Unit::None,
as_slash: None,
}),
@ -677,7 +677,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin_var(
"min-number",
Value::Dimension(SassNumber {
num: Number::from(f64::MIN_POSITIVE),
num: Number(f64::MIN_POSITIVE),
unit: Unit::None,
as_slash: None,
}),

View File

@ -89,19 +89,15 @@ struct Rgb {
impl PartialEq for Rgb {
fn eq(&self, other: &Self) -> bool {
if self.red != other.red
&& !(self.red >= Number::from(255.0) && other.red >= Number::from(255.0))
{
if self.red != other.red && !(self.red >= Number(255.0) && other.red >= Number(255.0)) {
return false;
}
if self.green != other.green
&& !(self.green >= Number::from(255.0) && other.green >= Number::from(255.0))
&& !(self.green >= Number(255.0) && other.green >= Number(255.0))
{
return false;
}
if self.blue != other.blue
&& !(self.blue >= Number::from(255.0) && other.blue >= Number::from(255.0))
{
if self.blue != other.blue && !(self.blue >= Number(255.0) && other.blue >= Number(255.0)) {
return false;
}
true
@ -203,16 +199,16 @@ impl Color {
/// <https://github.com/sass/dart-sass/blob/0d0270cb12a9ac5cce73a4d0785fecb00735feee/lib/src/functions/color.dart#L718>
pub fn mix(self, other: &Color, weight: Number) -> Self {
let weight = weight.clamp(0.0, 100.0);
let normalized_weight = weight * Number::from(2.0) - Number::one();
let normalized_weight = weight * Number(2.0) - Number::one();
let alpha_distance = self.alpha() - other.alpha();
let combined_weight1 = if normalized_weight * alpha_distance == Number::from(-1) {
let combined_weight1 = if normalized_weight * alpha_distance == Number(-1.0) {
normalized_weight
} else {
(normalized_weight + alpha_distance)
/ (Number::one() + normalized_weight * alpha_distance)
};
let weight1 = (combined_weight1 + Number::one()) / Number::from(2.0);
let weight1 = (combined_weight1 + Number::one()) / Number(2.0);
let weight2 = Number::one() - weight1;
Color::from_rgba(
@ -233,9 +229,9 @@ impl Color {
return h.hue();
}
let red = self.red() / Number::from(255.0);
let green = self.green() / Number::from(255.0);
let blue = self.blue() / Number::from(255.0);
let red = self.red() / Number(255.0);
let green = self.green() / Number(255.0);
let blue = self.blue() / Number(255.0);
let min = red.min(green.min(blue));
let max = red.max(green.max(blue));
@ -245,25 +241,25 @@ impl Color {
let hue = if min == max {
Number::zero()
} else if max == red {
Number::from(60.0) * (green - blue) / delta
Number(60.0) * (green - blue) / delta
} else if max == green {
Number::from(120.0) + Number::from(60.0) * (blue - red) / delta
Number(120.0) + Number(60.0) * (blue - red) / delta
} else {
Number::from(240.0) + Number::from(60.0) * (red - green) / delta
Number(240.0) + Number(60.0) * (red - green) / delta
};
hue % Number::from(360.0)
hue % Number(360.0)
}
/// Calculate saturation from RGBA values
pub fn saturation(&self) -> Number {
if let Some(h) = &self.hsla {
return h.saturation() * Number::from(100.0);
return h.saturation() * Number(100.0);
}
let red: Number = self.red() / Number::from(255.0);
let green = self.green() / Number::from(255.0);
let blue = self.blue() / Number::from(255.0);
let red: Number = self.red() / Number(255.0);
let green = self.green() / Number(255.0);
let blue = self.blue() / Number(255.0);
let min = red.min(green.min(blue));
let max = red.max(green.max(blue));
@ -278,26 +274,26 @@ impl Color {
let s = delta
/ if sum > Number::one() {
Number::from(2.0) - sum
Number(2.0) - sum
} else {
sum
};
s * Number::from(100.0)
s * Number(100.0)
}
/// Calculate luminance from RGBA values
pub fn lightness(&self) -> Number {
if let Some(h) = &self.hsla {
return h.luminance() * Number::from(100.0);
return h.luminance() * Number(100.0);
}
let red: Number = self.red() / Number::from(255.0);
let green = self.green() / Number::from(255.0);
let blue = self.blue() / Number::from(255.0);
let red: Number = self.red() / Number(255.0);
let green = self.green() / Number(255.0);
let blue = self.blue() / Number(255.0);
let min = red.min(green.min(blue));
let max = red.max(green.max(blue));
(((min + max) / Number::from(2.0)) * Number::from(100.0)).round()
(((min + max) / Number(2.0)) * Number(100.0)).round()
}
pub fn as_hsla(&self) -> (Number, Number, Number, Number) {
@ -305,13 +301,13 @@ impl Color {
return (h.hue(), h.saturation(), h.luminance(), self.alpha());
}
let red = self.red() / Number::from(255.0);
let green = self.green() / Number::from(255.0);
let blue = self.blue() / Number::from(255.0);
let red = self.red() / Number(255.0);
let green = self.green() / Number(255.0);
let blue = self.blue() / Number(255.0);
let min = red.min(green.min(blue));
let max = red.max(green.max(blue));
let lightness = (min + max) / Number::from(2.0);
let lightness = (min + max) / Number(2.0);
let saturation = if min == max {
Number::zero()
@ -319,7 +315,7 @@ impl Color {
let d = max - min;
let mm = max + min;
d / if mm > Number::one() {
Number::from(2.0) - mm
Number(2.0) - mm
} else {
mm
}
@ -328,18 +324,18 @@ impl Color {
let mut hue = if min == max {
Number::zero()
} else if blue == max {
Number::from(4.0) + (red - green) / (max - min)
Number(4.0) + (red - green) / (max - min)
} else if green == max {
Number::from(2.0) + (blue - red) / (max - min)
Number(2.0) + (blue - red) / (max - min)
} else {
(green - blue) / (max - min)
};
if hue.is_negative() {
hue += Number::from(360.0);
hue += Number(360.0);
}
hue *= Number::from(60.0);
hue *= Number(60.0);
(hue % Number(360.0), saturation, lightness, self.alpha())
}
@ -422,9 +418,9 @@ impl Color {
return self.clone();
}
let red = Number::from(u8::max_value()) - self.red();
let green = Number::from(u8::max_value()) - self.green();
let blue = Number::from(u8::max_value()) - self.blue();
let red = Number(255.0) - self.red();
let green = Number(255.0) - self.green();
let blue = Number(255.0) - self.blue();
let inverse = Color::new_rgba(red, green, blue, self.alpha(), ColorFormat::Infer);
@ -434,7 +430,7 @@ impl Color {
pub fn complement(&self) -> Self {
let (hue, saturation, luminance, alpha) = self.as_hsla();
Color::from_hsla(hue + Number::from(180), saturation, luminance, alpha)
Color::from_hsla(hue + Number(180.0), saturation, luminance, alpha)
}
}
@ -442,7 +438,7 @@ impl Color {
impl Color {
pub fn alpha(&self) -> Number {
if self.alpha > Number::one() {
self.alpha / Number::from(255.0)
self.alpha / Number(255.0)
} else {
self.alpha
}

View File

@ -913,7 +913,7 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> {
Number::from(red),
Number::from(green),
Number::from(blue),
Number::from(alpha),
Number(alpha),
// todo:
// // Don't emit four- or eight-digit hex colors as hex, since that's not
// // yet well-supported in browsers.