Skip to content

Commit 90e0999

Browse files
committed
added LAB and LCH values, default for handling out of bound is currently clipping and raising a warning, but it is not yet decided in https://www.w3.org/TR/2016/WD-css-color-4-20160705/#specifying-lab-lch not that the parsing is not there yet
1 parent afb76be commit 90e0999

2 files changed

Lines changed: 241 additions & 0 deletions

File tree

org/w3c/css/values/LAB.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio University, Beihang University 2018.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
//
7+
package org.w3c.css.values;
8+
9+
import org.w3c.css.util.ApplContext;
10+
import org.w3c.css.util.InvalidParamException;
11+
12+
import java.math.BigDecimal;
13+
14+
public class LAB {
15+
String output = null;
16+
CssValue vl, va, vb, alpha;
17+
boolean faSet = false;
18+
19+
/**
20+
* Create a new HSL
21+
*/
22+
public LAB() {
23+
}
24+
25+
public static final CssValue filterL(ApplContext ac, CssValue val)
26+
throws InvalidParamException {
27+
if (val.getRawType() == CssTypes.CSS_CALC) {
28+
// TODO add warning about uncheckability
29+
// might need to extend...
30+
} else {
31+
if (val.getType() == CssTypes.CSS_NUMBER) {
32+
CssCheckableValue v = val.getCheckableValue();
33+
if (!v.isPositive()) {
34+
ac.getFrame().addWarning("out-of-range", val.toString());
35+
CssNumber nb = new CssNumber();
36+
nb.setIntValue(0);
37+
return nb;
38+
}
39+
if (val.getRawType() == CssTypes.CSS_NUMBER) {
40+
BigDecimal pp = ((CssNumber) val).value;
41+
if (pp.compareTo(HWB.s100) > 0) {
42+
ac.getFrame().addWarning("out-of-range", val.toString());
43+
CssNumber nb = new CssNumber();
44+
nb.setIntValue(100);
45+
return nb;
46+
}
47+
}
48+
}
49+
}
50+
return val;
51+
}
52+
53+
public final void setL(ApplContext ac, CssValue val)
54+
throws InvalidParamException {
55+
output = null;
56+
vl = filterL(ac, val);
57+
58+
}
59+
60+
public final void setA(ApplContext ac, CssValue val)
61+
throws InvalidParamException {
62+
output = null;
63+
va = val;
64+
}
65+
66+
public final void setB(ApplContext ac, CssValue val)
67+
throws InvalidParamException {
68+
output = null;
69+
vb = val;
70+
}
71+
72+
public final void setAlpha(ApplContext ac, CssValue val)
73+
throws InvalidParamException {
74+
output = null;
75+
faSet = true;
76+
alpha = RGBA.filterAlpha(ac, val);
77+
}
78+
79+
80+
/**
81+
* Returns a string representation of the object.
82+
*/
83+
public String toString() {
84+
if (output == null) {
85+
StringBuilder sb = new StringBuilder("lab(");
86+
sb.append(vl).append(" ");
87+
sb.append(va).append(" ");
88+
sb.append(vb);
89+
if (faSet) {
90+
sb.append(", ").append(alpha);
91+
}
92+
sb.append(")");
93+
output = sb.toString();
94+
}
95+
return output;
96+
}
97+
}

