Skip to content

Commit 99d96d0

Browse files
committed
LAB and LCH per current draft of css-color-4 amended by issue #csswg-drafts/289
1 parent 9553130 commit 99d96d0

5 files changed

Lines changed: 201 additions & 53 deletions

File tree

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6111,6 +6111,12 @@ final public boolean prio() throws ParseException {
61116111
} else if (funcname.equals("hwb(")) {
61126112
color.setHWBColor(exp, ac);
61136113
{if ("" != null) return color;}
6114+
} else if (funcname.equals("lab(")) {
6115+
color.setLABColor(exp, ac);
6116+
{if ("" != null) return color;}
6117+
} else if (funcname.equals("lch(")) {
6118+
color.setLCHColor(exp, ac);
6119+
{if ("" != null) return color;}
61146120
} else if (funcname.equals("image(")) {
61156121
CssImage img = new CssImage();
61166122
img.setImageList(exp, ac);
@@ -7035,6 +7041,17 @@ private boolean jj_3R_196()
70357041
return false;
70367042
}
70377043

7044+
private boolean jj_3R_214()
7045+
{
7046+
Token xsp;
7047+
xsp = jj_scanpos;
7048+
if (jj_scan_token(39)) {
7049+
jj_scanpos = xsp;
7050+
if (jj_scan_token(40)) return true;
7051+
}
7052+
return false;
7053+
}
7054+
70387055
private boolean jj_3R_195()
70397056
{
70407057
if (jj_scan_token(ST)) return true;
@@ -7071,17 +7088,6 @@ private boolean jj_3R_190()
70717088
return false;
70727089
}
70737090

7074-
private boolean jj_3R_214()
7075-
{
7076-
Token xsp;
7077-
xsp = jj_scanpos;
7078-
if (jj_scan_token(39)) {
7079-
jj_scanpos = xsp;
7080-
if (jj_scan_token(40)) return true;
7081-
}
7082-
return false;
7083-
}
7084-
70857091
private boolean jj_3R_189()
70867092
{
70877093
if (jj_scan_token(ABSOLUTLENGTH)) return true;
@@ -7118,6 +7124,12 @@ private boolean jj_3R_184()
71187124
return false;
71197125
}
71207126

7127+
private boolean jj_3R_217()
7128+
{
7129+
if (jj_scan_token(IDENT)) return true;
7130+
return false;
7131+
}
7132+
71217133
private boolean jj_3R_166()
71227134
{
71237135
Token xsp;
@@ -7194,12 +7206,6 @@ private boolean jj_3R_169()
71947206
return false;
71957207
}
71967208

