Skip to content

Commit 8fe9708

Browse files
committed
HSL redone using css-color-4 modern syntax
1 parent ddf6091 commit 8fe9708

5 files changed

Lines changed: 141 additions & 211 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6106,7 +6106,7 @@ final public boolean prio() throws ParseException {
61066106
color.setHSLColor(exp, ac);
61076107
{if ("" != null) return color;}
61086108
} else if (funcname.equals("hsla(")) {
6109-
color.setHSLAColor(exp, ac);
6109+
color.setHSLColor(exp, ac);
61106110
{if ("" != null) return color;}
61116111
} else if (funcname.equals("hwb(")) {
61126112
color.setHWBColor(exp, ac);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3046,7 +3046,7 @@ CssValue function() :
30463046
color.setHSLColor(exp, ac);
30473047
return color;
30483048
} else if (funcname.equals("hsla(")) {
3049-
color.setHSLAColor(exp, ac);
3049+
color.setHSLColor(exp, ac);
30503050
return color;
30513051
} else if (funcname.equals("hwb(")) {
30523052
color.setHWBColor(exp, ac);

org/w3c/css/values/CssColor.java

Lines changed: 122 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public final int getType() {
3131
RGB rgb = null;
3232
RGBA rgba = null;
3333
HSL hsl = null;
34-
HSLA hsla = null;
3534
HWB hwb = null;
3635

3736
/**
@@ -89,8 +88,6 @@ public String toString() {
8988
return rgba.toString();
9089
} else if (hsl != null) {
9190
return hsl.toString();
92-
} else if (hsla != null) {
93-
return hsla.toString();
9491
} else {
9592
return hwb.toString();
9693
}
@@ -324,6 +321,128 @@ public void setModernRGBColor(CssExpression exp, ApplContext ac)
324321
}
325322
}
326323

324+
/**
325+
* Parse a HSL color.
326+
* format hsl( <percentage>{3} [ / <alpha-value> ]? ) |
327+
* hsl( <number>{3} [ / <alpha-value> ]? )
328+
*/
329+
public void setHSLColor(CssExpression exp, ApplContext ac)
330+
throws InvalidParamException {
331+
// HSL defined in CSS3 and onward
332+
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
333+
StringBuilder sb = new StringBuilder();
334+
sb.append("hsl(").append(exp.toStringFromStart()).append(')');
335+
throw new InvalidParamException("notversion", sb.toString(),
336+
ac.getCssVersionString(), ac);
337+
}
338+
339+
CssValue val = exp.getValue();
340+
char op = exp.getOperator();
341+
color = null;
342+
hsl = new HSL();
343+
boolean separator_space = (op == SPACE);
344+
345+
if (val == null || (!separator_space && (op != COMMA))) {
346+
throw new InvalidParamException("invalid-color", ac);
347+
}
348+
349+
// H
350+
switch (val.getType()) {
351+
case CssTypes.CSS_ANGLE:
352+
case CssTypes.CSS_NUMBER:
353+
hsl.setHue(ac, val);
354+
break;
355+
default:
356+
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
357+
}
358+
359+
exp.next();
360+
val = exp.getValue();
361+
op = exp.getOperator();
362+
363+
if (val == null || (separator_space && (op != SPACE)) ||
364+
(!separator_space && (op != COMMA))) {
365+
throw new InvalidParamException("invalid-color", ac);
366+
}
367+
368+
// S
369+
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
370+
hsl.setSaturation(ac, val);
371+
} else {
372+
exp.starts();
373+
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
374+
}
375+
376+
exp.next();
377+
val = exp.getValue();
378+
op = exp.getOperator();
379+
380+
if (val == null) {
381+
throw new InvalidParamException("invalid-color", ac);
382+
}
383+
384+
// L
385+
386+
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
387+
hsl.setLightness(ac, val);
388+
} else {
389+
exp.starts();
390+
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
391+
}
392+
393+
exp.next();
394+
395+
// check for optional alpha channel
396+
if (!exp.end()) {
397+
// care for old syntax
398+
if (op == COMMA && !separator_space) {
399+
val = exp.getValue();
400+
switch (val.getType()) {
401+
case CssTypes.CSS_NUMBER:
402+
case CssTypes.CSS_PERCENTAGE:
403+
hsl.setAlpha(ac, val);
404+
break;
405+
default:
406+
throw new InvalidParamException("rgb", val, ac);
407+
}
408+
} else {
409+
// otherwise modern syntax
410+
if (op != SPACE) {
411+
throw new InvalidParamException("invalid-color", ac);
412+
}
413+
// now we need an alpha.
414+
val = exp.getValue();
415+
op = exp.getOperator();
416+
417+
if (val.getType() != CssTypes.CSS_SWITCH) {
418+
throw new InvalidParamException("rgb", val, ac);
419+
}
420+
if (op != SPACE) {
421+
throw new InvalidParamException("invalid-color", ac);
422+
}
423+
exp.next();
424+
// now we get the alpha value
425+
if (exp.end()) {
426+
throw new InvalidParamException("rgb", exp.getValue(), ac);
427+
}
428+
val = exp.getValue();
429+
switch (val.getType()) {
430+
case CssTypes.CSS_NUMBER:
431+
case CssTypes.CSS_PERCENTAGE:
432+
hsl.setAlpha(ac, val);
433+
break;
434+
default:
435+
throw new InvalidParamException("rgb", val, ac);
436+
}
437+
}
438+
exp.next();
439+
440+
if (!exp.end()) {
441+
throw new InvalidParamException("rgb", exp.getValue(), ac);
442+
}
443+
}
444+
}
445+
327446
/**
328447
* Parse a RGB color.
329448
* format #(3-6)<hexnum>
@@ -494,8 +613,6 @@ public boolean equals(Object cssColor) {
494613
return rgba.equals(otherColor.rgba);
495614
} else if ((hsl != null) && (otherColor.hsl != null)) {
496615
return hsl.equals(otherColor.hsl);
497-
} else if ((hsla != null) && (otherColor.hsla != null)) {
498-
return hsla.equals(otherColor.hsla);
499616
}
500617
return false;
501618
}
@@ -622,156 +739,6 @@ private void __setRGBAColor(RGBA rgba, CssExpression exp, ApplContext ac)
622739
}
623740
}
624741

625-
public void setHSLColor(CssExpression exp, ApplContext ac)
626-
throws InvalidParamException {
627-
// HSL defined in CSS3 and onward
628-
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
629-
StringBuilder sb = new StringBuilder();
630-
sb.append("hsl(").append(exp.toStringFromStart()).append(')');
631-
throw new InvalidParamException("notversion", sb.toString(),
632-
ac.getCssVersionString(), ac);
633-
}
634-
color = null;
635-
hsl = new HSL();
636-
637-
CssValue val = exp.getValue();
638-
char op = exp.getOperator();
639-
// H
640-
if (val == null || op != COMMA) {
641-
throw new InvalidParamException("invalid-color", ac);
642-
}
643-
switch (val.getType()) {
644-
case CssTypes.CSS_ANGLE:
645-
case CssTypes.CSS_NUMBER:
646-
hsl.setHue(ac, val);
647-
break;
648-
default:
649-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
650-
}
651-
652-
// S
653-
exp.next();
654-
val = exp.getValue();
655-
op = exp.getOperator();
656-
if (val == null || op != COMMA) {
657-
exp.starts();
658-
throw new InvalidParamException("invalid-color", ac);
659-
}
660-
// todo is (0 number) allowed ?
661-
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
662-
hsl.setSaturation(ac, val);
663-
} else {
664-
exp.starts();
665-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
666-
}
667-
668-
// L
669-
exp.next();
670-
val = exp.getValue();
671-
op = exp.getOperator();
672-
if (val == null) {
673-
exp.starts();
674-
throw new InvalidParamException("invalid-color", ac);
675-
}
676-
677-
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
678-
hsl.setLightness(ac, val);
679-
} else {
680-
exp.starts();
681-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
682-
}
683-
684-
exp.next();
685-
if (exp.getValue() != null) {
686-
exp.starts();
687-
throw new InvalidParamException("rgb", exp.getValue(), ac);
688-
}
689-
}
690-
691-
692-
public void setHSLAColor(CssExpression exp, ApplContext ac)
693-
throws InvalidParamException {
694-
// RGBA defined in CSS3 and onward
695-
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
696-
StringBuilder sb = new StringBuilder();
697-
sb.append("hsla(").append(exp.toStringFromStart()).append(')');
698-
throw new InvalidParamException("notversion", sb.toString(),
699-
ac.getCssVersionString(), ac);
700-
}
701-
702-
color = null;
703-
hsla = new HSLA();
704-
705-
CssValue val = exp.getValue();
706-
char op = exp.getOperator();
707-
// H
708-
if (val == null || op != COMMA) {
709-
throw new InvalidParamException("invalid-color", ac);
710-
}
711-
switch (val.getType()) {
712-
case CssTypes.CSS_ANGLE:
713-
case CssTypes.CSS_NUMBER:
714-
hsla.setHue(ac, val);
715-
break;
716-
default:
717-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
718-
}
719-
720-
// S
721-
exp.next();
722-
val = exp.getValue();
723-
op = exp.getOperator();
724-
if (val == null || op != COMMA) {
725-
exp.starts();
726-
throw new InvalidParamException("invalid-color", ac);
727-
}
728-
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
729-
hsla.setSaturation(ac, val);
730-
} else {
731-
exp.starts();
732-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
733-
}
734-
735-
// L
736-
exp.next();
737-
val = exp.getValue();
738-
op = exp.getOperator();
739-
if (val == null || op != COMMA) {
740-
exp.starts();
741-
throw new InvalidParamException("invalid-color", ac);
742-
}
743-
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
744-
hsla.setLightness(ac, val);
745-
} else {
746-
exp.starts();
747-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
748-
}
749-
750-
// A
751-
exp.next();
752-
val = exp.getValue();
753-
if (val == null) {
754-
exp.starts();
755-
throw new InvalidParamException("invalid-color", ac);
756-
}
757-
switch (val.getType()) {
758-
case CssTypes.CSS_NUMBER:
759-
// starting with CSS Color 4
760-
case CssTypes.CSS_PERCENTAGE:
761-
hsla.setAlpha(ac, val);
762-
break;
763-
default:
764-
exp.starts();
765-
throw new InvalidParamException("rgb", val, ac); // FIXME rgba
766-
}
767-
// extra values?
768-
exp.next();
769-
if (exp.getValue() != null) {
770-
exp.starts();
771-
throw new InvalidParamException("rgb", exp.getValue(), ac);
772-
}
773-
}
774-
775742
public void setHWBColor(CssExpression exp, ApplContext ac)
776743
throws InvalidParamException {
777744
// HWB defined in CSSColor Level 4 and onward, currently used in the CSS level

org/w3c/css/values/HSL.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
public class HSL {
2323
String output = null;
24-
CssValue vh, vs, vl;
24+
CssValue vh, vs, vl, va;
25+
26+
static final String functionname = "hsl";
2527

2628
/**
2729
* Create a new HSL
@@ -120,15 +122,25 @@ public final void setLightness(ApplContext ac, CssValue val)
120122
vl = filterValue(ac, val);
121123
}
122124

125+
126+
public void setAlpha(ApplContext ac, CssValue alpha)
127+
throws InvalidParamException {
128+
output = null;
129+
va = RGBA.filterAlpha(ac, alpha);
130+
}
131+
123132
/**
124133
* Returns a string representation of the object.
125134
*/
126135
public String toString() {
127136
if (output == null) {
128-
StringBuilder sb = new StringBuilder("hsl(");
129-
sb.append(vh).append(", ");
130-
sb.append(vs).append(", ");
131-
sb.append(vl).append(")");
137+
StringBuilder sb = new StringBuilder(functionname);
138+
sb.append('(').append(vh).append(' ');
139+
sb.append(vs).append(' ').append(vl);
140+
if (va != null) {
141+
sb.append(" / ").append(va);
142+
}
143+
sb.append(')');
132144
output = sb.toString();
133145
}
134146
return output;

0 commit comments

Comments
 (0)