Allow optional alpha argument in rgb()

This commit is contained in:
ConnorSkees 2020-02-09 15:07:29 -05:00
parent d6ed0391db
commit 2f54de15c5
2 changed files with 12 additions and 2 deletions

View File

@ -25,7 +25,12 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
Value::Dimension(n, Unit::Percent) => (n / Number::from(100)) * Number::from(255), Value::Dimension(n, Unit::Percent) => (n / Number::from(100)) * Number::from(255),
_ => todo!("expected either unitless or % number for alpha"), _ => todo!("expected either unitless or % number for alpha"),
}; };
Some(Value::Color(Color::from_rgba(red, green, blue, Number::from(1)))) let alpha = match arg!(args, 3, "alpha"=Value::Dimension(Number::from(1), Unit::None)) {
Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
_ => todo!("non-number alpha given to builtin function `rgb()`")
};
Some(Value::Color(Color::from_rgba(red, green, blue, alpha)))
} else { } else {
todo!("channels variable in `rgb`") todo!("channels variable in `rgb`")
} }
@ -154,7 +159,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
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, Unit::None)
| Value::Dimension(n, Unit::Percent) => n / Number::from(100), | Value::Dimension(n, Unit::Percent) => n / Number::from(100),
_ => todo!("non-number weight in given to builtin function `invert()`") _ => todo!("non-number weight given to builtin function `invert()`")
}; };
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Some(Value::Color(c.invert(weight))), Value::Color(c) => Some(Value::Color(c.invert(weight))),

View File

@ -86,6 +86,11 @@ test!(
"a {\n color: rgba(1, 2, 3, 3);\n}\n", "a {\n color: rgba(1, 2, 3, 3);\n}\n",
"a {\n color: #010203;\n}\n" "a {\n color: #010203;\n}\n"
); );
test!(
rgba_negative_alpha,
"a {\n color: rgba(1, 2, 3, -10%);\n}\n",
"a {\n color: rgba(1, 2, 3, 0);\n}\n"
);
test!( test!(
rgba_opacity_decimal, rgba_opacity_decimal,
"a {\n color: rgba(1, 2, 3, .6);\n}\n", "a {\n color: rgba(1, 2, 3, .6);\n}\n",