Skip to content

Commit 3a83508

Browse files
committed
support oklab and oklch color declaration
1 parent 2a5e3d8 commit 3a83508

12 files changed

Lines changed: 272 additions & 97 deletions

File tree

src/main/java/org/htmlunit/cssparser/dom/HSLColorImpl.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class HSLColorImpl implements Serializable {
4444
*/
4545
public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMException {
4646
if (function == null) {
47-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space hsl or hsla is required.");
47+
throw new DOMException(DOMException.SYNTAX_ERR, "Color space 'hsl' or 'hsla' is required.");
4848
}
4949
final String functionLC = function.toLowerCase(Locale.ROOT);
5050
if (!"hsl".equals(functionLC) && !"hsla".equals(functionLC)) {
@@ -54,30 +54,30 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5454

5555
LexicalUnit next = lu;
5656
if (next == null) {
57-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
57+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5858
}
5959
hue_ = getHuePart(next);
6060

6161
next = next.getNextLexicalUnit();
6262
if (next == null) {
63-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
63+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6464
}
6565

6666
commaSeparated_ = LexicalUnitType.OPERATOR_COMMA == next.getLexicalUnitType();
6767
if (commaSeparated_) {
6868
if (hue_.getLexicalUnitType() == LexicalUnitType.NONE) {
6969
throw new DOMException(DOMException.SYNTAX_ERR,
70-
function_ + " has to use blank as separator if none is used.");
70+
"'" + function_ + "' has to use blank as separator if none is used.");
7171
}
7272

7373
next = next.getNextLexicalUnit();
7474
if (next == null) {
75-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
75+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
7676
}
7777

7878
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
7979
throw new DOMException(DOMException.SYNTAX_ERR,
80-
function_ + " has to use blank as separator if none is used.");
80+
"'" + function_ + "' has to use blank as separator if none is used.");
8181
}
8282
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
8383
throw new DOMException(DOMException.SYNTAX_ERR, "Saturation part has to be percentage.");
@@ -86,21 +86,22 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8686

8787
next = next.getNextLexicalUnit();
8888
if (next == null) {
89-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
89+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
9090
}
9191

9292
if (LexicalUnitType.OPERATOR_COMMA != next.getLexicalUnitType()) {
93-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " parameters must be separated by ','.");
93+
throw new DOMException(DOMException.SYNTAX_ERR,
94+
"'" + function_ + "' parameters must be separated by ','.");
9495
}
9596

9697
next = next.getNextLexicalUnit();
9798
if (next == null) {
98-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
99+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
99100
}
100101

101102
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
102103
throw new DOMException(DOMException.SYNTAX_ERR,
103-
function_ + " has to use blank as separator if none is used.");
104+
"'" + function_ + "' has to use blank as separator if none is used.");
104105
}
105106
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
106107
throw new DOMException(DOMException.SYNTAX_ERR, "Lightness part has to be percentage.");
@@ -113,7 +114,8 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
113114
}
114115

115116
if (LexicalUnitType.OPERATOR_COMMA != next.getLexicalUnitType()) {
116-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " parameters must be separated by ','.");
117+
throw new DOMException(DOMException.SYNTAX_ERR,
118+
"'" + function_ + "' parameters must be separated by ','.");
117119
}
118120

119121
next = next.getNextLexicalUnit();
@@ -123,12 +125,13 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
123125

124126
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
125127
throw new DOMException(DOMException.SYNTAX_ERR,
126-
function_ + " has to use blank as separator if none is used.");
128+
"'" + function_ + "' has to use blank as separator if none is used.");
127129
}
128130
alpha_ = getAlphaPart(next);
129131
next = next.getNextLexicalUnit();
130132
if (next != null) {
131-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for " + function_ + " function.");
133+
throw new DOMException(DOMException.SYNTAX_ERR,
134+
"Too many parameters for '" + function_ + "' function.");
132135
}
133136
return;
134137
}
@@ -141,11 +144,11 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
141144

142145
next = next.getNextLexicalUnit();
143146
if (next == null) {
144-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
147+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
145148
}
146149
if (next.getLexicalUnitType() == LexicalUnitType.OPERATOR_COMMA) {
147150
throw new DOMException(DOMException.SYNTAX_ERR,
148-
function_ + " requires consitent separators (blank or comma).");
151+
"'" + function_ + "' requires consitent separators (blank or comma).");
149152
}
150153

151154
if (LexicalUnitType.NONE != next.getLexicalUnitType()
@@ -159,7 +162,8 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
159162
}
160163

