More builtin color function errors

This commit is contained in:
ConnorSkees 2020-02-16 12:14:20 -05:00
parent 1995b5ec6e
commit 9a36c3be73
3 changed files with 16 additions and 22 deletions

View File

@ -73,7 +73,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}; };
let degrees = match arg!(args, 1, "degrees").eval() { let degrees = match arg!(args, 1, "degrees").eval() {
Value::Dimension(n, _) => n, Value::Dimension(n, _) => n,
_ => todo!("expected either unitless or % number for degrees"), v => return Err(format!("$degrees: {} is not a number.", v).into()),
}; };
Ok(Value::Color(color.adjust_hue(degrees))) Ok(Value::Color(color.adjust_hue(degrees)))
}); });
@ -137,9 +137,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}); });
decl!(f "invert", |args, _| { decl!(f "invert", |args, _| {
let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) { let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) {
Value::Dimension(n, Unit::None) Value::Dimension(n, _) => n / Number::from(100),
| Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$weight: {} is not a number.", v).into()),
_ => todo!("non-number weight given to builtin function `invert()`")
}; };
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Color(c.invert(weight))), Value::Color(c) => Ok(Value::Color(c.invert(weight))),

View File

@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use super::Builtin; use super::Builtin;
use crate::common::QuoteKind; use crate::common::QuoteKind;
use crate::units::Unit; use crate::units::Unit;
use crate::value::{Number, Value}; use crate::value::Value;
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) { pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "alpha", |args, _| { decl!(f "alpha", |args, _| {
@ -25,9 +25,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let amount = match arg!(args, 1, "amount").eval() { let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, _) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$amount: {} is not a number.", v).into()),
_ => todo!("expected either unitless or % number for amount"),
}; };
Ok(Value::Color(color.fade_in(amount))) Ok(Value::Color(color.fade_in(amount)))
}); });
@ -37,9 +36,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let amount = match arg!(args, 1, "amount").eval() { let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, _) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$amount: {} is not a number.", v).into()),
_ => todo!("expected either unitless or % number for amount"),
}; };
Ok(Value::Color(color.fade_in(amount))) Ok(Value::Color(color.fade_in(amount)))
}); });
@ -49,9 +47,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let amount = match arg!(args, 1, "amount").eval() { let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, _) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$amount: {} is not a number.", v).into()),
_ => todo!("expected either unitless or % number for amount"),
}; };
Ok(Value::Color(color.fade_out(amount))) Ok(Value::Color(color.fade_out(amount)))
}); });
@ -61,9 +58,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let amount = match arg!(args, 1, "amount").eval() { let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, _) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$amount: {} is not a number.", v).into()),
_ => todo!("expected either unitless or % number for amount"),
}; };
Ok(Value::Color(color.fade_out(amount))) Ok(Value::Color(color.fade_out(amount)))
}); });

View File

@ -117,7 +117,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
} else if args.len() == 2 { } else if args.len() == 2 {
let color = match arg!(args, 0, "color").eval() { let color = match arg!(args, 0, "color").eval() {
Value::Color(c) => c, Value::Color(c) => c,
_ => todo!("expected color") v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let alpha = match arg!(args, 1, "alpha").eval() { let alpha = match arg!(args, 1, "alpha").eval() {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, Unit::None) => n,
@ -183,10 +183,9 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()), v => return Err(format!("$color: {} is not a color.", v).into()),
}; };
let weight = match arg!(args, 2, "weight"=Value::Dimension(Number::ratio(1, 2), Unit::None)) { let weight = match arg!(args, 2, "weight"=Value::Dimension(Number::from(50), Unit::None)) {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, _) => n / Number::from(100),
Value::Dimension(n, Unit::Percent) => n / Number::from(100), v => return Err(format!("$weight: {} is not a number.", v).into()),
_ => todo!("expected either unitless or % number for $weight")
}; };
Ok(Value::Color(color1.mix(color2, weight))) Ok(Value::Color(color1.mix(color2, weight)))
}); });