From df291313f96d81036acd248e7e899c68b1163add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Gelji=C4=87?= Date: Tue, 28 Aug 2018 21:02:02 +0200 Subject: [PATCH] Add support for transparent color value to several functions (color components, alpha, adjust-color) #319 * gracefully return values as per http://www.w3.org/TR/css3-color/#transparent-def whenever possible --- .../AdjustColorFunctionGenerator.java | 2 +- .../function/AlphaFunctionGenerator.java | 4 +++- .../ColorComponentFunctionGenerator.java | 2 +- .../vaadin/sass/internal/util/ColorUtil.java | 22 +++++++++++++++++-- .../resources/automatic/css/adjust-color.css | 5 +++++ .../css/color-component-functions.css | 12 ++++++++++ .../resources/automatic/css/scale-color.css | 5 +++++ .../automatic/scss/adjust-color.scss | 5 +++++ .../scss/color-component-functions.scss | 13 +++++++++++ .../resources/automatic/scss/scale-color.scss | 5 +++++ 10 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/vaadin/sass/internal/parser/function/AdjustColorFunctionGenerator.java b/src/main/java/com/vaadin/sass/internal/parser/function/AdjustColorFunctionGenerator.java index 4bd0f5a5..2b2c97f6 100644 --- a/src/main/java/com/vaadin/sass/internal/parser/function/AdjustColorFunctionGenerator.java +++ b/src/main/java/com/vaadin/sass/internal/parser/function/AdjustColorFunctionGenerator.java @@ -151,7 +151,7 @@ private LexicalUnitImpl getColor(LexicalUnitImpl function, } LexicalUnitImpl result = (LexicalUnitImpl) resultItem; if (!ColorUtil.isColor(result) && !ColorUtil.isRgba(result) - && !ColorUtil.isHsla(result)) { + && !ColorUtil.isHsla(result) && !ColorUtil.isTransparent(result)) { throw new ParseException( "The color argument must represent a valid color", function); } diff --git a/src/main/java/com/vaadin/sass/internal/parser/function/AlphaFunctionGenerator.java b/src/main/java/com/vaadin/sass/internal/parser/function/AlphaFunctionGenerator.java index d102364a..cbbb5996 100644 --- a/src/main/java/com/vaadin/sass/internal/parser/function/AlphaFunctionGenerator.java +++ b/src/main/java/com/vaadin/sass/internal/parser/function/AlphaFunctionGenerator.java @@ -42,6 +42,8 @@ protected SassListItem computeForArgumentList(LexicalUnitImpl function, ActualArgumentList parameterList = color.getParameterList(); SassListItem last = parameterList.get(parameterList.size() - 1); opacity = ((LexicalUnitImpl) last).getFloatValue(); + } else if (ColorUtil.isTransparent(color)) { + opacity = 0f; } return LexicalUnitImpl.createNumber(function.getLineNumber(), function.getColumnNumber(), opacity); @@ -52,7 +54,7 @@ private void checkParameters(LexicalUnitImpl function, LexicalUnitImpl color = (LexicalUnitImpl) getParam(args, "color"); if (!(color instanceof LexicalUnitImpl) || (!ColorUtil.isColor(color) && !ColorUtil.isRgba(color) && !ColorUtil - .isHsla(color))) { + .isHsla(color)) && !ColorUtil.isTransparent(color)) { throw new ParseException("The function " + function.getFunctionName() + " requires a color as its first parameter", function); diff --git a/src/main/java/com/vaadin/sass/internal/parser/function/ColorComponentFunctionGenerator.java b/src/main/java/com/vaadin/sass/internal/parser/function/ColorComponentFunctionGenerator.java index b92d5c7c..4ad2026c 100644 --- a/src/main/java/com/vaadin/sass/internal/parser/function/ColorComponentFunctionGenerator.java +++ b/src/main/java/com/vaadin/sass/internal/parser/function/ColorComponentFunctionGenerator.java @@ -80,7 +80,7 @@ private void checkParameters(LexicalUnitImpl function, } LexicalUnitImpl firstParam = (LexicalUnitImpl) arg; if (!ColorUtil.isColor(firstParam) && !ColorUtil.isRgba(firstParam) - && !ColorUtil.isHsla(firstParam)) { + && !ColorUtil.isHsla(firstParam) && !ColorUtil.isTransparent(firstParam)) { throw new ParseException("The parameter of the function " + function.getFunctionName() + " must be a valid color", function); diff --git a/src/main/java/com/vaadin/sass/internal/util/ColorUtil.java b/src/main/java/com/vaadin/sass/internal/util/ColorUtil.java index 7790ca82..414425ca 100644 --- a/src/main/java/com/vaadin/sass/internal/util/ColorUtil.java +++ b/src/main/java/com/vaadin/sass/internal/util/ColorUtil.java @@ -35,6 +35,7 @@ public class ColorUtil { .compile("#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})"); private static Map colorNameToHex = new HashMap(); private static Map hexToColorName = new HashMap(); + private static String transparent = "transparent"; static { colorNameToHex.put("aqua", "#00ffff"); @@ -168,6 +169,18 @@ public static boolean isHslColor(LexicalUnitImpl unit) { && unit.getParameterList().size() == 3; } + /** + * Returns true if the lexical unit represents the transparent keyword, false + * otherwise. + * + * @param unit lexical unit + * @return true if unit represents the transparent color + */ + public static boolean isTransparent(LexicalUnitImpl unit) { + return unit.getLexicalUnitType() == LexicalUnit.SAC_IDENT + && transparent.equals(unit.getStringValue()); + } + /** * Returns the alpha component of the color. For colors that do not have an * explicit alpha component, returns 1. @@ -177,7 +190,9 @@ public static boolean isHslColor(LexicalUnitImpl unit) { * @return The alpha component of color. */ public static float getAlpha(LexicalUnitImpl color) { - if (isHsla(color) || isRgba(color)) { + if (isTransparent(color)) { + return 0; + } else if (isHsla(color) || isRgba(color)) { ActualArgumentList params = color.getParameterList(); return params.get(params.size() - 1).getContainedValue() .getFloatValue(); @@ -198,7 +213,10 @@ public static float getAlpha(LexicalUnitImpl color) { * @return RGB components or null if not a color */ public static int[] colorToRgb(LexicalUnitImpl color) { - if (isRgba(color)) { + if (isTransparent(color)) { + // as per https://www.w3.org/TR/css-color-3/#transparent-def + return new int[] { 0, 0, 0, 0 }; + } else if (isRgba(color)) { if (color.getParameterList().size() == 2 && color.getParameterList().get(0) instanceof LexicalUnitImpl) { return colorToRgb((LexicalUnitImpl) color.getParameterList() diff --git a/src/test/resources/automatic/css/adjust-color.css b/src/test/resources/automatic/css/adjust-color.css index 2f76698b..07c7adfa 100644 --- a/src/test/resources/automatic/css/adjust-color.css +++ b/src/test/resources/automatic/css/adjust-color.css @@ -37,4 +37,9 @@ .hsla { rb: rgba(41, 112, 127, 0.5); sla: rgba(45, 134, 134, 0.25); +} + +.transparent { + t1: transparent; + t2: rgba(0, 0, 5, 0); } \ No newline at end of file diff --git a/src/test/resources/automatic/css/color-component-functions.css b/src/test/resources/automatic/css/color-component-functions.css index 49997553..e30c8160 100644 --- a/src/test/resources/automatic/css/color-component-functions.css +++ b/src/test/resources/automatic/css/color-component-functions.css @@ -41,4 +41,16 @@ alpha: 1; opacity: 1; alpha2: 0.5; +} + +.transparent{ + red: 0; + green: 0; + blue: 0; + alpha: 0; + opacity: 0; + colorA: rgba(0, 0, 0, 0); + colorB: transparent; + colorC: rgba(0, 0, 0, 0); + colorD: rgba(255, 255, 255, 0); } \ No newline at end of file diff --git a/src/test/resources/automatic/css/scale-color.css b/src/test/resources/automatic/css/scale-color.css index 131fc352..1ff1f72c 100644 --- a/src/test/resources/automatic/css/scale-color.css +++ b/src/test/resources/automatic/css/scale-color.css @@ -33,4 +33,9 @@ rb: rgba(75, 112, 133, 0.5); sla: rgba(36, 129, 129, 0.375); ga: rgba(16, 56, 112, 0.75); +} + +.transparent { + t1: transparent; + t2: rgba(0, 0, 12, 0); } \ No newline at end of file diff --git a/src/test/resources/automatic/scss/adjust-color.scss b/src/test/resources/automatic/scss/adjust-color.scss index 0c18b8d7..b47c33b0 100644 --- a/src/test/resources/automatic/scss/adjust-color.scss +++ b/src/test/resources/automatic/scss/adjust-color.scss @@ -51,4 +51,9 @@ $hsla: hsla(180, 75%, 25%, 0.5); rb: adjust-color($hsla, $red: 25, $blue: 15); sla: adjust-color($hsla, $saturation: -25%, $lightness: 10%, $alpha: -0.25); +} + +.transparent { + t1: adjust-color(transparent); + t2: adjust-color(transparent, $blue: 5); } \ No newline at end of file diff --git a/src/test/resources/automatic/scss/color-component-functions.scss b/src/test/resources/automatic/scss/color-component-functions.scss index 12e2ed85..92b2235c 100644 --- a/src/test/resources/automatic/scss/color-component-functions.scss +++ b/src/test/resources/automatic/scss/color-component-functions.scss @@ -52,4 +52,17 @@ $blue:3; alpha: alpha($hslcolor); opacity: opacity($hslcolor); alpha2: alpha($hslacolor); +} + +.transparent{ + $transparentcolor: transparent; + red: red($transparentcolor); + green: green($transparentcolor); + blue: blue($transparentcolor); + alpha: alpha($transparentcolor); + opacity: opacity($transparentcolor); + colorA: rgba(0,0,0,0); + colorB: $transparentcolor; + colorC: darken($transparentcolor, 50); + colorD: lighten($transparentcolor, 100); } \ No newline at end of file diff --git a/src/test/resources/automatic/scss/scale-color.scss b/src/test/resources/automatic/scss/scale-color.scss index 473c1c87..b9da30fd 100644 --- a/src/test/resources/automatic/scss/scale-color.scss +++ b/src/test/resources/automatic/scss/scale-color.scss @@ -44,4 +44,9 @@ rb: scale-color($hsla, $red: 25%, $blue: 15%); sla: scale-color($hsla, $saturation: -25%, $lightness: 10%, $alpha: -25%); ga: scale-color($color: $hsla, $green: -50%, $alpha: 50%); +} + +.transparent { + t1: scale-color(transparent); + t2: scale-color(transparent, $blue: 5%); } \ No newline at end of file