This commit is contained in:
ConnorSkees 2020-02-16 18:03:19 -05:00
parent f57da880b6
commit 87579b181b
7 changed files with 26 additions and 25 deletions

View File

@ -53,21 +53,21 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.hue(), Unit::Deg)), Value::Color(c) => Ok(Value::Dimension(c.hue(), Unit::Deg)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "saturation", |args, _| { decl!(f "saturation", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.saturation(), Unit::Percent)), Value::Color(c) => Ok(Value::Dimension(c.saturation(), Unit::Percent)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "lightness", |args, _| { decl!(f "lightness", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.lightness(), Unit::Percent)), Value::Color(c) => Ok(Value::Dimension(c.lightness(), Unit::Percent)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "adjust-hue", |args, _| { decl!(f "adjust-hue", |args, _| {
@ -157,8 +157,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Color(c.invert(weight))), Value::Color(c) => Ok(Value::Color(c.invert(weight))),
Value::Dimension(n, Unit::Percent) => Ok(Value::Ident(format!("invert({}%)", n), QuoteKind::None)), Value::Dimension(n, Unit::Percent) => Ok(Value::Ident(format!("invert({}%)", n), QuoteKind::None)),
Value::Dimension(..) => return Err("Only one argument may be passed to the plain-CSS invert() function.".into()), Value::Dimension(..) => Err("Only one argument may be passed to the plain-CSS invert() function.".into()),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
} }

View File

@ -11,7 +11,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "opacity", |args, _| { decl!(f "opacity", |args, _| {
@ -19,7 +19,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)),
Value::Dimension(num, unit) => Ok(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), Value::Dimension(num, unit) => Ok(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "opacify", |args, _| { decl!(f "opacify", |args, _| {

View File

@ -67,7 +67,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}); });
decl!(f "adjust-color", |args, _| { decl!(f "adjust-color", |args, _| {
let color = match arg!(args, 0, "color").eval() { let color = match arg!(args, 0, "color").eval() {
Value::Color(c) => c.clone(), Value::Color(c) => c,
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
@ -121,7 +121,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}); });
decl!(f "scale-color", |args, _| { decl!(f "scale-color", |args, _| {
let color = match arg!(args, 0, "color").eval() { let color = match arg!(args, 0, "color").eval() {
Value::Color(c) => c.clone(), Value::Color(c) => c,
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
@ -176,7 +176,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "ie-hex-str", |args, _| { decl!(f "ie-hex-str", |args, _| {
max_args!(args, 1); max_args!(args, 1);
let color = match arg!(args, 0, "color").eval() { let color = match arg!(args, 0, "color").eval() {
Value::Color(c) => c.clone(), Value::Color(c) => c,
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
Ok(Value::Ident(color.to_ie_hex_str(), QuoteKind::None)) Ok(Value::Ident(color.to_ie_hex_str(), QuoteKind::None))

View File

@ -158,21 +158,21 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.red(), Unit::None)), Value::Color(c) => Ok(Value::Dimension(c.red(), Unit::None)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "green", |args, _| { decl!(f "green", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.green(), Unit::None)), Value::Color(c) => Ok(Value::Dimension(c.green(), Unit::None)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "blue", |args, _| { decl!(f "blue", |args, _| {
max_args!(args, 1); max_args!(args, 1);
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.blue(), Unit::None)), Value::Color(c) => Ok(Value::Dimension(c.blue(), Unit::None)),
v => return Err(format!("$color: {} is not a color.", v).into()), v => Err(format!("$color: {} is not a color.", v).into()),
} }
}); });
decl!(f "mix", |args, _| { decl!(f "mix", |args, _| {
@ -191,6 +191,6 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
Value::Dimension(n, u) => bound!("weight", n, u, 0, 100) / Number::from(100), Value::Dimension(n, u) => bound!("weight", n, u, 0, 100) / Number::from(100),
v => return Err(format!("$weight: {} is not a number.", v).into()), v => return Err(format!("$weight: {} is not a number.", v).into()),
}; };
Ok(Value::Color(color1.mix(color2, weight))) Ok(Value::Color(color1.mix(&color2, weight)))
}); });
} }

View File

@ -67,7 +67,7 @@ struct Rgba {
} }
impl Rgba { impl Rgba {
pub fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self { pub const fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self {
Rgba { Rgba {
red, red,
green, green,
@ -90,7 +90,7 @@ struct Hsla {
} }
impl Hsla { impl Hsla {
pub fn new(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self { pub const fn new(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self {
Hsla { Hsla {
hue, hue,
saturation, saturation,
@ -172,7 +172,7 @@ impl Color {
/// Mix two colors together with weight /// Mix two colors together with weight
/// Algorithm adapted from /// Algorithm adapted from
/// <https://github.com/sass/dart-sass/blob/0d0270cb12a9ac5cce73a4d0785fecb00735feee/lib/src/functions/color.dart#L718> /// <https://github.com/sass/dart-sass/blob/0d0270cb12a9ac5cce73a4d0785fecb00735feee/lib/src/functions/color.dart#L718>
pub fn mix(self, other: Color, weight: Number) -> Self { pub fn mix(self, other: &Color, weight: Number) -> Self {
let weight = clamp!(weight, 0, 100); let weight = clamp!(weight, 0, 100);
let normalized_weight = weight.clone() * Number::from(2) - Number::from(1); let normalized_weight = weight.clone() * Number::from(2) - Number::from(1);
let alpha_distance = self.alpha() - other.alpha(); let alpha_distance = self.alpha() - other.alpha();
@ -432,7 +432,7 @@ impl Color {
let blue = Number::from(u8::max_value()) - self.blue(); let blue = Number::from(u8::max_value()) - self.blue();
let repr = repr(&red, &green, &blue, &self.alpha()); let repr = repr(&red, &green, &blue, &self.alpha());
let inverse = Color::new_rgba(red, green, blue, self.alpha(), repr); let inverse = Color::new_rgba(red, green, blue, self.alpha(), repr);
inverse.mix(self.clone(), weight) inverse.mix(self, weight)
} }
pub fn complement(&self) -> Self { pub fn complement(&self) -> Self {

View File

@ -46,7 +46,8 @@
clippy::cast_possible_truncation, clippy::cast_possible_truncation,
clippy::single_match_else, clippy::single_match_else,
clippy::indexing_slicing, clippy::indexing_slicing,
clippy::match_same_arms clippy::match_same_arms,
clippy::or_fun_call,
)] )]
#![cfg_attr(feature = "nightly", feature(track_caller))] #![cfg_attr(feature = "nightly", feature(track_caller))]
// todo! handle erroring on styles at the toplevel // todo! handle erroring on styles at the toplevel
@ -213,7 +214,7 @@ enum Expr {
/// A full selector `a > h1` /// A full selector `a > h1`
Selector(Selector), Selector(Selector),
/// A variable declaration `$var: 1px` /// A variable declaration `$var: 1px`
VariableDecl(String, Value), VariableDecl(String, Box<Value>),
/// A mixin declaration `@mixin foo {}` /// A mixin declaration `@mixin foo {}`
MixinDecl(String, Box<Mixin>), MixinDecl(String, Box<Mixin>),
FunctionDecl(String, Box<Function>), FunctionDecl(String, Box<Function>),
@ -472,10 +473,10 @@ impl<'a> StyleSheetParser<'a> {
} }
Expr::VariableDecl(name, val) => { Expr::VariableDecl(name, val) => {
if self.scope == 0 { if self.scope == 0 {
scope.insert_var(&name, val.clone()); scope.insert_var(&name, *val.clone());
self.global_scope.insert_var(&name, val); self.global_scope.insert_var(&name, *val);
} else { } else {
scope.insert_var(&name, val); scope.insert_var(&name, *val);
} }
} }
Expr::Include(rules) => stmts.extend(rules), Expr::Include(rules) => stmts.extend(rules),
@ -559,7 +560,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
devour_whitespace(toks); devour_whitespace(toks);
let VariableDecl { val, default } = eat_variable_value(toks, scope)?; let VariableDecl { val, default } = eat_variable_value(toks, scope)?;
if !default || scope.get_var(&name).is_err() { if !default || scope.get_var(&name).is_err() {
return Ok(Some(Expr::VariableDecl(name, val))); return Ok(Some(Expr::VariableDecl(name, Box::new(val))));
} }
} else { } else {
values.push(Token { values.push(Token {

View File

@ -111,7 +111,7 @@ impl Mixin {
})); }));
} }
Expr::VariableDecl(name, val) => { Expr::VariableDecl(name, val) => {
self.scope.insert_var(&name, val); self.scope.insert_var(&name, *val);
} }
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)), Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
} }