diff --git a/src/builtin/color_hsl.rs b/src/builtin/color/hsl.rs similarity index 100% rename from src/builtin/color_hsl.rs rename to src/builtin/color/hsl.rs diff --git a/src/builtin/color/mod.rs b/src/builtin/color/mod.rs new file mode 100644 index 0000000..e2f7a27 --- /dev/null +++ b/src/builtin/color/mod.rs @@ -0,0 +1,15 @@ +use std::collections::BTreeMap; + +use super::Builtin; + +mod hsl; +mod opacity; +mod other; +mod rgb; + +pub(crate) fn register(f: &mut BTreeMap) { + hsl::register(f); + opacity::register(f); + other::register(f); + rgb::register(f); +} diff --git a/src/builtin/color/opacity.rs b/src/builtin/color/opacity.rs new file mode 100644 index 0000000..419bacc --- /dev/null +++ b/src/builtin/color/opacity.rs @@ -0,0 +1,70 @@ +use std::collections::BTreeMap; + +use super::Builtin; +use crate::common::QuoteKind; +use crate::units::Unit; +use crate::value::{Number, Value}; + +pub(crate) fn register(f: &mut BTreeMap) { + decl!(f "alpha", |args, _| { + match arg!(args, 0, "color") { + Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), + _ => todo!("non-color given to builtin function `alpha()`") + } + }); + decl!(f "opacity", |args, _| { + match arg!(args, 0, "color") { + Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), + Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), + _ => todo!("non-color given to builtin function `opacity()`") + } + }); + decl!(f "opacify", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `opacify()`") + }; + 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.fade_in(amount))) + }); + decl!(f "fade-in", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `fade-in()`") + }; + 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.fade_in(amount))) + }); + decl!(f "transparentize", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `transparentize()`") + }; + 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.fade_out(amount))) + }); + decl!(f "fade-out", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `fade-out()`") + }; + 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.fade_out(amount))) + }); +} \ No newline at end of file diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs new file mode 100644 index 0000000..74b54dd --- /dev/null +++ b/src/builtin/color/other.rs @@ -0,0 +1,7 @@ +use std::collections::BTreeMap; + +use super::Builtin; + +pub(crate) fn register(_f: &mut BTreeMap) { + +} \ No newline at end of file diff --git a/src/builtin/color.rs b/src/builtin/color/rgb.rs similarity index 66% rename from src/builtin/color.rs rename to src/builtin/color/rgb.rs index 4ff32d4..5ee68d1 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color/rgb.rs @@ -2,7 +2,6 @@ use std::collections::BTreeMap; use super::Builtin; use crate::color::Color; -use crate::common::QuoteKind; use crate::units::Unit; use crate::value::{Number, Value}; @@ -120,65 +119,4 @@ pub(crate) fn register(f: &mut BTreeMap) { _ => todo!("non-color given to builtin function `blue()`") } }); - decl!(f "opacity", |args, _| { - match arg!(args, 0, "color") { - Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), - Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), - _ => todo!("non-color given to builtin function `opacity()`") - } - }); - decl!(f "alpha", |args, _| { - match arg!(args, 0, "color") { - Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), - _ => todo!("non-color given to builtin function `alpha()`") - } - }); - decl!(f "opacify", |args, _| { - let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c, - _ => todo!("non-color given to builtin function `opacify()`") - }; - 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.fade_in(amount))) - }); - decl!(f "fade-in", |args, _| { - let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c, - _ => todo!("non-color given to builtin function `fade-in()`") - }; - 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.fade_in(amount))) - }); - decl!(f "transparentize", |args, _| { - let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c, - _ => todo!("non-color given to builtin function `transparentize()`") - }; - 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.fade_out(amount))) - }); - decl!(f "fade-out", |args, _| { - let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c, - _ => todo!("non-color given to builtin function `fade-out()`") - }; - 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.fade_out(amount))) - }); -} +} \ No newline at end of file diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 82e9393..3cb388f 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -9,7 +9,6 @@ use crate::value::Value; mod macros; mod color; -mod color_hsl; mod list; mod map; mod math; @@ -23,7 +22,6 @@ 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);