Refactor color functions into separate modules
This commit is contained in:
parent
dafd1f3e0d
commit
8f63694ffd
15
src/builtin/color/mod.rs
Normal file
15
src/builtin/color/mod.rs
Normal file
@ -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<String, Builtin>) {
|
||||
hsl::register(f);
|
||||
opacity::register(f);
|
||||
other::register(f);
|
||||
rgb::register(f);
|
||||
}
|
70
src/builtin/color/opacity.rs
Normal file
70
src/builtin/color/opacity.rs
Normal file
@ -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<String, Builtin>) {
|
||||
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)))
|
||||
});
|
||||
}
|
7
src/builtin/color/other.rs
Normal file
7
src/builtin/color/other.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::Builtin;
|
||||
|
||||
pub(crate) fn register(_f: &mut BTreeMap<String, Builtin>) {
|
||||
|
||||
}
|
@ -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<String, Builtin>) {
|
||||
_ => 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)))
|
||||
});
|
||||
}
|
@ -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<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