161164
if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) {
162-
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " alpha value must be separated by '/'.");
165+
throw new DOMException(DOMException.SYNTAX_ERR,
166+
"'" + function_ + "' alpha value must be separated by '/'.");
163167
}
164168
next = next.getNextLexicalUnit();
165169
if (next == null) {
@@ -170,7 +174,7 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
170174

171175
next = next.getNextLexicalUnit();
172176
if (next != null) {
173-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for " + function_ + " function.");
177+
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for '" + function_ + "' function.");
174178
}
175179
}
176180

src/main/java/org/htmlunit/cssparser/dom/HWBColorImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class HWBColorImpl implements Serializable {
4141
*/
4242
public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMException {
4343
if (function == null) {
44-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space hwb is required.");
44+
throw new DOMException(DOMException.SYNTAX_ERR, "Color space 'hwb' is required.");
4545
}
4646
final String functionLC = function.toLowerCase(Locale.ROOT);
4747
if (!"hwb".equals(functionLC)) {
@@ -50,13 +50,13 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5050

5151
LexicalUnit next = lu;
5252
if (next == null) {
53-
throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values.");
53+
throw new DOMException(DOMException.SYNTAX_ERR, "'hwb' requires at least three values.");
5454
}
5555
hue_ = getHuePart(next);
5656

5757
next = next.getNextLexicalUnit();
5858
if (next == null) {
59-
throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values.");
59+
throw new DOMException(DOMException.SYNTAX_ERR, "'hwb' requires at least three values.");
6060
}
6161

6262
if (LexicalUnitType.NONE != next.getLexicalUnitType()
@@ -67,7 +67,7 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
6767

6868
next = next.getNextLexicalUnit();
6969
if (next == null) {
70-
throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values.");
70+
throw new DOMException(DOMException.SYNTAX_ERR, "'hwb' requires at least three values.");
7171
}
7272

7373
if (LexicalUnitType.NONE != next.getLexicalUnitType()
@@ -81,7 +81,7 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8181
}
8282

8383
if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) {
84-
throw new DOMException(DOMException.SYNTAX_ERR, "hwb alpha value must be separated by '/'.");
84+
throw new DOMException(DOMException.SYNTAX_ERR, "'hwb' alpha value must be separated by '/'.");
8585
}
8686
next = next.getNextLexicalUnit();
8787
if (next == null) {
@@ -92,7 +92,7 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
9292

9393
next = next.getNextLexicalUnit();
9494
if (next != null) {
95-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for hwb function.");
95+
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for 'hwb' function.");
9696
}
9797
}
9898

src/main/java/org/htmlunit/cssparser/dom/LABColorImpl.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import org.w3c.dom.DOMException;
2323

2424
/**
25-
* Implementation of LABColor.
25+
* Implementation of LABColor and OKLABColor.
2626
*
2727
* @author Ronald Brill
2828
*/
2929
public class LABColorImpl implements Serializable {
30+
private final String function_;
31+
3032
private CSSValueImpl lightness_;
3133
private CSSValueImpl aDistance_;
3234
private CSSValueImpl bDistance_;
@@ -35,35 +37,36 @@ public class LABColorImpl implements Serializable {
3537
/**
3638
* Constructor that reads the values from the given
3739
* chain of LexicalUnits.
38-
* @param function the name of the function lab
40+
* @param function the name of the function; lab or oklab
3941
* @param lu the values
4042
* @throws DOMException in case of error
4143
*/
4244
public LABColorImpl(final String function, final LexicalUnit lu) throws DOMException {
4345
if (function == null) {
44-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space lab is required.");
46+
throw new DOMException(DOMException.SYNTAX_ERR, "Color space 'lab' or 'oklab' is required.");
4547
}
4648
final String functionLC = function.toLowerCase(Locale.ROOT);
47-
if (!"lab".equals(functionLC)) {
49+
if (!"lab".equals(functionLC) && !"oklab".equals(functionLC)) {
4850
throw new DOMException(DOMException.SYNTAX_ERR, "Color space '" + functionLC + "' not supported.");
4951
}
52+
function_ = functionLC;
5053

5154
LexicalUnit next = lu;
5255
if (next == null) {
53-
throw new DOMException(DOMException.SYNTAX_ERR, "lab requires at least three values.");
56+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5457
}
5558

5659
lightness_ = getPart(next);
5760

5861
next = next.getNextLexicalUnit();
5962
if (next == null) {
60-
throw new DOMException(DOMException.SYNTAX_ERR, "lab requires at least three values.");
63+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6164
}
6265

6366
aDistance_ = getPart(next);
6467
next = next.getNextLexicalUnit();
6568
if (next == null) {
66-
throw new DOMException(DOMException.SYNTAX_ERR, "lab requires at least three values.");
69+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6770
}
6871

6972
bDistance_ = getPart(next);
@@ -73,7 +76,8 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
7376
}
7477

7578
if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) {
76-
throw new DOMException(DOMException.SYNTAX_ERR, "lab alpha value must be separated by '/'.");
79+
throw new DOMException(DOMException.SYNTAX_ERR,
80+
"'" + function_ + "' alpha value must be separated by '/'.");
7781
}
7882
next = next.getNextLexicalUnit();
7983
if (next == null) {
@@ -83,7 +87,7 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8387
alpha_ = getAlphaPart(next);
8488
next = next.getNextLexicalUnit();
8589
if (next != null) {
86-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for lab function.");
90+
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for '" + function_ + "' function.");
8791
}
8892
}
8993

@@ -185,7 +189,8 @@ public String toString() {
185189
final StringBuilder sb = new StringBuilder();
186190

187191
sb
188-
.append("lab(")
192+
.append(function_)
193+
.append("(")
189194
.append(lightness_)
190195
.append(" ")
191196
.append(aDistance_)

src/main/java/org/htmlunit/cssparser/dom/LCHColorImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import org.w3c.dom.DOMException;
2323

2424
/**
25-
* Implementation of LCHColor.
25+
* Implementation of LCHColor and OKLCHColor.
2626
*
2727
* @author Ronald Brill
2828
*/
2929
public class LCHColorImpl implements Serializable {
30+
private final String function_;
31+
3032
private CSSValueImpl lightness_;
3133
private CSSValueImpl chroma_;
3234
private CSSValueImpl hue_;
@@ -35,36 +37,37 @@ public class LCHColorImpl implements Serializable {
3537
/**
3638
* Constructor that reads the values from the given
3739
* chain of LexicalUnits.
38-
* @param function the name of the function; lch
40+
* @param function the name of the function; lch or oklch
3941
* @param lu the values
4042
* @throws DOMException in case of error
4143
*/
4244
public LCHColorImpl(final String function, final LexicalUnit lu) throws DOMException {
4345
if (function == null) {
44-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space lch is required.");
46+
throw new DOMException(DOMException.SYNTAX_ERR, "Color space 'lch' or 'oklch' is required.");
4547
}
4648
final String functionLC = function.toLowerCase(Locale.ROOT);
47-
if (!"lch".equals(functionLC)) {
49+
if (!"lch".equals(functionLC) && !"oklch".equals(functionLC)) {
4850
throw new DOMException(DOMException.SYNTAX_ERR, "Color space '" + functionLC + "' not supported.");
4951
}
52+
function_ = functionLC;
5053

5154
LexicalUnit next = lu;
5255
if (next == null) {
53-
throw new DOMException(DOMException.SYNTAX_ERR, "lch requires at least three values.");
56+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5457
}
5558

5659
lightness_ = getPart(next, "lightness");
5760

5861
next = next.getNextLexicalUnit();
5962
if (next == null) {
60-
throw new DOMException(DOMException.SYNTAX_ERR, "lch requires at least three values.");
63+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6164
}
6265

6366
chroma_ = getPart(next, "chroma");
6467

6568
next = next.getNextLexicalUnit();
6669
if (next == null) {
67-
throw new DOMException(DOMException.SYNTAX_ERR, "lch requires at least three values.");
70+
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6871
}
6972

7073
hue_ = getHuePart(next);
@@ -74,18 +77,18 @@ public LCHColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
7477
}
7578

7679
if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) {
77-
throw new DOMException(DOMException.SYNTAX_ERR, "lch alpha value must be separated by '/'.");
80+
throw new DOMException(DOMException.SYNTAX_ERR,
81+
"'" + function_ + "' alpha value must be separated by '/'.");
7882
}
7983
next = next.getNextLexicalUnit();
8084
if (next == null) {
8185
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
8286
}
8387

8488
alpha_ = getAlphaPart(next);
85-
8689
next = next.getNextLexicalUnit();
8790
if (next != null) {
88-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for lch function.");
91+
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for '" + function_ + "' function.");
8992
}
9093
}
9194

@@ -199,7 +202,8 @@ public String toString() {
199202
final StringBuilder sb = new StringBuilder();
200203

201204
sb
202-
.append("lch(")
205+
.append(function_)
206+
.append("(")
203207
.append(lightness_)
204208
.append(" ")
205209
.append(chroma_)

0 commit comments

Comments
 (0)