box color in value

This commit is contained in:
ConnorSkees 2020-04-21 18:54:19 -04:00
parent 632ff5aae8
commit 33ccabce7b
6 changed files with 70 additions and 46 deletions

View File

@ -75,12 +75,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
None => return Err(("Missing element $hue.", args.span()).into()),
};
Ok(Value::Color(Color::from_hsla(
Ok(Value::Color(Box::new(Color::from_hsla(
hue,
saturation,
lightness,
Number::one(),
)))
))))
} else {
let hue = match arg!(args, scope, super_selector, 0, "hue") {
Value::Dimension(n, _) => n,
@ -211,9 +211,9 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(Color::from_hsla(
Ok(Value::Color(Box::new(Color::from_hsla(
hue, saturation, lightness, alpha,
)))
))))
}
}),
);
@ -283,12 +283,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
None => return Err(("Missing element $hue.", args.span()).into()),
};
Ok(Value::Color(Color::from_hsla(
Ok(Value::Color(Box::new(Color::from_hsla(
hue,
saturation,
lightness,
Number::one(),
)))
))))
} else {
let hue = match arg!(args, scope, super_selector, 0, "hue") {
Value::Dimension(n, _) => n,
@ -419,9 +419,9 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(Color::from_hsla(
Ok(Value::Color(Box::new(Color::from_hsla(
hue, saturation, lightness, alpha,
)))
))))
}
}),
);
@ -494,7 +494,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.adjust_hue(degrees)))
Ok(Value::Color(Box::new(color.adjust_hue(degrees))))
}),
);
f.insert(
@ -524,7 +524,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.lighten(amount)))
Ok(Value::Color(Box::new(color.lighten(amount))))
}),
);
f.insert(
@ -554,7 +554,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.darken(amount)))
Ok(Value::Color(Box::new(color.darken(amount))))
}),
);
f.insert(
@ -601,7 +601,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.saturate(amount)))
Ok(Value::Color(Box::new(color.saturate(amount))))
}),
);
f.insert(
@ -631,7 +631,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.desaturate(amount)))
Ok(Value::Color(Box::new(color.desaturate(amount))))
}),
);
f.insert(
@ -654,7 +654,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.desaturate(Number::one())))
Ok(Value::Color(Box::new(color.desaturate(Number::one()))))
}),
);
f.insert(
@ -671,7 +671,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.complement()))
Ok(Value::Color(Box::new(color.complement())))
}),
);
f.insert(
@ -698,7 +698,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
}
};
match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => Ok(Value::Color(c.invert(weight))),
Value::Color(c) => Ok(Value::Color(Box::new(c.invert(weight)))),
Value::Dimension(n, Unit::Percent) => {
Ok(Value::Ident(format!("invert({}%)", n), QuoteKind::None))
}

View File

@ -66,7 +66,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.fade_in(amount)))
Ok(Value::Color(Box::new(color.fade_in(amount))))
}),
);
f.insert(
@ -96,7 +96,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.fade_in(amount)))
Ok(Value::Color(Box::new(color.fade_in(amount))))
}),
);
f.insert(
@ -126,7 +126,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.fade_out(amount)))
Ok(Value::Color(Box::new(color.fade_out(amount))))
}),
);
f.insert(
@ -156,7 +156,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.fade_out(amount)))
Ok(Value::Color(Box::new(color.fade_out(amount))))
}),
);
}

View File

