Skip to content

Commit 84e11da

Browse files
committed
introduce AbstractColor
1 parent 3a83508 commit 84e11da

6 files changed

Lines changed: 109 additions & 181 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2019-2024 Ronald Brill.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.htmlunit.cssparser.dom;
16+
17+
import java.io.Serializable;
18+
19+
import org.htmlunit.cssparser.parser.LexicalUnit;
20+
import org.htmlunit.cssparser.parser.LexicalUnit.LexicalUnitType;
21+
import org.w3c.dom.DOMException;
22+
23+
/**
24+
* Color base class.
25+
*
26+
* @author Ronald Brill
27+
*/
28+
public class AbstractColor implements Serializable {
29+
30+
private CSSValueImpl alpha_;
31+
32+
protected static CSSValueImpl getNumberPercentagePart(final LexicalUnit next) {
33+
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
34+
35+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
36+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
37+
38+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
39+
return new CSSValueImpl(next, true);
40+
}
41+
42+
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
43+
}
44+
45+
protected void getAlphaPart(final LexicalUnit next) {
46+
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
47+
48+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
49+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
50+
51+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
52+
setAlpha(new CSSValueImpl(next, true));
53+
return;
54+
}
55+
56+
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
57+
}
58+
59+
/**
60+
* @return the alpha part.
61+
*/
62+
public CSSValueImpl getAlpha() {
63+
return alpha_;
64+
}
65+
66+
/**
67+
* Sets the alpha part to a new value.
68+
* @param alpha the new CSSValueImpl
69+
*/
70+
public void setAlpha(final CSSValueImpl alpha) {
71+
alpha_ = alpha;
72+
}
73+
}

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

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package org.htmlunit.cssparser.dom;
1616

17-
import java.io.Serializable;
1817
import java.util.Locale;
1918

