simplify declaration of opacity fns

This commit is contained in:
ConnorSkees 2020-04-30 18:54:29 -04:00
parent 3e8b83d8ad
commit 11e0d523b2

View File

@ -1,162 +1,164 @@
use super::GlobalFunctionMap; use super::GlobalFunctionMap;
use super::Builtin; use super::Builtin;
use crate::args::CallArgs;
use crate::common::QuoteKind; use crate::common::QuoteKind;
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::unit::Unit; use crate::unit::Unit;
use crate::value::Number; use crate::value::Number;
use crate::value::Value; use crate::value::Value;
pub(crate) fn register(f: &mut GlobalFunctionMap) { pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert( fn alpha(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
"alpha", args.max_args(1)?;
Builtin::new(|mut args, scope, super_selector| { match arg!(args, scope, super_selector, 0, "color") {
args.max_args(1)?; Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)),
match arg!(args, scope, super_selector, 0, "color") { v => Err((
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), format!("$color: {} is not a color.", v.to_css_string(args.span())?),
v => Err(( args.span(),
)
.into()),
}
}
fn opacity(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)),
Value::Dimension(num, unit) => Ok(Value::Ident(
format!("opacity({}{})", num, unit),
QuoteKind::None,
)),
v => Err((
format!("$color: {} is not a color.", v.to_css_string(args.span())?),
args.span(),
)
.into()),
}
}
fn opacify(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
v => {
return Err((
format!("$color: {} is not a color.", v.to_css_string(args.span())?), format!("$color: {} is not a color.", v.to_css_string(args.span())?),
args.span(), args.span(),
) )
.into()), .into())
} }
}), };
); let amount = match arg!(args, scope, super_selector, 1, "amount") {
f.insert( Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1),
"opacity", v => {
Builtin::new(|mut args, scope, super_selector| { return Err((
args.max_args(1)?; format!(
match arg!(args, scope, super_selector, 0, "color") { "$amount: {} is not a number.",
Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), v.to_css_string(args.span())?
Value::Dimension(num, unit) => Ok(Value::Ident( ),
format!("opacity({}{})", num, unit), args.span(),
QuoteKind::None, )
)), .into())
v => Err(( }
};
Ok(Value::Color(Box::new(color.fade_in(amount))))
}
fn fade_in(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
v => {
return Err((
format!("$color: {} is not a color.", v.to_css_string(args.span())?), format!("$color: {} is not a color.", v.to_css_string(args.span())?),
args.span(), args.span(),
) )
.into()), .into())
} }
}), };
); let amount = match arg!(args, scope, super_selector, 1, "amount") {
f.insert( Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1),
"opacify", v => {
Builtin::new(|mut args, scope, super_selector| { return Err((
args.max_args(2)?; format!(
let color = match arg!(args, scope, super_selector, 0, "color") { "$amount: {} is not a number.",
Value::Color(c) => c, v.to_css_string(args.span())?
v => { ),
return Err(( args.span(),
format!("$color: {} is not a color.", v.to_css_string(args.span())?), )
args.span(), .into())
) }
.into()) };
} Ok(Value::Color(Box::new(color.fade_in(amount))))
}; }
let amount = match arg!(args, scope, super_selector, 1, "amount") {
Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1), fn transparentize(
v => { mut args: CallArgs,
return Err(( scope: &Scope,
format!( super_selector: &Selector,
"$amount: {} is not a number.", ) -> SassResult<Value> {
v.to_css_string(args.span())? args.max_args(2)?;
), let color = match arg!(args, scope, super_selector, 0, "color") {
args.span(), Value::Color(c) => c,
) v => {
.into()) return Err((
} format!("$color: {} is not a color.", v.to_css_string(args.span())?),
}; args.span(),
Ok(Value::Color(Box::new(color.fade_in(amount)))) )
}), .into())
); }
f.insert( };
"fade-in", let amount = match arg!(args, scope, super_selector, 1, "amount") {
Builtin::new(|mut args, scope, super_selector| { Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1),
args.max_args(2)?; v => {
let color = match arg!(args, scope, super_selector, 0, "color") { return Err((
Value::Color(c) => c, format!(
v => { "$amount: {} is not a number.",
return Err(( v.to_css_string(args.span())?
format!("$color: {} is not a color.", v.to_css_string(args.span())?), ),
args.span(), args.span(),
) )
.into()) .into())
} }
}; };
let amount = match arg!(args, scope, super_selector, 1, "amount") { Ok(Value::Color(Box::new(color.fade_out(amount))))
Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1), }
v => {
return Err(( fn fade_out(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
format!( args.max_args(2)?;
"$amount: {} is not a number.", let color = match arg!(args, scope, super_selector, 0, "color") {
v.to_css_string(args.span())? Value::Color(c) => c,
), v => {
args.span(), return Err((
) format!("$color: {} is not a color.", v.to_css_string(args.span())?),
.into()) args.span(),
} )
}; .into())
Ok(Value::Color(Box::new(color.fade_in(amount)))) }
}), };
); let amount = match arg!(args, scope, super_selector, 1, "amount") {
f.insert( Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1),
"transparentize", v => {
Builtin::new(|mut args, scope, super_selector| { return Err((
args.max_args(2)?; format!(
let color = match arg!(args, scope, super_selector, 0, "color") { "$amount: {} is not a number.",
Value::Color(c) => c, v.to_css_string(args.span())?
v => { ),
return Err(( args.span(),
format!("$color: {} is not a color.", v.to_css_string(args.span())?), )
args.span(), .into())
) }
.into()) };
} Ok(Value::Color(Box::new(color.fade_out(amount))))
}; }
let amount = match arg!(args, scope, super_selector, 1, "amount") {
Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1), f.insert("alpha", Builtin::new(alpha));
v => { f.insert("opacity", Builtin::new(opacity));
return Err(( f.insert("opacify", Builtin::new(opacify));
format!( f.insert("fade-in", Builtin::new(fade_in));
"$amount: {} is not a number.", f.insert("transparentize", Builtin::new(transparentize));
v.to_css_string(args.span())? f.insert("fade-out", Builtin::new(fade_out));
),
args.span(),
)
.into())
}
};
Ok(Value::Color(Box::new(color.fade_out(amount))))
}),
);
f.insert(
"fade-out",
Builtin::new(|mut args, scope, super_selector| {
args.max_args(2)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
v => {
return Err((
format!("$color: {} is not a color.", v.to_css_string(args.span())?),
args.span(),
)
.into())
}
};
let amount = match arg!(args, scope, super_selector, 1, "amount") {
Value::Dimension(n, u) => bound!(args, "amount", n, u, 0, 1),
v => {
return Err((
format!(
"$amount: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
};
Ok(Value::Color(Box::new(color.fade_out(amount))))
}),
);
} }