@ -71,7 +71,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
opt_rgba!(args, blue, "blue", 0, 255, scope, super_selector);
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Color::from_rgba(red.unwrap_or(color.red()), green.unwrap_or(color.green()), blue.unwrap_or(color.blue()), alpha.unwrap_or(color.alpha()))))
return Ok(Value::Color(Box::new(Color::from_rgba(red.unwrap_or(color.red()), green.unwrap_or(color.green()), blue.unwrap_or(color.blue()), alpha.unwrap_or(color.alpha())))))
}
let hue = match named_arg!(args, scope, super_selector, "hue"=Value::Null) {
@ -88,11 +88,11 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
if hue.is_some() || saturation.is_some() || luminance.is_some() {
// Color::as_hsla() returns more exact values than Color::hue(), etc.
let (this_hue, this_saturation, this_luminance, this_alpha) = color.as_hsla();
return Ok(Value::Color(Color::from_hsla(hue.unwrap_or(this_hue), saturation.unwrap_or(this_saturation), luminance.unwrap_or(this_luminance), alpha.unwrap_or(this_alpha))))
return Ok(Value::Color(Box::new(Color::from_hsla(hue.unwrap_or(this_hue), saturation.unwrap_or(this_saturation), luminance.unwrap_or(this_luminance), alpha.unwrap_or(this_alpha)))))
}
Ok(Value::Color(if let Some(a) = alpha {
color.with_alpha(a)
Box::new(color.with_alpha(a))
} else {
color
}))
@ -117,12 +117,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
opt_rgba!(args, blue, "blue", -255, 255, scope, super_selector);
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Color::from_rgba(
return Ok(Value::Color(Box::new(Color::from_rgba(
color.red() + red.unwrap_or(Number::zero()),
color.green() + green.unwrap_or(Number::zero()),
color.blue() + blue.unwrap_or(Number::zero()),
color.alpha() + alpha.unwrap_or(Number::zero()),
)));
))));
}
let hue = match named_arg!(args, scope, super_selector, "hue" = Value::Null) {
@ -159,17 +159,17 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
if hue.is_some() || saturation.is_some() || luminance.is_some() {
// Color::as_hsla() returns more exact values than Color::hue(), etc.
let (this_hue, this_saturation, this_luminance, this_alpha) = color.as_hsla();
return Ok(Value::Color(Color::from_hsla(
return Ok(Value::Color(Box::new(Color::from_hsla(
this_hue + hue.unwrap_or(Number::zero()),
this_saturation + saturation.unwrap_or(Number::zero()),
this_luminance + luminance.unwrap_or(Number::zero()),
this_alpha + alpha.unwrap_or(Number::zero()),
)));
))));
}
Ok(Value::Color(if let Some(a) = alpha {
let temp_alpha = color.alpha();
color.with_alpha(temp_alpha + a)
Box::new(color.with_alpha(temp_alpha + a))
} else {
color
}))
@ -211,7 +211,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
opt_scale_arg!(args, blue, "blue", -100, 100, scope, super_selector);
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Color::from_rgba(
return Ok(Value::Color(Box::new(Color::from_rgba(
scale(
color.red(),
red.unwrap_or(Number::zero()),
@ -232,7 +232,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
alpha.unwrap_or(Number::zero()),
Number::one(),
),
)));
))));
}
opt_scale_arg!(args, saturation, "saturation", -100, 100, scope, super_selector);
@ -241,7 +241,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
if saturation.is_some() || luminance.is_some() {
// Color::as_hsla() returns more exact values than Color::hue(), etc.
let (this_hue, this_saturation, this_luminance, this_alpha) = color.as_hsla();
return Ok(Value::Color(Color::from_hsla(
return Ok(Value::Color(Box::new(Color::from_hsla(
scale(this_hue, Number::zero(), Number::from(360)),
scale(
this_saturation,
@ -254,12 +254,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
Number::one(),
),
scale(this_alpha, alpha.unwrap_or(Number::zero()), Number::one()),
)));
))));
}
Ok(Value::Color(if let Some(a) = alpha {
let temp_alpha = color.alpha();
color.with_alpha(scale(temp_alpha, a, Number::one()))
Box::new(color.with_alpha(scale(temp_alpha, a, Number::one())))
} else {
color
}))

View File

