Handle alpha over 1 (a little bit)

This commit is contained in:
ConnorSkees 2020-02-14 20:13:58 -05:00
parent bb7e47a4fb
commit 19f59efd98
4 changed files with 15 additions and 4 deletions

View File

@ -150,7 +150,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}; };
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))),
_ => todo!("non-color given to builtin function `alpha()`") _ => todo!("non-color given to builtin function `invert()`")
} }
}); });
} }

View File

@ -8,13 +8,13 @@ use crate::value::{Number, 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, _| {
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), Value::Color(c) => Some(Value::Dimension(c.alpha(), Unit::None)),
_ => todo!("non-color given to builtin function `alpha()`") _ => todo!("non-color given to builtin function `alpha()`")
} }
}); });
decl!(f "opacity", |args, _| { decl!(f "opacity", |args, _| {
match arg!(args, 0, "color") { match arg!(args, 0, "color") {
Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), Value::Color(c) => Some(Value::Dimension(c.alpha(), Unit::None)),
Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)),
_ => todo!("non-color given to builtin function `opacity()`") _ => todo!("non-color given to builtin function `opacity()`")
} }

View File

@ -255,6 +255,7 @@ impl Color {
} else { } else {
hue hue
}; };
let saturation = clamp!(saturation, 0, 1); let saturation = clamp!(saturation, 0, 1);
let luminance = clamp!(luminance, 0, 1); let luminance = clamp!(luminance, 0, 1);
let alpha = clamp!(alpha, 0, 1); let alpha = clamp!(alpha, 0, 1);
@ -367,7 +368,12 @@ impl Color {
/// Opacity color functions /// Opacity color functions
impl Color { impl Color {
pub fn alpha(&self) -> Number { pub fn alpha(&self) -> Number {
self.alpha.clone() let a = self.alpha.clone();
if a > Number::from(1) {
a / Number::from(255)
} else {
a
}
} }
/// Change `alpha` to value given /// Change `alpha` to value given

View File

@ -436,3 +436,8 @@ test!(
"a {\n color: scale-color(hsl(200, 70%, 80%), $saturation: -90%, $alpha: -30%);\n}\n", "a {\n color: scale-color(hsl(200, 70%, 80%), $saturation: -90%, $alpha: -30%);\n}\n",
"a {\n color: rgba(200, 205, 208, 0.7);\n}\n" "a {\n color: rgba(200, 205, 208, 0.7);\n}\n"
); );
test!(
scale_color_alpha_over_1,
"a {\n color: scale-color(sienna, $alpha: -70%);\n}\n",
"a {\n color: rgba(160, 82, 45, 0.3);\n}\n"
);