1414 */
1515package org .htmlunit .cssparser .dom ;
1616
17- import java .util .Locale ;
18-
1917import org .htmlunit .cssparser .parser .LexicalUnit ;
20- import org .htmlunit .cssparser .parser .LexicalUnit .LexicalUnitType ;
2118import org .w3c .dom .DOMException ;
2219
2320/**
2623 * @author Ronald Brill
2724 */
2825public class HSLColorImpl extends AbstractColor {
29- private final String function_ ;
30-
31- private CSSValueImpl hue_ ;
32- private CSSValueImpl saturation_ ;
33- private CSSValueImpl lightness_ ;
34- private final boolean commaSeparated_ ;
3526
3627 /**
3728 * Constructor that reads the values from the given
@@ -41,254 +32,6 @@ public class HSLColorImpl extends AbstractColor {
4132 * @throws DOMException in case of error
4233 */
4334 public HSLColorImpl (final String function , final LexicalUnit lu ) throws DOMException {
44- if (function == null ) {
45- throw new DOMException (DOMException .SYNTAX_ERR , "Color space 'hsl' or 'hsla' is required." );
46- }
47- final String functionLC = function .toLowerCase (Locale .ROOT );
48- if (!"hsl" .equals (functionLC ) && !"hsla" .equals (functionLC )) {
49- throw new DOMException (DOMException .SYNTAX_ERR , "Color space '" + functionLC + "' not supported." );
50- }
51- function_ = functionLC ;
52-
53- if (lu == null ) {
54- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
55- }
56-
57- if (handleRelativeColors (lu )) {
58- commaSeparated_ = false ;
59- return ;
60- }
61-
62- LexicalUnit next = lu ;
63- hue_ = getHuePart (next );
64-
65- next = next .getNextLexicalUnit ();
66- if (next == null ) {
67- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
68- }
69-
70- commaSeparated_ = LexicalUnitType .OPERATOR_COMMA == next .getLexicalUnitType ();
71- if (commaSeparated_ ) {
72- if (hue_ .getLexicalUnitType () == LexicalUnitType .NONE ) {
73- throw new DOMException (DOMException .SYNTAX_ERR ,
74- "'" + function_ + "' has to use blank as separator if none is used." );
75- }
76-
77- next = next .getNextLexicalUnit ();
78- if (next == null ) {
79- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
80- }
81-
82- if (LexicalUnitType .NONE == next .getLexicalUnitType ()) {
83- throw new DOMException (DOMException .SYNTAX_ERR ,
84- "'" + function_ + "' has to use blank as separator if none is used." );
85- }
86- if (LexicalUnitType .PERCENTAGE != next .getLexicalUnitType ()) {
87- throw new DOMException (DOMException .SYNTAX_ERR , "Saturation part has to be percentage." );
88- }
89- saturation_ = new CSSValueImpl (next , true );
90-
91- next = next .getNextLexicalUnit ();
92- if (next == null ) {
93- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
94- }
95-
96- if (LexicalUnitType .OPERATOR_COMMA != next .getLexicalUnitType ()) {
97- throw new DOMException (DOMException .SYNTAX_ERR ,
98- "'" + function_ + "' parameters must be separated by ','." );
99- }
100-
101- next = next .getNextLexicalUnit ();
102- if (next == null ) {
103- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
104- }
105-
106- if (LexicalUnitType .NONE == next .getLexicalUnitType ()) {
107- throw new DOMException (DOMException .SYNTAX_ERR ,
108- "'" + function_ + "' has to use blank as separator if none is used." );
109- }
110- if (LexicalUnitType .PERCENTAGE != next .getLexicalUnitType ()) {
111- throw new DOMException (DOMException .SYNTAX_ERR , "Lightness part has to be percentage." );
112- }
113- lightness_ = new CSSValueImpl (next , true );
114-
115- next = next .getNextLexicalUnit ();
116- if (next == null ) {
117- return ;
118- }
119-
120- if (LexicalUnitType .OPERATOR_COMMA != next .getLexicalUnitType ()) {
121- throw new DOMException (DOMException .SYNTAX_ERR ,
122- "'" + function_ + "' parameters must be separated by ','." );
123- }
124-
125- next = next .getNextLexicalUnit ();
126- if (next == null ) {
127- throw new DOMException (DOMException .SYNTAX_ERR , "Missing alpha value." );
128- }
129-
130- if (LexicalUnitType .NONE == next .getLexicalUnitType ()) {
131- throw new DOMException (DOMException .SYNTAX_ERR ,
132- "'" + function_ + "' has to use blank as separator if none is used." );
133- }
134-
135- getAlphaPart (next );
136-
137- next = next .getNextLexicalUnit ();
138- if (next != null ) {
139- throw new DOMException (DOMException .SYNTAX_ERR ,
140- "Too many parameters for '" + function_ + "' function." );
141- }
142- return ;
143- }
144-
145- if (LexicalUnitType .NONE != next .getLexicalUnitType ()
146- && LexicalUnitType .PERCENTAGE != next .getLexicalUnitType ()) {
147- throw new DOMException (DOMException .SYNTAX_ERR , "Saturation part has to be percentage." );
148- }
149- saturation_ = new CSSValueImpl (next , true );
150-
151- next = next .getNextLexicalUnit ();
152- if (next == null ) {
153- throw new DOMException (DOMException .SYNTAX_ERR , "'" + function_ + "' requires at least three values." );
154- }
155- if (next .getLexicalUnitType () == LexicalUnitType .OPERATOR_COMMA ) {
156- throw new DOMException (DOMException .SYNTAX_ERR ,
157- "'" + function_ + "' requires consitent separators (blank or comma)." );
158- }
159-
160- if (LexicalUnitType .NONE != next .getLexicalUnitType ()
161- && LexicalUnitType .PERCENTAGE != next .getLexicalUnitType ()) {
162- throw new DOMException (DOMException .SYNTAX_ERR , "Lightness part has to be percentage." );
163- }
164- lightness_ = new CSSValueImpl (next , true );
165- next = next .getNextLexicalUnit ();
166- if (next == null ) {
167- return ;
168- }
169-
170- if (next .getLexicalUnitType () != LexicalUnitType .OPERATOR_SLASH ) {
171- throw new DOMException (DOMException .SYNTAX_ERR ,
172- "'" + function_ + "' alpha value must be separated by '/'." );
173- }
174- next = next .getNextLexicalUnit ();
175- if (next == null ) {
176- throw new DOMException (DOMException .SYNTAX_ERR , "Missing alpha value." );
177- }
178-
179- getAlphaPart (next );
180-
181- next = next .getNextLexicalUnit ();
182- if (next != null ) {
183- throw new DOMException (DOMException .SYNTAX_ERR , "Too many parameters for '" + function_ + "' function." );
184- }
185- }
186-
187- private static CSSValueImpl getHuePart (final LexicalUnit next ) {
188- if (LexicalUnitType .DEGREE == next .getLexicalUnitType ()
189- || LexicalUnitType .RADIAN == next .getLexicalUnitType ()
190- || LexicalUnitType .GRADIAN == next .getLexicalUnitType ()
191- || LexicalUnitType .TURN == next .getLexicalUnitType ()
192-
193- || LexicalUnitType .INTEGER == next .getLexicalUnitType ()
194- || LexicalUnitType .REAL == next .getLexicalUnitType ()
195-
196- || LexicalUnitType .NONE == next .getLexicalUnitType ()) {
197- return new CSSValueImpl (next , true );
198- }
199-
200- throw new DOMException (DOMException .SYNTAX_ERR , "Color hue part has to be numeric or an angle." );
201- }
202-
203- /**
204- * @return the hue part.
205- */
206- public CSSValueImpl getHue () {
207- return hue_ ;
208- }
209-
210- /**
211- * Sets the hue part to a new value.
212- * @param hue the new CSSValueImpl
213- */
214- public void setHue (final CSSValueImpl hue ) {
215- hue_ = hue ;
216- }
217-
218- /**
219- * @return the saturation part.
220- */
221- public CSSValueImpl getSaturation () {
222- return saturation_ ;
223- }
224-
225- /**
226- * Sets the saturation part to a new value.
227- * @param saturation the new CSSValueImpl
228- */
229- public void setSaturation (final CSSValueImpl saturation ) {
230- saturation_ = saturation ;
231- }
232-
233- /**
234- * @return the lightness part.
235- */
236- public CSSValueImpl getLightness () {
237- return lightness_ ;
238- }
239-
240- /**
241- * Sets the lightness part to a new value.
242- * @param lightness the new CSSValueImpl
243- */
244- public void setLightness (final CSSValueImpl lightness ) {
245- lightness_ = lightness ;
246- }
247-
248- /**
249- * {@inheritDoc}
250- */
251- @ Override
252- public String toString () {
253- final StringBuilder sb = new StringBuilder ();
254-
255- sb
256- .append (function_ )
257- .append ("(" );
258-
259- final CSSValueImpl rel = getRelative ();
260- if (rel != null ) {
261- sb
262- .append ("from " )
263- .append (rel );
264- }
265- else {
266- if (commaSeparated_ ) {
267- sb
268- .append (hue_ )
269- .append (", " )
270- .append (saturation_ )
271- .append (", " )
272- .append (lightness_ );
273- if (null != getAlpha ()) {
274- sb .append (", " ).append (getAlpha ());
275- }
276- }
277- else {
278- sb
279- .append (hue_ )
280- .append (" " )
281- .append (saturation_ )
282- .append (" " )
283- .append (lightness_ );
284- if (null != getAlpha ()) {
285- sb .append (" / " ).append (getAlpha ());
286- }
287- }
288- }
289-
290- sb .append (")" );
291-
292- return sb .toString ();
35+ super (function , lu );
29336 }
29437}
0 commit comments