From 33ccabce7b81bde5c7773af8fa77a0a41c8f71c7 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Tue, 21 Apr 2020 18:54:19 -0400 Subject: [PATCH] box color in value --- src/builtin/color/hsl.rs | 32 ++++++++++++++++---------------- src/builtin/color/opacity.rs | 8 ++++---- src/builtin/color/other.rs | 26 +++++++++++++------------- src/builtin/color/rgb.rs | 18 +++++++++++------- src/value/mod.rs | 2 +- src/value/parse.rs | 30 +++++++++++++++++++++++++----- 6 files changed, 70 insertions(+), 46 deletions(-) diff --git a/src/builtin/color/hsl.rs b/src/builtin/color/hsl.rs index d7614e2..0759fbc 100644 --- a/src/builtin/color/hsl.rs +++ b/src/builtin/color/hsl.rs @@ -75,12 +75,12 @@ pub(crate) fn register(f: &mut HashMap) { 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) { .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) { 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) { .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) { .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) { .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) { .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) { .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) { .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) { .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) { .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) { } }; 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)) } diff --git a/src/builtin/color/opacity.rs b/src/builtin/color/opacity.rs index 636f6c1..6029ebb 100644 --- a/src/builtin/color/opacity.rs +++ b/src/builtin/color/opacity.rs @@ -66,7 +66,7 @@ pub(crate) fn register(f: &mut HashMap) { .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) { .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) { .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) { .into()) } }; - Ok(Value::Color(color.fade_out(amount))) + Ok(Value::Color(Box::new(color.fade_out(amount)))) }), ); } diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs index 30e30cd..0aa73fa 100644 --- a/src/builtin/color/other.rs +++ b/src/builtin/color/other.rs @@ -71,7 +71,7 @@ pub(crate) fn register(f: &mut HashMap) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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 })) diff --git a/src/builtin/color/rgb.rs b/src/builtin/color/rgb.rs index 72bbfe9..097386c 100644 --- a/src/builtin/color/rgb.rs +++ b/src/builtin/color/rgb.rs @@ -116,7 +116,7 @@ pub(crate) fn register(f: &mut HashMap) { 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) { .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) { .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) { 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) { .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) { .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) { .into()) } }; - Ok(Value::Color(color1.mix(&color2, weight))) + Ok(Value::Color(Box::new(color1.mix(&color2, weight)))) }), ); } diff --git a/src/value/mod.rs b/src/value/mod.rs index cdc0d05..8f449f7 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -29,7 +29,7 @@ pub(crate) enum Value { Null, Dimension(Number, Unit), List(Vec, ListSeparator, Brackets), - Color(Color), + Color(Box), UnaryOp(Op, Box), BinaryOp(Box, Op, Box), Paren(Box), diff --git a/src/value/parse.rs b/src/value/parse.rs index d179ad1..41fafc6 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -73,7 +73,10 @@ fn parse_hex>( 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>( 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>( 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>( 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 {