From 7f17139a3b98029ff6a215bef43587e10bcff5c2 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 12:43:12 -0500 Subject: [PATCH] Refactor HSL functions into separate module --- src/builtin/color.rs | 140 ------------------------------------ src/builtin/color_hsl.rs | 149 +++++++++++++++++++++++++++++++++++++++ src/builtin/mod.rs | 2 + 3 files changed, 151 insertions(+), 140 deletions(-) create mode 100644 src/builtin/color_hsl.rs diff --git a/src/builtin/color.rs b/src/builtin/color.rs index a1d375a..4ff32d4 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -102,49 +102,6 @@ pub(crate) fn register(f: &mut BTreeMap) { 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) { _ => 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) { _ => 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) { }; 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)))) - }); } diff --git a/src/builtin/color_hsl.rs b/src/builtin/color_hsl.rs new file mode 100644 index 0000000..a322ebb --- /dev/null +++ b/src/builtin/color_hsl.rs @@ -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) { + 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()`") + } + }); +} \ No newline at end of file diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 3cb388f..82e9393 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -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 = { 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);