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> {
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<Value> {
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<Value> {
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<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> {
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<Value>
fn darken(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
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<Value
fn desaturate(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
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<Value> {
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())))
}

View File

@ -5,16 +5,9 @@ use super::rgb::{parse_channels, ParsedChannels};
pub(crate) fn blackness(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
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));

View File

@ -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<Value> {
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<Value>
fn transparentize(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
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 {

View File

@ -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<Value> {
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<Value> {
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))
}

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> {
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<Value> {
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<Value> {
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<Value> {
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,