Skip to content

Commit 85dcde9

Browse files
committed
added gray() as an alias of lab(), note #csswg-drafts/635 about allowing percentages here
1 parent 80c6651 commit 85dcde9

4 files changed

Lines changed: 108 additions & 22 deletions

File tree

org/w3c/css/parser/analyzer/CssParser.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6114,6 +6114,9 @@ final public boolean prio() throws ParseException {
61146114
} else if (funcname.equals("lab(")) {
61156115
color.setLABColor(exp, ac);
61166116
{if ("" != null) return color;}
6117+
} else if (funcname.equals("gray(")) {
6118+
color.setGrayColor(exp, ac);
6119+
{if ("" != null) return color;}
61176120
} else if (funcname.equals("lch(")) {
61186121
color.setLCHColor(exp, ac);
61196122
{if ("" != null) return color;}
@@ -7029,26 +7032,26 @@ private boolean jj_3R_216()
70297032
return false;
70307033
}
70317034

7032-
private boolean jj_3R_197()
7035+
private boolean jj_3R_214()
70337036
{
7034-
if (jj_scan_token(DIMEN)) return true;
7037+
Token xsp;
7038+
xsp = jj_scanpos;
7039+
if (jj_scan_token(39)) {
7040+
jj_scanpos = xsp;
7041+
if (jj_scan_token(40)) return true;
7042+
}
70357043
return false;
70367044
}
70377045

7038-
private boolean jj_3R_196()
7046+
private boolean jj_3R_197()
70397047
{
7040-
if (jj_scan_token(SPL)) return true;
7048+
if (jj_scan_token(DIMEN)) return true;
70417049
return false;
70427050
}
70437051

7044-
private boolean jj_3R_214()
7052+
private boolean jj_3R_196()
70457053
{
7046-
Token xsp;
7047-
xsp = jj_scanpos;
7048-
if (jj_scan_token(39)) {
7049-
jj_scanpos = xsp;
7050-
if (jj_scan_token(40)) return true;
7051-
}
7054+
if (jj_scan_token(SPL)) return true;
70527055
return false;
70537056
}
70547057

@@ -7118,15 +7121,15 @@ private boolean jj_3R_185()
71187121
return false;
71197122
}
71207123

7121-
private boolean jj_3R_184()
7124+
private boolean jj_3R_217()
71227125
{
7123-
if (jj_3R_165()) return true;
7126+
if (jj_scan_token(IDENT)) return true;
71247127
return false;
71257128
}
71267129

7127-
private boolean jj_3R_217()
7130+
private boolean jj_3R_184()
71287131
{
7129-
if (jj_scan_token(IDENT)) return true;
7132+
if (jj_3R_165()) return true;
71307133
return false;
71317134
}
71327135

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,6 +3054,9 @@ CssValue function() :
30543054
} else if (funcname.equals("lab(")) {
30553055
color.setLABColor(exp, ac);
30563056
return color;
3057+
} else if (funcname.equals("gray(")) {
3058+
color.setGrayColor(exp, ac);
3059+
return color;
30573060
} else if (funcname.equals("lch(")) {
30583061
color.setLCHColor(exp, ac);
30593062
return color;

org/w3c/css/values/CssColor.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.w3c.css.util.CssVersion;
1212
import org.w3c.css.util.InvalidParamException;
1313

14+
import java.math.BigDecimal;
1415
import java.util.HashMap;
1516

1617
import static org.w3c.css.values.CssOperator.COMMA;
@@ -913,6 +914,68 @@ public void setLABColor(CssExpression exp, ApplContext ac)
913914
}
914915
}
915916

917+
public void setGrayColor(CssExpression exp, ApplContext ac)
918+
throws InvalidParamException {
919+
// HWB defined in CSSColor Level 4 and onward, currently used in the CSS level
920+
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
921+
StringBuilder sb = new StringBuilder();
922+
sb.append("gray(").append(exp.toStringFromStart()).append(')');
923+
throw new InvalidParamException("notversion", sb.toString(),
924+
ac.getCssVersionString(), ac);
925+
}
926+
927+
color = null;
928+
lab = new LAB();
929+
lab.setGray(true);
930+
CssValue val = exp.getValue();
931+
char op = exp.getOperator();
932+
// L
933+
if (val == null) {
934+
throw new InvalidParamException("invalid-color", ac);
935+
}
936+
switch (val.getType()) {
937+
case CssTypes.CSS_NUMBER:
938+
lab.setL(ac, val);
939+
break;
940+
default:
941+
throw new InvalidParamException("rgb", val, ac); // FIXME gray
942+
}
943+
944+
// A
945+
CssNumber n = new CssNumber();
946+
n.setValue(BigDecimal.ZERO);
947+
lab.setA(ac, n);
948+
// B
949+
lab.setB(ac, n);
950+
951+
exp.next();
952+
if (!exp.end()) {
953+
if (op != COMMA) {
954+
throw new InvalidParamException("operator", exp.toStringFromStart(), ac);
955+
}
956+
// Alpha
957+
val = exp.getValue();
958+
if (val == null) {
959+
throw new InvalidParamException("invalid-color", exp.toStringFromStart(), ac);
960+
}
961+
switch (val.getType()) {
962+
case CssTypes.CSS_NUMBER:
963+
case CssTypes.CSS_PERCENTAGE:
964+
lab.setAlpha(ac, val);
965+
break;
966+
default:
967+
exp.starts();
968+
throw new InvalidParamException("rgb", val, ac); // FIXME gray
969+
}
970+
exp.next();
971+
}
972+
// extra values?
973+
if (!exp.end()) {
974+
exp.starts();
975+
throw new InvalidParamException("rgb", exp.toStringFromStart(), ac);
976+
}
977+
}
978+
916979

917980
public void setLCHColor(CssExpression exp, ApplContext ac)
918981
throws InvalidParamException {
@@ -997,5 +1060,6 @@ public void setLCHColor(CssExpression exp, ApplContext ac)
9971060
}
9981061
}
9991062

1063+
10001064
}
10011065

org/w3c/css/values/LAB.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class LAB {
1313
String output = null;
1414
CssValue vl, va, vb, alpha;
1515
boolean faSet = false;
16+
boolean isGray = false;
1617

1718
/**
1819
* Create a new LAB
@@ -77,20 +78,35 @@ public final void setAlpha(ApplContext ac, CssValue val)
7778
alpha = RGBA.filterAlpha(ac, val);
7879
}
7980

81+
public final boolean setGray(boolean isGray) {
82+
this.isGray = isGray;
83+
return isGray;
84+
}
85+
8086

8187
/**
8288
* Returns a string representation of the object.
8389
*/
8490
public String toString() {
8591
if (output == null) {
86-
StringBuilder sb = new StringBuilder("lab(");
87-
sb.append(vl).append(" ");
88-
sb.append(va).append(" ");
89-
sb.append(vb);
90-
if (faSet) {
91-
sb.append(", ").append(alpha);
92+
StringBuilder sb;
93+
if (isGray) {
94+
sb = new StringBuilder("gray(");
95+
sb.append(vl);
96+
if (faSet) {
97+
sb.append(", ").append(alpha);
98+
}
99+
sb.append(')');
100+
} else {
101+
sb = new StringBuilder("lab(");
102+
sb.append(vl).append(' ');
103+
sb.append(va).append(' ');
104+
sb.append(vb);
105+
if (faSet) {
106+
sb.append(", ").append(alpha);
107+
}
108+
sb.append(')');
92109
}
93-
sb.append(")");
94110
output = sb.toString();
95111
}
96112
return output;

0 commit comments

Comments
 (0)