simplify declaration of hsl fns

This commit is contained in:
ConnorSkees 2020-04-30 18:51:10 -04:00
parent 22098ca684
commit 3e8b83d8ad

View File

@ -3,15 +3,17 @@ use super::GlobalFunctionMap;
use num_traits::One;
use super::Builtin;
use crate::args::CallArgs;
use crate::color::Color;
use crate::common::QuoteKind;
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert(
"hsl",
Builtin::new(|mut args, scope, super_selector| {
fn hsl(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
if args.is_empty() {
return Err(("Missing argument $channels.", args.span()).into());
}
@ -215,11 +217,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
hue, saturation, lightness, alpha,
))))
}
}),
);
f.insert(
"hsla",
Builtin::new(|mut args, scope, super_selector| {
}
fn hsla(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
if args.is_empty() {
return Err(("Missing argument $channels.", args.span()).into());
}
@ -423,11 +423,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
hue, saturation, lightness, alpha,
))))
}
}),
);
f.insert(
"hue",
Builtin::new(|mut args, scope, super_selector| {
}
fn hue(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.hue(), Unit::Deg)),
@ -437,11 +435,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"saturation",
Builtin::new(|mut args, scope, super_selector| {
}
fn saturation(
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.saturation(), Unit::Percent)),
@ -451,11 +451,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"lightness",
Builtin::new(|mut args, scope, super_selector| {
}
fn lightness(
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.lightness(), Unit::Percent)),
@ -465,11 +467,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"adjust-hue",
Builtin::new(|mut args, scope, super_selector| {
}
fn adjust_hue(
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,
@ -495,11 +499,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.adjust_hue(degrees))))
}),
);
f.insert(
"lighten",
Builtin::new(|mut args, scope, super_selector| {
}
fn lighten(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,
@ -525,11 +527,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.lighten(amount))))
}),
);
f.insert(
"darken",
Builtin::new(|mut args, scope, super_selector| {
}
fn darken(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,
@ -555,18 +555,15 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.darken(amount))))
}),
);
f.insert(
"saturate",
Builtin::new(|mut args, scope, super_selector| {
}
fn saturate(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
if args.len() == 1 {
return Ok(Value::Ident(
format!(
"saturate({})",
arg!(args, scope, super_selector, 0, "amount")
.to_css_string(args.span())?
arg!(args, scope, super_selector, 0, "amount").to_css_string(args.span())?
),
QuoteKind::None,
));
@ -602,11 +599,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.saturate(amount))))
}),
);
f.insert(
"desaturate",
Builtin::new(|mut args, scope, super_selector| {
}
fn desaturate(
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,
@ -632,11 +631,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.desaturate(amount))))
}),
);
f.insert(
"grayscale",
Builtin::new(|mut args, scope, super_selector| {
}
fn grayscale(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -655,11 +656,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.desaturate(Number::one()))))
}),
);
f.insert(
"complement",
Builtin::new(|mut args, scope, super_selector| {
}
fn complement(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -672,11 +675,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Color(Box::new(color.complement())))
}),
);
f.insert(
"invert",
Builtin::new(|mut args, scope, super_selector| {
}
fn invert(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let weight = match arg!(
args,
@ -713,6 +714,19 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
}
f.insert("hsl", Builtin::new(hsl));
f.insert("hsla", Builtin::new(hsla));
f.insert("hue", Builtin::new(hue));
f.insert("saturation", Builtin::new(saturation));
f.insert("adjust-hue", Builtin::new(adjust_hue));
f.insert("lightness", Builtin::new(lightness));
f.insert("lighten", Builtin::new(lighten));
f.insert("darken", Builtin::new(darken));
f.insert("saturate", Builtin::new(saturate));
f.insert("desaturate", Builtin::new(desaturate));
f.insert("grayscale", Builtin::new(grayscale));
f.insert("complement", Builtin::new(complement));
f.insert("invert", Builtin::new(invert));
}