Special case builtin function opacity(number)

This commit is contained in:
ConnorSkees 2020-02-08 18:55:48 -05:00
parent bf98382b0b
commit 2a6ec38fd4
2 changed files with 10 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use num_rational::BigRational;
use super::Builtin;
use crate::color::Color;
use crate::common::QuoteKind;
use crate::units::Unit;
use crate::value::{Number, Value};
@ -42,6 +43,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "opacity", |args, _| {
match arg!(args, 0, "color") {
Value::Color(c) => Some(Value::Dimension(Number::new(BigRational::new(BigInt::from(c.alpha()), BigInt::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()`")
}
});

View File

@ -56,3 +56,11 @@ test!(
"a {\n color: alpha(red);\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
opacity_function_number,
"a {\n color: opacity(1);\n}\n"
);
test!(
opacity_function_number_unit,
"a {\n color: opacity(1px);\n}\n"
);