2019
import org.htmlunit.cssparser.parser.LexicalUnit;
@@ -26,13 +25,12 @@
2625
*
2726
* @author Ronald Brill
2827
*/
29-
public class HSLColorImpl implements Serializable {
28+
public class HSLColorImpl extends AbstractColor {
3029
private final String function_;
3130

3231
private CSSValueImpl hue_;
3332
private CSSValueImpl saturation_;
3433
private CSSValueImpl lightness_;
35-
private CSSValueImpl alpha_;
3634
private final boolean commaSeparated_;
3735

3836
/**
@@ -127,7 +125,9 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
127125
throw new DOMException(DOMException.SYNTAX_ERR,
128126
"'" + function_ + "' has to use blank as separator if none is used.");
129127
}
130-
alpha_ = getAlphaPart(next);
128+
129+
getAlphaPart(next);
130+
131131
next = next.getNextLexicalUnit();
132132
if (next != null) {
133133
throw new DOMException(DOMException.SYNTAX_ERR,
@@ -170,7 +170,7 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
170170
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
171171
}
172172

173-
alpha_ = getAlphaPart(next);
173+
getAlphaPart(next);
174174

175175
next = next.getNextLexicalUnit();
176176
if (next != null) {
@@ -194,19 +194,6 @@ private static CSSValueImpl getHuePart(final LexicalUnit next) {
194194
throw new DOMException(DOMException.SYNTAX_ERR, "Color hue part has to be numeric or an angle.");
195195
}
196196

197-
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
198-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
199-
200-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
201-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
202-
203-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
204-
return new CSSValueImpl(next, true);
205-
}
206-
207-
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
208-
}
209-
210197
/**
211198
* @return the hue part.
212199
*/
@@ -252,21 +239,6 @@ public void setLightness(final CSSValueImpl lightness) {
252239
lightness_ = lightness;
253240
}
254241

255-
/**
256-
* @return the alpha part.
257-
*/
258-
public CSSValueImpl getAlpha() {
259-
return alpha_;
260-
}
261-
262-
/**
263-
* Sets the alpha part to a new value.
264-
* @param alpha the new CSSValueImpl
265-
*/
266-
public void setAlpha(final CSSValueImpl alpha) {
267-
alpha_ = alpha;
268-
}
269-
270242
/**
271243
* {@inheritDoc}
272244
*/
@@ -284,9 +256,8 @@ public String toString() {
284256
.append(saturation_)
285257
.append(", ")
286258
.append(lightness_);
287-
288-
if (null != alpha_) {
289-
sb.append(", ").append(alpha_);
259+
if (null != getAlpha()) {
260+
sb.append(", ").append(getAlpha());
290261
}
291262
}
292263
else {
@@ -295,9 +266,8 @@ public String toString() {
295266
.append(saturation_)
296267
.append(" ")
297268
.append(lightness_);
298-
299-
if (null != alpha_) {
300-
sb.append(" / ").append(alpha_);
269+
if (null != getAlpha()) {
270+
sb.append(" / ").append(getAlpha());
301271
}
302272
}
303273

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package org.htmlunit.cssparser.dom;
1616

17-
import java.io.Serializable;
1817
import java.util.Locale;
1918

2019
import org.htmlunit.cssparser.parser.LexicalUnit;
@@ -26,11 +25,10 @@
2625
*
2726
* @author Ronald Brill
2827
*/
29-
public class HWBColorImpl implements Serializable {
28+
public class HWBColorImpl extends AbstractColor {
3029
private CSSValueImpl hue_;
3130
private CSSValueImpl whiteness_;
3231
private CSSValueImpl blackness_;
33-
private CSSValueImpl alpha_;
3432

3533
/**
3634
* Constructor that reads the values from the given
@@ -88,7 +86,7 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8886
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
8987
}
9088

91-
alpha_ = getAlphaPart(next);
89+
getAlphaPart(next);
9290

9391
next = next.getNextLexicalUnit();
9492
if (next != null) {
@@ -112,19 +110,6 @@ private static CSSValueImpl getHuePart(final LexicalUnit next) {
112110
throw new DOMException(DOMException.SYNTAX_ERR, "Color hue part has to be numeric or an angle.");
113111
}
114112

115-
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
116-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
117-
118-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
119-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
120-
121-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
122-
return new CSSValueImpl(next, true);
123-
}
124-
125-
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
126-
}
127-
128113
/**
129114
* @return the hue part.
130115
*/
@@ -170,21 +155,6 @@ public void setBlackness(final CSSValueImpl blackness) {
170155
blackness_ = blackness;
171156
}
172157

173-
/**
174-
* @return the alpha part.
175-
*/
176-
public CSSValueImpl getAlpha() {
177-
return alpha_;
178-
}
179-
180-
/**
181-
* Sets the alpha part to a new value.
182-
* @param alpha the new CSSValueImpl
183-
*/
184-
public void setAlpha(final CSSValueImpl alpha) {
185-
alpha_ = alpha;
186-
}
187-
188158
/**
189159
* {@inheritDoc}
190160
*/
@@ -200,8 +170,8 @@ public String toString() {
200170
.append(" ")
201171
.append(blackness_);
202172

203-
if (null != alpha_) {
204-
sb.append(" / ").append(alpha_);
173+
if (null != getAlpha()) {
174+
sb.append(" / ").append(getAlpha());
205175
}
206176

207177
sb.append(")");

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

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package org.htmlunit.cssparser.dom;
1616

17-
import java.io.Serializable;
1817
import java.util.Locale;
1918

2019
import org.htmlunit.cssparser.parser.LexicalUnit;
@@ -26,13 +25,12 @@
2625
*
2726
* @author Ronald Brill
2827
*/
29-
public class LABColorImpl implements Serializable {
28+
public class LABColorImpl extends AbstractColor {
3029
private final String function_;
3130

3231
private CSSValueImpl lightness_;
3332
private CSSValueImpl aDistance_;
3433
private CSSValueImpl bDistance_;
35-
private CSSValueImpl alpha_;
3634

3735
/**
3836
* Constructor that reads the values from the given
@@ -56,20 +54,20 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5654
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5755
}
5856

59-
lightness_ = getPart(next);
57+
lightness_ = getNumberPercentagePart(next);
6058

6159
next = next.getNextLexicalUnit();
6260
if (next == null) {
6361
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6462
}
6563

66-
aDistance_ = getPart(next);
64+
aDistance_ = getNumberPercentagePart(next);
6765
next = next.getNextLexicalUnit();
6866
if (next == null) {
6967
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
7068
}
7169

72-
bDistance_ = getPart(next);
70+
bDistance_ = getNumberPercentagePart(next);
7371
next = next.getNextLexicalUnit();
7472
if (next == null) {
7573
return;
@@ -84,39 +82,14 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8482
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
8583
}
8684

87-
alpha_ = getAlphaPart(next);
85+
getAlphaPart(next);
86+
8887
next = next.getNextLexicalUnit();
8988
if (next != null) {
9089
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for '" + function_ + "' function.");
9190
}
9291
}
9392

94-
private static CSSValueImpl getPart(final LexicalUnit next) {
95-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
96-
97-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
98-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
99-
100-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
101-
return new CSSValueImpl(next, true);
102-
}
103-
104-
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
105-
}
106-
107-
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
108-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
109-
110-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
111-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
112-
113-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
114-
return new CSSValueImpl(next, true);
115-
}
116-
117-
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
118-
}
119-
12093
/**
12194
* @return the lightness part.
12295
*/
@@ -166,21 +139,6 @@ public void setB(final CSSValueImpl b) {
166139
bDistance_ = b;
167140
}
168141

169-
/**
170-
* @return the alpha part.
171-
*/
172-
public CSSValueImpl getAlpha() {
173-
return alpha_;
174-
}
175-
176-
/**
177-
* Sets the alpha part to a new value.
178-
* @param alpha the new CSSValueImpl
179-
*/
180-
public void setAlpha(final CSSValueImpl alpha) {
181-
alpha_ = alpha;
182-
}
183-
184142
/**
185143
* {@inheritDoc}
186144
*/
@@ -197,8 +155,8 @@ public String toString() {
197155
.append(" ")
198156
.append(bDistance_);
199157

200-
if (null != alpha_) {
201-
sb.append(" / ").append(alpha_);
158+
if (null != getAlpha()) {
159+
sb.append(" / ").append(getAlpha());
202160
}
203161

204162
sb.append(")");

0 commit comments

Comments
 (0)