@ -116,7 +116,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
let color = Color::from_rgba(red, green, blue, Number::one());
Ok(Value::Color(color))
Ok(Value::Color(Box::new(color)))
} else if args.len() == 2 {
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -172,7 +172,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.with_alpha(alpha)))
Ok(Value::Color(Box::new(color.with_alpha(alpha))))
} else {
let red = match arg!(args, scope, super_selector, 0, "red") {
Value::Dimension(n, Unit::None) => n,
@ -330,7 +330,9 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(Color::from_rgba(red, green, blue, alpha)))
Ok(Value::Color(Box::new(Color::from_rgba(
red, green, blue, alpha,
))))
}
}),
);
@ -441,7 +443,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
let color = Color::from_rgba(red, green, blue, Number::one());
Ok(Value::Color(color))
Ok(Value::Color(Box::new(color)))
} else if args.len() == 2 {
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -497,7 +499,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color.with_alpha(alpha)))
Ok(Value::Color(Box::new(color.with_alpha(alpha))))
} else {
let red = match arg!(args, scope, super_selector, 0, "red") {
Value::Dimension(n, Unit::None) => n,
@ -655,7 +657,9 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(Color::from_rgba(red, green, blue, alpha)))
Ok(Value::Color(Box::new(Color::from_rgba(
red, green, blue, alpha,
))))
}
}),
);
@ -746,7 +750,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
.into())
}
};
Ok(Value::Color(color1.mix(&color2, weight)))
Ok(Value::Color(Box::new(color1.mix(&color2, weight))))
}),
);
}

View File

@ -29,7 +29,7 @@ pub(crate) enum Value {
Null,
Dimension(Number, Unit),
List(Vec<Value>, ListSeparator, Brackets),
Color(Color),
Color(Box<Color>),
UnaryOp(Op, Box<Value>),
BinaryOp(Box<Value>, Op, Box<Value>),
Paren(Box<Value>),

View File

@ -73,7 +73,10 @@ fn parse_hex<I: Iterator<Item = Token>>(
let red = (((v & 0xf00) >> 8) * 0x11) as u8;
let green = (((v & 0x0f0) >> 4) * 0x11) as u8;
let blue = ((v & 0x00f) * 0x11) as u8;
Ok(Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))).span(span))
Ok(
Value::Color(Box::new(Color::new(red, green, blue, 1, format!("#{}", s))))
.span(span),
)
}
4 => {
let v = match u16::from_str_radix(&s, 16) {
@ -84,7 +87,14 @@ fn parse_hex<I: Iterator<Item = Token>>(
let green = (((v & 0x0f00) >> 8) * 0x11) as u8;
let blue = (((v & 0x00f0) >> 4) * 0x11) as u8;
let alpha = ((v & 0x000f) * 0x11) as u8;
Ok(Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s))).span(span))
Ok(Value::Color(Box::new(Color::new(
red,
green,
blue,
alpha,
format!("#{}", s),
)))
.span(span))
}
6 => {
let v = match u32::from_str_radix(&s, 16) {
@ -94,7 +104,10 @@ fn parse_hex<I: Iterator<Item = Token>>(
let red = ((v & 0x00ff_0000) >> 16) as u8;
let green = ((v & 0x0000_ff00) >> 8) as u8;
let blue = (v & 0x0000_00ff) as u8;
Ok(Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))).span(span))
Ok(
Value::Color(Box::new(Color::new(red, green, blue, 1, format!("#{}", s))))
.span(span),
)
}
8 => {
let v = match u32::from_str_radix(&s, 16) {
@ -105,7 +118,14 @@ fn parse_hex<I: Iterator<Item = Token>>(
let green = ((v & 0x00ff_0000) >> 16) as u8;
let blue = ((v & 0x0000_ff00) >> 8) as u8;
let alpha = (v & 0x0000_00ff) as u8;
Ok(Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s))).span(span))
Ok(Value::Color(Box::new(Color::new(
red,
green,
blue,
alpha,
format!("#{}", s),
)))
.span(span))
}
_ => Err(("Expected hex digit.", span).into()),
}
@ -549,7 +569,7 @@ impl Value {
_ => {
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {
Ok(IntermediateValue::Value(Spanned {
node: Value::Color(c.into_color(s)),
node: Value::Color(Box::new(c.into_color(s))),
span,
}))
} else {