7197-
private boolean jj_3R_217()
7198-
{
7199-
if (jj_scan_token(IDENT)) return true;
7200-
return false;
7201-
}
7202-
72037209
private boolean jj_3_1()
72047210
{
72057211
if (jj_3R_147()) return true;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,6 +3051,12 @@ CssValue function() :
30513051
} else if (funcname.equals("hwb(")) {
30523052
color.setHWBColor(exp, ac);
30533053
return color;
3054+
} else if (funcname.equals("lab(")) {
3055+
color.setLABColor(exp, ac);
3056+
return color;
3057+
} else if (funcname.equals("lch(")) {
3058+
color.setLCHColor(exp, ac);
3059+
return color;
30543060
} else if (funcname.equals("image(")) {
30553061
CssImage img = new CssImage();
30563062
img.setImageList(exp, ac);

org/w3c/css/values/CssColor.java

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ public final int getType() {
2828
}
2929

3030
Object color = null;
31+
// FIXME TODO, replace with one color type + one type for converted color for comparison
3132
RGB rgb = null;
3233
RGBA rgba = null;
3334
HSL hsl = null;
3435
HWB hwb = null;
36+
LAB lab = null;
37+
LCH lch = null;
3538

3639
/**
3740
* Create a new CssColor.
@@ -755,7 +758,7 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
755758
CssValue val = exp.getValue();
756759
char op = exp.getOperator();
757760
// H
758-
if (val == null || op != COMMA) {
761+
if (val == null || op != SPACE) {
759762
throw new InvalidParamException("invalid-color", ac);
760763
}
761764
switch (val.getType()) {
@@ -771,7 +774,7 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
771774
exp.next();
772775
val = exp.getValue();
773776
op = exp.getOperator();
774-
if (val == null || op != COMMA) {
777+
if (val == null || op != SPACE) {
775778
exp.starts();
776779
throw new InvalidParamException("invalid-color", ac);
777780
}
@@ -786,7 +789,7 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
786789
exp.next();
787790
val = exp.getValue();
788791
op = exp.getOperator();
789-
if (val == null || (op != COMMA && exp.getRemainingCount() > 1)) {
792+
if (val == null || (op != SPACE && exp.getRemainingCount() > 1)) {
790793
exp.starts();
791794
throw new InvalidParamException("invalid-color", ac);
792795
}
@@ -820,5 +823,165 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
820823
}
821824
}
822825

826+
827+
public void setLABColor(CssExpression exp, ApplContext ac)
828+
throws InvalidParamException {
829+
// HWB defined in CSSColor Level 4 and onward, currently used in the CSS level
830+
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
831+
StringBuilder sb = new StringBuilder();
832+
sb.append("lab(").append(exp.toStringFromStart()).append(')');
833+
throw new InvalidParamException("notversion", sb.toString(),
834+
ac.getCssVersionString(), ac);
835+
}
836+
837+
color = null;
838+
lab = new LAB();
839+
CssValue val = exp.getValue();
840+
char op = exp.getOperator();
841+
// L
842+
if (val == null || op != SPACE) {
843+
throw new InvalidParamException("invalid-color", ac);
844+
}
845+
switch (val.getType()) {
846+
case CssTypes.CSS_NUMBER:
847+
lab.setL(ac, val);
848+
break;
849+
default:
850+
throw new InvalidParamException("rgb", val, ac); // FIXME lab
851+
}
852+
853+
// A
854+
exp.next();
855+
val = exp.getValue();
856+
op = exp.getOperator();
857+
if (val == null || op != SPACE) {
858+
exp.starts();
859+
throw new InvalidParamException("invalid-color", ac);
860+
}
861+
if (val.getType() == CssTypes.CSS_NUMBER) {
862+
lab.setA(ac, val);
863+
} else {
864+
exp.starts();
865+
throw new InvalidParamException("rgb", val, ac); // FIXME lab
866+
}
867+
868+
// B
869+
exp.next();
870+
val = exp.getValue();
871+
op = exp.getOperator();
872+
if (val == null || (op != SPACE && exp.getRemainingCount() > 1)) {
873+
exp.starts();
874+
throw new InvalidParamException("invalid-color", ac);
875+
}
876+
if (val.getType() == CssTypes.CSS_NUMBER) {
877+
lab.setB(ac, val);
878+
} else {
879+
exp.starts();
880+
throw new InvalidParamException("rgb", val, ac); // FIXME lab
881+
}
882+
hwb.normalize();
883+
884+
// Alpha
885+
exp.next();
886+
val = exp.getValue();
887+
if (val != null) {
888+
switch (val.getType()) {
889+
case CssTypes.CSS_NUMBER:
890+
case CssTypes.CSS_PERCENTAGE:
891+
lab.setAlpha(ac, val);
892+
break;
893+
default:
894+
exp.starts();
895+
throw new InvalidParamException("rgb", val, ac); // FIXME lab
896+
}
897+
}
898+
// extra values?
899+
exp.next();
900+
if (exp.getValue() != null) {
901+
exp.starts();
902+
throw new InvalidParamException("rgb", exp.getValue(), ac);
903+
}
904+
}
905+
906+
907+
public void setlchColor(CssExpression exp, ApplContext ac)
908+
throws InvalidParamException {
909+
// HWB defined in CSSColor Level 4 and onward, currently used in the CSS level
910+
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
911+
StringBuilder sb = new StringBuilder();
912+
sb.append("lch(").append(exp.toStringFromStart()).append(')');
913+
throw new InvalidParamException("notversion", sb.toString(),
914+
ac.getCssVersionString(), ac);
915+
}
916+
917+
color = null;
918+
lch = new LCH();
919+
CssValue val = exp.getValue();
920+
char op = exp.getOperator();
921+
// L
922+
if (val == null || op != SPACE) {
923+
throw new InvalidParamException("invalid-color", ac);
924+
}
925+
switch (val.getType()) {
926+
case CssTypes.CSS_NUMBER:
927+
lch.setL(ac, val);
928+
break;
929+
default:
930+
throw new InvalidParamException("rgb", val, ac); // FIXME lch
931+
}
932+
933+
// A
934+
exp.next();
935+
val = exp.getValue();
936+
op = exp.getOperator();
937+
if (val == null || op != SPACE) {
938+
exp.starts();
939+
throw new InvalidParamException("invalid-color", ac);
940+
}
941+
if (val.getType() == CssTypes.CSS_NUMBER) {
942+
lch.setC(ac, val);
943+
} else {
944+
exp.starts();
945+
throw new InvalidParamException("rgb", val, ac); // FIXME lch
946+
}
947+
948+
// B
949+
exp.next();
950+
val = exp.getValue();
951+
op = exp.getOperator();
952+
if (val == null || (op != SPACE && exp.getRemainingCount() > 1)) {
953+
exp.starts();
954+
throw new InvalidParamException("invalid-color", ac);
955+
}
956+
if (val.getType() == CssTypes.CSS_NUMBER) {
957+
lch.setH(ac, val);
958+
} else {
959+
exp.starts();
960+
throw new InvalidParamException("rgb", val, ac); // FIXME lch
961+
}
962+
hwb.normalize();
963+
964+
// Alpha
965+
exp.next();
966+
val = exp.getValue();
967+
if (val != null) {
968+
switch (val.getType()) {
969+
case CssTypes.CSS_NUMBER:
970+
case CssTypes.CSS_PERCENTAGE:
971+
lch.setAlpha(ac, val);
972+
break;
973+
default:
974+
exp.starts();
975+
throw new InvalidParamException("rgb", val, ac); // FIXME lch
976+
}
977+
}
978+
// extra values?
979+
exp.next();
980+
if (exp.getValue() != null) {
981+
exp.starts();
982+
throw new InvalidParamException("rgb", exp.getValue(), ac);
983+
}
984+
}
985+
823986
}
824987

org/w3c/css/values/LAB.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
import org.w3c.css.util.ApplContext;
1010
import org.w3c.css.util.InvalidParamException;
1111

12-
import java.math.BigDecimal;
13-
1412
public class LAB {
1513
String output = null;
1614
CssValue vl, va, vb, alpha;
1715
boolean faSet = false;
1816

1917
/**
20-
* Create a new HSL
18+
* Create a new LAB
2119
*/
2220
public LAB() {
2321
}
@@ -36,6 +34,8 @@ public static final CssValue filterL(ApplContext ac, CssValue val)
3634
nb.setIntValue(0);
3735
return nb;
3836
}
37+
/*
38+
// L value is not clamped for now, but maybe in the future.
3939
if (val.getRawType() == CssTypes.CSS_NUMBER) {
4040
BigDecimal pp = ((CssNumber) val).value;
4141
if (pp.compareTo(HWB.s100) > 0) {
@@ -45,6 +45,7 @@ public static final CssValue filterL(ApplContext ac, CssValue val)
4545
return nb;
4646
}
4747
}
48+
*/
4849
}
4950
}
5051
return val;

org/w3c/css/values/LCH.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,11 @@ public class LCH {
1717
boolean faSet = false;
1818

1919
/**
20-
* Create a new HSL
20+
* Create a new LCH
2121
*/
2222
public LCH() {
2323
}
2424

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-
5325
public static final CssValue filterC(ApplContext ac, CssValue val)
5426
throws InvalidParamException {
5527
if (val.getRawType() == CssTypes.CSS_CALC) {
@@ -100,7 +72,7 @@ public static final CssValue filterH(ApplContext ac, CssValue val)
10072
public final void setL(ApplContext ac, CssValue val)
10173
throws InvalidParamException {
10274
output = null;
103-
vl = filterL(ac, val);
75+
vl = LAB.filterL(ac, val);
10476

10577
}
10678

0 commit comments

Comments
 (0)