unify color checking in builtin color fns

This commit is contained in:
Connor Skees 2022-12-28 16:45:06 -05:00
parent 494acb9933
commit e5da366fac
5 changed files with 99 additions and 207 deletions

View File

@ -125,50 +125,41 @@ pub(crate) fn hsla(args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Va
pub(crate) fn hue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn hue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: (c.hue()), .assert_color_with_name("color", args.span())?;
unit: Unit::Deg,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.hue(),
v => Err(( unit: Unit::Deg,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn saturation(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn saturation(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: (c.saturation()), .assert_color_with_name("color", args.span())?;
unit: Unit::Percent,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.saturation(),
v => Err(( unit: Unit::Percent,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn lightness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn lightness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: c.lightness(), .assert_color_with_name("color", args.span())?;
unit: Unit::Percent,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.lightness(),
v => Err(( unit: Unit::Percent,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn adjust_hue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn adjust_hue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
@ -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<Value> { fn lighten(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let amount = args let amount = args
.get_err(1, "amount")? .get_err(1, "amount")?
@ -207,16 +191,10 @@ fn lighten(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value>
fn darken(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { fn darken(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let amount = match args.get_err(1, "amount")? { let amount = match args.get_err(1, "amount")? {
Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(),
Value::Dimension(SassNumber { Value::Dimension(SassNumber {
@ -298,16 +276,9 @@ fn saturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value
fn desaturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { fn desaturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let amount = match args.get_err(1, "amount")? { let amount = match args.get_err(1, "amount")? {
Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(),
Value::Dimension(SassNumber { 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<Value> { pub(crate) fn complement(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
Ok(Value::Color(Box::new(color.complement()))) Ok(Value::Color(Box::new(color.complement())))
} }

View File

@ -5,16 +5,9 @@ use super::rgb::{parse_channels, ParsedChannels};
pub(crate) fn blackness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn blackness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let blackness = let blackness =
Number(1.0) - (color.red().max(color.green()).max(color.blue()) / Number(255.0)); Number(1.0) - (color.red().max(color.green()).max(color.blue()) / Number(255.0));

View File

@ -34,7 +34,7 @@ pub(crate) fn alpha(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResu
if args.len() <= 1 { if args.len() <= 1 {
match args.get_err(0, "color")? { match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Dimension(SassNumber { Value::Color(c) => Ok(Value::Dimension(SassNumber {
num: (c.alpha()), num: c.alpha(),
unit: Unit::None, unit: Unit::None,
as_slash: 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")? { match args.get_err(0, "color")? {
Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(),
Value::Color(c) => Ok(Value::Dimension(SassNumber { Value::Color(c) => Ok(Value::Dimension(SassNumber {
num: (c.alpha()), num: c.alpha(),
unit: Unit::None, unit: Unit::None,
as_slash: 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<Value> { fn opacify(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let amount = args let amount = args
.get_err(1, "amount")? .get_err(1, "amount")?
.assert_number_with_name("amount", args.span())?; .assert_number_with_name("amount", args.span())?;
@ -116,16 +109,9 @@ fn opacify(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value>
fn transparentize(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { fn transparentize(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let amount = match args.get_err(1, "amount")? { let amount = match args.get_err(1, "amount")? {
Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(), Value::Dimension(SassNumber { num: n, .. }) if n.is_nan() => todo!(),
Value::Dimension(SassNumber { Value::Dimension(SassNumber {

View File

@ -47,16 +47,9 @@ pub(crate) fn change_color(mut args: ArgumentResult, visitor: &mut Visitor) -> S
.into()); .into());
} }
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
opt_rgba!(args, alpha, "alpha", 0, 1); opt_rgba!(args, alpha, "alpha", 0, 1);
opt_rgba!(args, red, "red", 0, 255); 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 { Ok(Value::Color(if let Some(a) = alpha {
Box::new(color.with_alpha(a)) Box::new(color.with_alpha(a))
} else { } else {
color Box::new(color)
})) }))
} }
pub(crate) fn adjust_color(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn adjust_color(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
opt_rgba!(args, alpha, "alpha", -1, 1); opt_rgba!(args, alpha, "alpha", -1, 1);
opt_rgba!(args, red, "red", -255, 255); 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(); let temp_alpha = color.alpha();
Box::new(color.with_alpha(temp_alpha + a)) Box::new(color.with_alpha(temp_alpha + a))
} else { } 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 span = args.span();
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(span)?),
span,
)
.into())
}
};
macro_rules! opt_scale_arg { macro_rules! opt_scale_arg {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal) => { ($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(); let temp_alpha = color.alpha();
Box::new(color.with_alpha(scale(temp_alpha, a, Number::one()))) Box::new(color.with_alpha(scale(temp_alpha, a, Number::one())))
} else { } else {
color Box::new(color)
})) }))
} }
pub(crate) fn ie_hex_str(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn ie_hex_str(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
let color = match args.get_err(0, "color")? { let color = args
Value::Color(c) => c, .get_err(0, "color")?
v => { .assert_color_with_name("color", args.span())?;
return Err((
format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
Ok(Value::String(color.to_ie_hex_str(), QuoteKind::None)) Ok(Value::String(color.to_ie_hex_str(), QuoteKind::None))
} }

View File

@ -354,75 +354,52 @@ pub(crate) fn rgba(args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Va
pub(crate) fn red(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn red(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: (c.red()), .assert_color_with_name("color", args.span())?;
unit: Unit::None,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.red(),
v => Err(( unit: Unit::None,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn green(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn green(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: (c.green()), .assert_color_with_name("color", args.span())?;
unit: Unit::None,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.green(),
v => Err(( unit: Unit::None,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn blue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn blue(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match args.get_err(0, "color")? { let color = args
Value::Color(c) => Ok(Value::Dimension(SassNumber { .get_err(0, "color")?
num: (c.blue()), .assert_color_with_name("color", args.span())?;
unit: Unit::None,
as_slash: None, Ok(Value::Dimension(SassNumber {
})), num: color.blue(),
v => Err(( unit: Unit::None,
format!("$color: {} is not a color.", v.inspect(args.span())?), as_slash: None,
args.span(), }))
)
.into()),
}
} }
pub(crate) fn mix(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> { pub(crate) fn mix(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(3)?; args.max_args(3)?;
let color1 = match args.get_err(0, "color1")? { let color1 = args
Value::Color(c) => c, .get_err(0, "color1")?
v => { .assert_color_with_name("color1", args.span())?;
return Err((
format!("$color1: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let color2 = match args.get_err(1, "color2")? { let color2 = args
Value::Color(c) => c, .get_err(1, "color2")?
v => { .assert_color_with_name("color2", args.span())?;
return Err((
format!("$color2: {} is not a color.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
let weight = match args.default_arg( let weight = match args.default_arg(
2, 2,