Refactor color functions into separate modules

This commit is contained in:
ConnorSkees 2020-02-14 15:08:41 -05:00
parent dafd1f3e0d
commit 8f63694ffd
6 changed files with 93 additions and 65 deletions

15
src/builtin/color/mod.rs Normal file
View 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);
}

View 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)))
});
}

View File

@ -0,0 +1,7 @@
use std::collections::BTreeMap;
use super::Builtin;
pub(crate) fn register(_f: &mut BTreeMap<String, Builtin>) {
}

View File

@ -2,7 +2,6 @@ use std::collections::BTreeMap;
use super::Builtin; use super::Builtin;
use crate::color::Color; use crate::color::Color;
use crate::common::QuoteKind;
use crate::units::Unit; use crate::units::Unit;
use crate::value::{Number, Value}; 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()`") _ => 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)))
});
}

View File

@ -9,7 +9,6 @@ use crate::value::Value;
mod macros; mod macros;
mod color; mod color;
mod color_hsl;
mod list; mod list;
mod map; mod map;
mod math; mod math;
@ -23,7 +22,6 @@ lazy_static! {
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = { pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = {
let mut m = BTreeMap::new(); let mut m = BTreeMap::new();
color::register(&mut m); color::register(&mut m);
color_hsl::register(&mut m);
list::register(&mut m); list::register(&mut m);
math::register(&mut m); math::register(&mut m);
meta::register(&mut m); meta::register(&mut m);