From e5da366fac0f6aba98f7dcfd55892c506ef4f459 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Wed, 28 Dec 2022 16:45:06 -0500 Subject: [PATCH] unify color checking in builtin color fns --- src/builtin/functions/color/hsl.rs | 116 +++++++++---------------- src/builtin/functions/color/hwb.rs | 13 +-- src/builtin/functions/color/opacity.rs | 30 ++----- src/builtin/functions/color/other.rs | 58 ++++--------- src/builtin/functions/color/rgb.rs | 89 +++++++------------ 5 files changed, 99 insertions(+), 207 deletions(-) diff --git a/src/builtin/functions/color/hsl.rs b/src/builtin/functions/color/hsl.rs index a8daac0..43b1dd3 100644 --- a/src/builtin/functions/color/hsl.rs +++ b/src/builtin/functions/color/hsl.rs @@ -125,50 +125,41 @@ pub(crate) fn hsla(args: ArgumentResult, visitor: &mut Visitor) -> SassResult SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.hue()), - unit: Unit::Deg, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.hue(), + unit: Unit::Deg, + as_slash: None, + })) } pub(crate) fn saturation(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.saturation()), - unit: Unit::Percent, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.saturation(), + unit: Unit::Percent, + as_slash: None, + })) } pub(crate) fn lightness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: c.lightness(), - unit: Unit::Percent, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.lightness(), + unit: Unit::Percent, + as_slash: None, + })) } pub(crate) fn adjust_hue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { @@ -186,16 +177,9 @@ pub(crate) fn adjust_hue(mut args: ArgumentResult, visitor: &mut Visitor) -> Sas fn lighten(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(2)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; let amount = args .get_err(1, "amount")? @@ -207,16 +191,10 @@ fn lighten(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult fn darken(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(2)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + let amount = match args.get_err(1, "amount")? { Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { @@ -298,16 +276,9 @@ fn saturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult SassResult { args.max_args(2)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; let amount = match args.get_err(1, "amount")? { Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { @@ -356,16 +327,9 @@ pub(crate) fn grayscale(mut args: ArgumentResult, visitor: &mut Visitor) -> Sass pub(crate) fn complement(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; Ok(Value::Color(Box::new(color.complement()))) } diff --git a/src/builtin/functions/color/hwb.rs b/src/builtin/functions/color/hwb.rs index ce65956..a1f8326 100644 --- a/src/builtin/functions/color/hwb.rs +++ b/src/builtin/functions/color/hwb.rs @@ -5,16 +5,9 @@ use super::rgb::{parse_channels, ParsedChannels}; pub(crate) fn blackness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; let blackness = Number(1.0) - (color.red().max(color.green()).max(color.blue()) / Number(255.0)); diff --git a/src/builtin/functions/color/opacity.rs b/src/builtin/functions/color/opacity.rs index 8d4aa26..3fb384b 100644 --- a/src/builtin/functions/color/opacity.rs +++ b/src/builtin/functions/color/opacity.rs @@ -34,7 +34,7 @@ pub(crate) fn alpha(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResu if args.len() <= 1 { match args.get_err(0, "color")? { Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.alpha()), + num: c.alpha(), unit: Unit::None, as_slash: None, })), @@ -73,7 +73,7 @@ pub(crate) fn opacity(mut args: ArgumentResult, visitor: &mut Visitor) -> SassRe match args.get_err(0, "color")? { Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.alpha()), + num: c.alpha(), unit: Unit::None, as_slash: None, })), @@ -95,16 +95,9 @@ pub(crate) fn opacity(mut args: ArgumentResult, visitor: &mut Visitor) -> SassRe fn opacify(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(2)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; let amount = args .get_err(1, "amount")? .assert_number_with_name("amount", args.span())?; @@ -116,16 +109,9 @@ fn opacify(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult fn transparentize(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(2)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; let amount = match args.get_err(1, "amount")? { Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { diff --git a/src/builtin/functions/color/other.rs b/src/builtin/functions/color/other.rs index 662819c..57f8c3e 100644 --- a/src/builtin/functions/color/other.rs +++ b/src/builtin/functions/color/other.rs @@ -47,16 +47,9 @@ pub(crate) fn change_color(mut args: ArgumentResult, visitor: &mut Visitor) -> S .into()); } - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; opt_rgba!(args, alpha, "alpha", 0, 1); opt_rgba!(args, red, "red", 0, 255); @@ -102,21 +95,14 @@ pub(crate) fn change_color(mut args: ArgumentResult, visitor: &mut Visitor) -> S Ok(Value::Color(if let Some(a) = alpha { Box::new(color.with_alpha(a)) } else { - color + Box::new(color) })) } pub(crate) fn adjust_color(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; opt_rgba!(args, alpha, "alpha", -1, 1); opt_rgba!(args, red, "red", -255, 255); @@ -163,7 +149,7 @@ pub(crate) fn adjust_color(mut args: ArgumentResult, visitor: &mut Visitor) -> S let temp_alpha = color.alpha(); Box::new(color.with_alpha(temp_alpha + a)) } else { - color + Box::new(color) })) } @@ -178,16 +164,9 @@ pub(crate) fn scale_color(mut args: ArgumentResult, visitor: &mut Visitor) -> Sa } let span = args.span(); - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(span)?), - span, - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; macro_rules! opt_scale_arg { ($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal) => { @@ -277,22 +256,15 @@ pub(crate) fn scale_color(mut args: ArgumentResult, visitor: &mut Visitor) -> Sa let temp_alpha = color.alpha(); Box::new(color.with_alpha(scale(temp_alpha, a, Number::one()))) } else { - color + Box::new(color) })) } pub(crate) fn ie_hex_str(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - let color = match args.get_err(0, "color")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; Ok(Value::String(color.to_ie_hex_str(), QuoteKind::None)) } diff --git a/src/builtin/functions/color/rgb.rs b/src/builtin/functions/color/rgb.rs index 2f234d3..23a1c01 100644 --- a/src/builtin/functions/color/rgb.rs +++ b/src/builtin/functions/color/rgb.rs @@ -354,75 +354,52 @@ pub(crate) fn rgba(args: ArgumentResult, visitor: &mut Visitor) -> SassResult SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.red()), - unit: Unit::None, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.red(), + unit: Unit::None, + as_slash: None, + })) } pub(crate) fn green(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.green()), - unit: Unit::None, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.green(), + unit: Unit::None, + as_slash: None, + })) } pub(crate) fn blue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(1)?; - match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Dimension(SassNumber { - num: (c.blue()), - unit: Unit::None, - as_slash: None, - })), - v => Err(( - format!("$color: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()), - } + let color = args + .get_err(0, "color")? + .assert_color_with_name("color", args.span())?; + + Ok(Value::Dimension(SassNumber { + num: color.blue(), + unit: Unit::None, + as_slash: None, + })) } pub(crate) fn mix(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult { args.max_args(3)?; - let color1 = match args.get_err(0, "color1")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color1: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color1 = args + .get_err(0, "color1")? + .assert_color_with_name("color1", args.span())?; - let color2 = match args.get_err(1, "color2")? { - Value::Color(c) => c, - v => { - return Err(( - format!("$color2: {} is not a color.", v.inspect(args.span())?), - args.span(), - ) - .into()) - } - }; + let color2 = args + .get_err(1, "color2")? + .assert_color_with_name("color2", args.span())?; let weight = match args.default_arg( 2,