Refactor HSL functions into separate module
This commit is contained in:
parent
f7351e1458
commit
7f17139a3b
@ -102,49 +102,6 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
Some(Value::Color(Color::from_rgba(red, green, blue, alpha)))
|
||||
}
|
||||
});
|
||||
decl!(f "hsl", |args, _| {
|
||||
let hue = match arg!(args, 0, "hue").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let saturation = match arg!(args, 1, "saturation").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let luminance = match arg!(args, 2, "luminance").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
Some(Value::Color(Color::from_hsla(hue, saturation, luminance, Number::from(1))))
|
||||
});
|
||||
decl!(f "hsla", |args, _| {
|
||||
let hue = match arg!(args, 0, "hue").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let saturation = match arg!(args, 1, "saturation").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let luminance = match arg!(args, 2, "luminance").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let alpha = match arg!(args, 3, "alpha").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
Some(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
||||
});
|
||||
decl!(f "red", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(Number::from(c.red()), Unit::None)),
|
||||
@ -163,24 +120,6 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
_ => todo!("non-color given to builtin function `blue()`")
|
||||
}
|
||||
});
|
||||
decl!(f "hue", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.hue(), Unit::Deg)),
|
||||
_ => todo!("non-color given to builtin function `hue()`")
|
||||
}
|
||||
});
|
||||
decl!(f "saturation", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.saturation(), Unit::Percent)),
|
||||
_ => todo!("non-color given to builtin function `saturation()`")
|
||||
}
|
||||
});
|
||||
decl!(f "lightness", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.lightness(), Unit::Percent)),
|
||||
_ => todo!("non-color given to builtin function `lightness()`")
|
||||
}
|
||||
});
|
||||
decl!(f "opacity", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)),
|
||||
@ -194,78 +133,6 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
_ => todo!("non-color given to builtin function `alpha()`")
|
||||
}
|
||||
});
|
||||
decl!(f "invert", |args, _| {
|
||||
let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("non-number weight given to builtin function `invert()`")
|
||||
};
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Color(c.invert(weight))),
|
||||
_ => todo!("non-color given to builtin function `alpha()`")
|
||||
}
|
||||
});
|
||||
decl!(f "adjust-hue", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `adjust-hue()`")
|
||||
};
|
||||
let degrees = match arg!(args, 1, "degrees").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for degrees"),
|
||||
};
|
||||
Some(Value::Color(color.adjust_hue(degrees)))
|
||||
});
|
||||
decl!(f "lighten", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `lighten()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.lighten(amount)))
|
||||
});
|
||||
decl!(f "darken", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `darken()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.darken(amount)))
|
||||
});
|
||||
decl!(f "saturate", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `saturate()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.saturate(amount)))
|
||||
});
|
||||
decl!(f "desaturate", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `desaturate()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.desaturate(amount)))
|
||||
});
|
||||
decl!(f "opacify", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
@ -314,11 +181,4 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
};
|
||||
Some(Value::Color(color.fade_out(amount)))
|
||||
});
|
||||
decl!(f "grayscale", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `grayscale()`")
|
||||
};
|
||||
Some(Value::Color(color.desaturate(Number::from(1))))
|
||||
});
|
||||
}
|
||||
|
149
src/builtin/color_hsl.rs
Normal file
149
src/builtin/color_hsl.rs
Normal file
@ -0,0 +1,149 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::Builtin;
|
||||
use crate::color::Color;
|
||||
use crate::units::Unit;
|
||||
use crate::value::{Number, Value};
|
||||
|
||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
decl!(f "hsl", |args, _| {
|
||||
let hue = match arg!(args, 0, "hue").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let saturation = match arg!(args, 1, "saturation").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let luminance = match arg!(args, 2, "luminance").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
Some(Value::Color(Color::from_hsla(hue, saturation, luminance, Number::from(1))))
|
||||
});
|
||||
decl!(f "hsla", |args, _| {
|
||||
let hue = match arg!(args, 0, "hue").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let saturation = match arg!(args, 1, "saturation").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let luminance = match arg!(args, 2, "luminance").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
let alpha = match arg!(args, 3, "alpha").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for alpha"),
|
||||
};
|
||||
Some(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
||||
});
|
||||
decl!(f "hue", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.hue(), Unit::Deg)),
|
||||
_ => todo!("non-color given to builtin function `hue()`")
|
||||
}
|
||||
});
|
||||
decl!(f "saturation", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.saturation(), Unit::Percent)),
|
||||
_ => todo!("non-color given to builtin function `saturation()`")
|
||||
}
|
||||
});
|
||||
decl!(f "lightness", |args, _| {
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Dimension(c.lightness(), Unit::Percent)),
|
||||
_ => todo!("non-color given to builtin function `lightness()`")
|
||||
}
|
||||
});
|
||||
decl!(f "adjust-hue", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `adjust-hue()`")
|
||||
};
|
||||
let degrees = match arg!(args, 1, "degrees").eval() {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent)
|
||||
| Value::Dimension(n, Unit::Deg) => n,
|
||||
_ => todo!("expected either unitless or % number for degrees"),
|
||||
};
|
||||
Some(Value::Color(color.adjust_hue(degrees)))
|
||||
});
|
||||
decl!(f "lighten", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `lighten()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.lighten(amount)))
|
||||
});
|
||||
decl!(f "darken", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `darken()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.darken(amount)))
|
||||
});
|
||||
decl!(f "saturate", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `saturate()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.saturate(amount)))
|
||||
});
|
||||
decl!(f "desaturate", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `desaturate()`")
|
||||
};
|
||||
let amount = match arg!(args, 1, "amount").eval() {
|
||||
Value::Dimension(n, Unit::None) => n,
|
||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("expected either unitless or % number for amount"),
|
||||
};
|
||||
Some(Value::Color(color.desaturate(amount)))
|
||||
});
|
||||
decl!(f "grayscale", |args, _| {
|
||||
let color = match arg!(args, 0, "color").eval() {
|
||||
Value::Color(c) => c,
|
||||
_ => todo!("non-color given to builtin function `grayscale()`")
|
||||
};
|
||||
Some(Value::Color(color.desaturate(Number::from(1))))
|
||||
});
|
||||
decl!(f "invert", |args, _| {
|
||||
let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) {
|
||||
Value::Dimension(n, Unit::None)
|
||||
| Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||
_ => todo!("non-number weight given to builtin function `invert()`")
|
||||
};
|
||||
match arg!(args, 0, "color") {
|
||||
Value::Color(c) => Some(Value::Color(c.invert(weight))),
|
||||
_ => todo!("non-color given to builtin function `alpha()`")
|
||||
}
|
||||
});
|
||||
}
|
@ -9,6 +9,7 @@ use crate::value::Value;
|
||||
mod macros;
|
||||
|
||||
mod color;
|
||||
mod color_hsl;
|
||||
mod list;
|
||||
mod map;
|
||||
mod math;
|
||||
@ -22,6 +23,7 @@ lazy_static! {
|
||||
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = {
|
||||
let mut m = BTreeMap::new();
|
||||
color::register(&mut m);
|
||||
color_hsl::register(&mut m);
|
||||
list::register(&mut m);
|
||||
math::register(&mut m);
|
||||
meta::register(&mut m);
|
||||
|
Loading…
x
Reference in New Issue
Block a user