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