Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Commit df29131

Browse files
committed
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
1 parent 3c8b2c0 commit df29131

File tree

10 files changed

+70
-5
lines changed

10 files changed

+70
-5
lines changed

src/main/java/com/vaadin/sass/internal/parser/function/AdjustColorFunctionGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private LexicalUnitImpl getColor(LexicalUnitImpl function,
151151
}
152152
LexicalUnitImpl result = (LexicalUnitImpl) resultItem;
153153
if (!ColorUtil.isColor(result) && !ColorUtil.isRgba(result)
154-
&& !ColorUtil.isHsla(result)) {
154+
&& !ColorUtil.isHsla(result) && !ColorUtil.isTransparent(result)) {
155155
throw new ParseException(
156156
"The color argument must represent a valid color", function);
157157
}

src/main/java/com/vaadin/sass/internal/parser/function/AlphaFunctionGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ protected SassListItem computeForArgumentList(LexicalUnitImpl function,
4242
ActualArgumentList parameterList = color.getParameterList();
4343
SassListItem last = parameterList.get(parameterList.size() - 1);
4444
opacity = ((LexicalUnitImpl) last).getFloatValue();
45+
} else if (ColorUtil.isTransparent(color)) {
46+
opacity = 0f;
4547
}
4648
return LexicalUnitImpl.createNumber(function.getLineNumber(),
4749
function.getColumnNumber(), opacity);
@@ -52,7 +54,7 @@ private void checkParameters(LexicalUnitImpl function,
5254
LexicalUnitImpl color = (LexicalUnitImpl) getParam(args, "color");
5355
if (!(color instanceof LexicalUnitImpl)
5456
|| (!ColorUtil.isColor(color) && !ColorUtil.isRgba(color) && !ColorUtil
55-
.isHsla(color))) {
57+
.isHsla(color)) && !ColorUtil.isTransparent(color)) {
5658
throw new ParseException("The function "
5759
+ function.getFunctionName()
5860
+ " requires a color as its first parameter", function);

src/main/java/com/vaadin/sass/internal/parser/function/ColorComponentFunctionGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void checkParameters(LexicalUnitImpl function,
8080
}
8181
LexicalUnitImpl firstParam = (LexicalUnitImpl) arg;
8282
if (!ColorUtil.isColor(firstParam) && !ColorUtil.isRgba(firstParam)
83-
&& !ColorUtil.isHsla(firstParam)) {
83+
&& !ColorUtil.isHsla(firstParam) && !ColorUtil.isTransparent(firstParam)) {
8484
throw new ParseException("The parameter of the function "
8585
+ function.getFunctionName() + " must be a valid color",
8686
function);

src/main/java/com/vaadin/sass/internal/util/ColorUtil.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ColorUtil {
3535
.compile("#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})");
3636
private static Map<String, String> colorNameToHex = new HashMap<String, String>();
3737
private static Map<String, String> hexToColorName = new HashMap<String, String>();
38+
private static String transparent = "transparent";
3839

3940
static {
4041
colorNameToHex.put("aqua", "#00ffff");
@@ -168,6 +169,18 @@ public static boolean isHslColor(LexicalUnitImpl unit) {
168169
&& unit.getParameterList().size() == 3;
169170
}
170171

172+
/**
173+
* Returns true if the lexical unit represents the transparent keyword, false
174+
* otherwise.
175+
*
176+
* @param unit lexical unit
177+
* @return true if unit represents the transparent color
178+
*/
179+
public static boolean isTransparent(LexicalUnitImpl unit) {
180+
return unit.getLexicalUnitType() == LexicalUnit.SAC_IDENT
181+
&& transparent.equals(unit.getStringValue());
182+
}
183+
171184
/**
172185
* Returns the alpha component of the color. For colors that do not have an
173186
* explicit alpha component, returns 1.
@@ -177,7 +190,9 @@ public static boolean isHslColor(LexicalUnitImpl unit) {
177190
* @return The alpha component of color.
178191
*/
179192
public static float getAlpha(LexicalUnitImpl color) {
180-
if (isHsla(color) || isRgba(color)) {
193+
if (isTransparent(color)) {
194+
return 0;
195+
} else if (isHsla(color) || isRgba(color)) {
181196
ActualArgumentList params = color.getParameterList();
182197
return params.get(params.size() - 1).getContainedValue()
183198
.getFloatValue();
@@ -198,7 +213,10 @@ public static float getAlpha(LexicalUnitImpl color) {
198213
* @return RGB components or null if not a color
199214
*/
200215
public static int[] colorToRgb(LexicalUnitImpl color) {
201-
if (isRgba(color)) {
216+
if (isTransparent(color)) {
217+
// as per https://www.w3.org/TR/css-color-3/#transparent-def
218+
return new int[] { 0, 0, 0, 0 };
219+
} else if (isRgba(color)) {
202220
if (color.getParameterList().size() == 2
203221
&& color.getParameterList().get(0) instanceof LexicalUnitImpl) {
204222
return colorToRgb((LexicalUnitImpl) color.getParameterList()

src/test/resources/automatic/css/adjust-color.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@
3737
.hsla {
3838
rb: rgba(41, 112, 127, 0.5);
3939
sla: rgba(45, 134, 134, 0.25);
40+
}
41+
42+
.transparent {
43+
t1: transparent;
44+
t2: rgba(0, 0, 5, 0);
4045
}

src/test/resources/automatic/css/color-component-functions.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@
4141
alpha: 1;
4242
opacity: 1;
4343
alpha2: 0.5;
44+
}
45+
46+
.transparent{
47+
red: 0;
48+
green: 0;
49+
blue: 0;
50+
alpha: 0;
51+
opacity: 0;
52+
colorA: rgba(0, 0, 0, 0);
53+
colorB: transparent;
54+
colorC: rgba(0, 0, 0, 0);
55+
colorD: rgba(255, 255, 255, 0);
4456
}

src/test/resources/automatic/css/scale-color.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@
3333
rb: rgba(75, 112, 133, 0.5);
3434
sla: rgba(36, 129, 129, 0.375);
3535
ga: rgba(16, 56, 112, 0.75);
36+
}
37+
38+
.transparent {
39+
t1: transparent;
40+
t2: rgba(0, 0, 12, 0);
3641
}

src/test/resources/automatic/scss/adjust-color.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@
5151
$hsla: hsla(180, 75%, 25%, 0.5);
5252
rb: adjust-color($hsla, $red: 25, $blue: 15);
5353
sla: adjust-color($hsla, $saturation: -25%, $lightness: 10%, $alpha: -0.25);
54+
}
55+
56+
.transparent {
57+
t1: adjust-color(transparent);
58+
t2: adjust-color(transparent, $blue: 5);
5459
}

src/test/resources/automatic/scss/color-component-functions.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,17 @@ $blue:3;
5252
alpha: alpha($hslcolor);
5353
opacity: opacity($hslcolor);
5454
alpha2: alpha($hslacolor);
55+
}
56+
57+
.transparent{
58+
$transparentcolor: transparent;
59+
red: red($transparentcolor);
60+
green: green($transparentcolor);
61+
blue: blue($transparentcolor);
62+
alpha: alpha($transparentcolor);
63+
opacity: opacity($transparentcolor);
64+
colorA: rgba(0,0,0,0);
65+
colorB: $transparentcolor;
66+
colorC: darken($transparentcolor, 50);
67+
colorD: lighten($transparentcolor, 100);
5568
}

src/test/resources/automatic/scss/scale-color.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@
4444
rb: scale-color($hsla, $red: 25%, $blue: 15%);
4545
sla: scale-color($hsla, $saturation: -25%, $lightness: 10%, $alpha: -25%);
4646
ga: scale-color($color: $hsla, $green: -50%, $alpha: 50%);
47+
}
48+
49+
.transparent {
50+
t1: scale-color(transparent);
51+
t2: scale-color(transparent, $blue: 5%);
4752
}

0 commit comments

Comments
 (0)