org/w3c/css/values/LCH.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// Author: Yves Lafon <ylafon@w3.org>
3+
//
4+
// (c) COPYRIGHT MIT, ERCIM, Keio University, Beihang University 2018.
5+
// Please first read the full copyright statement in file COPYRIGHT.html
6+
//
7+
package org.w3c.css.values;
8+
9+
import org.w3c.css.util.ApplContext;
10+
import org.w3c.css.util.InvalidParamException;
11+
12+
import java.math.BigDecimal;
13+
14+
public class LCH {
15+
String output = null;
16+
CssValue vl, vc, vh, alpha;
17+
boolean faSet = false;
18+
19+
/**
20+
* Create a new HSL
21+
*/
22+
public LCH() {
23+
}
24+
25+
public static final CssValue filterL(ApplContext ac, CssValue val)
26+
throws InvalidParamException {
27+
if (val.getRawType() == CssTypes.CSS_CALC) {
28+
// TODO add warning about uncheckability
29+
// might need to extend...
30+
} else {
31+
if (val.getType() == CssTypes.CSS_NUMBER) {
32+
CssCheckableValue v = val.getCheckableValue();
33+
if (!v.isPositive()) {
34+
ac.getFrame().addWarning("out-of-range", val.toString());
35+
CssNumber nb = new CssNumber();
36+
nb.setIntValue(0);
37+
return nb;
38+
}
39+
if (val.getRawType() == CssTypes.CSS_NUMBER) {
40+
BigDecimal pp = ((CssNumber) val).value;
41+
if (pp.compareTo(HWB.s100) > 0) {
42+
ac.getFrame().addWarning("out-of-range", val.toString());
43+
CssNumber nb = new CssNumber();
44+
nb.setValue(HWB.s100);
45+
return nb;
46+
}
47+
}
48+
}
49+
}
50+
return val;
51+
}
52+
53+
public static final CssValue filterC(ApplContext ac, CssValue val)
54+
throws InvalidParamException {
55+
if (val.getRawType() == CssTypes.CSS_CALC) {
56+
// TODO add warning about uncheckability
57+
// might need to extend...
58+
} else {
59+
if (val.getType() == CssTypes.CSS_NUMBER) {
60+
CssCheckableValue v = val.getCheckableValue();
61+
if (!v.isPositive()) {
62+
ac.getFrame().addWarning("out-of-range", val.toString());
63+
CssNumber nb = new CssNumber();
64+
nb.setValue(BigDecimal.ZERO);
65+
return nb;
66+
}
67+
}
68+
}
69+
return val;
70+
}
71+
72+
public static final CssValue filterH(ApplContext ac, CssValue val)
73+
throws InvalidParamException {
74+
if (val.getRawType() == CssTypes.CSS_CALC) {
75+
// TODO add warning about uncheckability
76+
// might need to extend...
77+
} else {
78+
if (val.getType() == CssTypes.CSS_NUMBER) {
79+
CssCheckableValue v = val.getCheckableValue();
80+
if (!v.isPositive()) {
81+
ac.getFrame().addWarning("out-of-range", val.toString());
82+
CssNumber nb = new CssNumber();
83+
nb.setIntValue(0);
84+
return nb;
85+
}
86+
if (val.getRawType() == CssTypes.CSS_NUMBER) {
87+
BigDecimal pp = ((CssNumber) val).value;
88+
if (pp.compareTo(CssAngle.deg360) > 0) {
89+
ac.getFrame().addWarning("out-of-range", val.toString());
90+
CssNumber nb = new CssNumber();
91+
nb.setValue(CssAngle.deg360);
92+
return nb;
93+
}
94+
}
95+
}
96+
}
97+
return val;
98+
}
99+
100+
public final void setL(ApplContext ac, CssValue val)
101+
throws InvalidParamException {
102+
output = null;
103+
vl = filterL(ac, val);
104+
105+
}
106+
107+
public final void setC(ApplContext ac, CssValue val)
108+
throws InvalidParamException {
109+
output = null;
110+
vc = filterC(ac, val);
111+
}
112+
113+
public final void setH(ApplContext ac, CssValue val)
114+
throws InvalidParamException {
115+
output = null;
116+
vh = filterH(ac, val);
117+
}
118+
119+
public final void setAlpha(ApplContext ac, CssValue val)
120+
throws InvalidParamException {
121+
output = null;
122+
faSet = true;
123+
alpha = RGBA.filterAlpha(ac, val);
124+
}
125+
126+
127+
/**
128+
* Returns a string representation of the object.
129+
*/
130+
public String toString() {
131+
if (output == null) {
132+
StringBuilder sb = new StringBuilder("lch(");
133+
sb.append(vl).append(" ");
134+
sb.append(vc).append(" ");
135+
sb.append(vh);
136+
if (faSet) {
137+
sb.append(", ").append(alpha);
138+
}
139+
sb.append(")");
140+
output = sb.toString();
141+
}
142+
return output;
143+
}
144+
}

0 commit comments

Comments
 (0)