Skip to content

Commit 5b0d3f3

Browse files
committed
HWB redone using CssValues
1 parent 5ccd62d commit 5b0d3f3

File tree

3 files changed

+100
-71
lines changed

3 files changed

+100
-71
lines changed

org/w3c/css/values/CssColor.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,13 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
647647
if (val == null || op != COMMA) {
648648
throw new InvalidParamException("invalid-color", ac);
649649
}
650-
if (val.getType() == CssTypes.CSS_NUMBER) {
651-
CssNumber number = (CssNumber) val;
652-
hwb.setHue(angleValue(number.getValue(), ac));
653-
} else {
654-
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
650+
switch (val.getType()) {
651+
case CssTypes.CSS_ANGLE:
652+
case CssTypes.CSS_NUMBER:
653+
hwb.setHue(ac, val);
654+
break;
655+
default:
656+
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
655657
}
656658

657659
// W
@@ -663,13 +665,7 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
663665
throw new InvalidParamException("invalid-color", ac);
664666
}
665667
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
666-
CssPercentage percent = (CssPercentage) val;
667-
if (percent.floatValue() <= 100f && percent.getValue().signum() >= 0) {
668-
hwb.setWhiteness(percent.floatValue());
669-
} else {
670-
exp.starts();
671-
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
672-
}
668+
hwb.setWhiteness(ac, val);
673669
} else {
674670
exp.starts();
675671
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
@@ -684,29 +680,25 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
684680
throw new InvalidParamException("invalid-color", ac);
685681
}
686682
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
687-
CssPercentage percent = (CssPercentage) val;
688-
if (percent.floatValue() <= 100f && percent.getValue().signum() >= 0) {
689-
hwb.setBlackness(percent.floatValue());
690-
} else {
691-
exp.starts();
692-
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
693-
}
683+
hwb.setBlackness(ac, val);
694684
} else {
695685
exp.starts();
696686
throw new InvalidParamException("rgb", val, ac); // FIXME hwb
697687
}
698-
hwb.normalizeHW();
688+
hwb.normalize();
699689

700690
// A
701691
exp.next();
702692
val = exp.getValue();
703693
if (val != null) {
704-
if (val.getType() == CssTypes.CSS_NUMBER) {
705-
CssNumber number = (CssNumber) val;
706-
hwb.setAlpha(clippedAlphaValue(number.getValue(), ac));
707-
} else {
708-
exp.starts();
709-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
694+
switch (val.getType()) {
695+
case CssTypes.CSS_NUMBER:
696+
case CssTypes.CSS_PERCENTAGE:
697+
hwb.setAlpha(ac, val);
698+
break;
699+
default:
700+
exp.starts();
701+
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
710702
}
711703
}
712704
// extra values?

org/w3c/css/values/HSL.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public class HSL {
2929
public HSL() {
3030
}
3131

32-
public final CssValue filterValue(ApplContext ac, CssValue val)
32+
public static final CssValue filterValue(ApplContext ac, CssValue val)
3333
throws InvalidParamException {
34-
output = null;
3534
if (val.getRawType() == CssTypes.CSS_CALC) {
3635
// TODO add warning about uncheckability
3736
// might need to extend...
@@ -55,9 +54,8 @@ public final CssValue filterValue(ApplContext ac, CssValue val)
5554
return val;
5655
}
5756

58-
public final void setHue(ApplContext ac, CssValue val)
57+
public final static CssValue filterHue(ApplContext ac, CssValue val)
5958
throws InvalidParamException {
60-
output = null;
6159
if (val.getRawType() == CssTypes.CSS_CALC) {
6260
// TODO add warning about uncheckability
6361
// might need to extend...
@@ -71,8 +69,7 @@ public final void setHue(ApplContext ac, CssValue val)
7169
float p = ((CssNumber) val).getValue();
7270
CssNumber nb = new CssNumber();
7371
nb.setFloatValue((float) ((((double) p % 360.0) + 360.0) % 360.0));
74-
vh = nb;
75-
return;
72+
return nb;
7673
}
7774
}
7875
if (val.getRawType() == CssTypes.CSS_NUMBER) {
@@ -81,8 +78,7 @@ public final void setHue(ApplContext ac, CssValue val)
8178
ac.getFrame().addWarning("out-of-range", Util.displayFloat(p));
8279
CssNumber nb = new CssNumber();
8380
nb.setFloatValue((float) ((((double) p % 360.0) + 360.0) % 360.0));
84-
vh = nb;
85-
return;
81+
return nb;
8682
}
8783
}
8884
} else if (val.getType() == CssTypes.CSS_ANGLE) {
@@ -102,16 +98,25 @@ public final void setHue(ApplContext ac, CssValue val)
10298
}
10399
}
104100
}
105-
vh = val;
101+
return val;
102+
}
103+
104+
public final void setHue(ApplContext ac, CssValue val)
105+
throws InvalidParamException {
106+
output = null;
107+
vh = filterHue(ac, val);
108+
106109
}
107110

108111
public final void setSaturation(ApplContext ac, CssValue val)
109112
throws InvalidParamException {
113+
output = null;
110114
vs = filterValue(ac, val);
111115
}
112116

113117
public final void setLightness(ApplContext ac, CssValue val)
114118
throws InvalidParamException {
119+
output = null;
115120
vl = filterValue(ac, val);
116121
}
117122

org/w3c/css/values/HWB.java

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,75 +13,107 @@
1313
*/
1414
package org.w3c.css.values;
1515

16-
import org.w3c.css.util.Util;
16+
import org.w3c.css.util.ApplContext;
17+
import org.w3c.css.util.InvalidParamException;
18+
19+
import java.math.BigDecimal;
1720

1821
public class HWB {
1922
String output = null;
20-
float fh;
21-
float fw = 100.f;
22-
float fb = 100.f;
23-
float fa = 100.f;
2423
boolean faSet = false;
2524

25+
CssValue vh, vw, vb, va;
26+
27+
static final BigDecimal s100 = new BigDecimal(100);
28+
2629
/**
2730
* Create a new HWB
2831
*/
2932
public HWB() {
3033
}
3134

32-
public void setHue(float hue) {
33-
this.fh = (float) ((((double) hue % 360.0) + 360.0) % 360.0);
34-
}
35-
36-
public void setHue(CssNumber hue) {
37-
setHue(hue.getValue());
38-
}
39-
40-
public void setWhiteness(float sat) {
41-
this.fw = sat;
42-
}
4335

44-
public void setWhiteness(CssNumber sat) {
45-
setWhiteness(sat.getValue());
36+
public static final CssValue filterValue(ApplContext ac, CssValue val)
37+
throws InvalidParamException {
38+
if (val.getRawType() == CssTypes.CSS_CALC) {
39+
// TODO add warning about uncheckability
40+
// might need to extend...
41+
} else {
42+
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
43+
CssCheckableValue v = val.getCheckableValue();
44+
v.checkPositiveness(ac, "RGB");
45+
if (val.getRawType() == CssTypes.CSS_PERCENTAGE) {
46+
float p = ((CssPercentage) val).floatValue();
47+
if (p > 100.) {
48+
throw new InvalidParamException("range", val, ac);
49+
}
50+
}
51+
}
52+
}
53+
return val;
4654
}
4755

48-
public void setBlackness(float light) {
49-
this.fb = light;
56+
public final void setHue(ApplContext ac, CssValue val)
57+
throws InvalidParamException {
58+
output = null;
59+
vh = HSL.filterHue(ac, val);
5060
}
5161

52-
public void setBlackness(CssNumber light) {
53-
setBlackness(light.getValue());
62+
public final void setWhiteness(ApplContext ac, CssValue val)
63+
throws InvalidParamException {
64+
output = null;
65+
vw = filterValue(ac, val);
5466
}
5567

56-
public void setAlpha(float alpha) {
57-
this.fa = alpha;
58-
this.faSet = true;
68+
public final void setBlackness(ApplContext ac, CssValue val)
69+
throws InvalidParamException {
70+
output = null;
71+
vb = filterValue(ac, val);
5972
}
6073

61-
public void setAlpha(CssNumber alpha) {
62-
setAlpha(alpha.getValue());
74+
public final void setAlpha(ApplContext ac, CssValue val)
75+
throws InvalidParamException {
76+
output = null;
77+
faSet = true;
78+
vb = RGBA.filterAlpha(ac, val);
6379
}
6480

65-
public void normalizeHW() {
66-
if (fw + fb > 100.f) {
67-
81+
public void normalize() {
82+
if (vw == null || vb == null) {
83+
return;
84+
}
85+
if (vw.getRawType() == CssTypes.CSS_PERCENTAGE &&
86+
vb.getRawType() == CssTypes.CSS_PERCENTAGE) {
87+
CssPercentage pw, pb;
88+
BigDecimal w, b, s;
89+
pw = (CssPercentage) vw;
90+
pb = (CssPercentage) vb;
91+
w = pw.getValue();
92+
b = pb.getValue();
93+
s = w.add(b);
94+
if (s.compareTo(s100) != 0) {
95+
w = w.divide(s, BigDecimal.ROUND_HALF_UP).multiply(s100);
96+
b = b.divide(s, BigDecimal.ROUND_HALF_UP).multiply(s100);
97+
pw.setValue(w);
98+
pb.setValue(b);
99+
}
68100
}
69-
70101
}
71102

72103
/**
73104
* Returns a string representation of the object.
74105
*/
75106
public String toString() {
76107
if (output == null) {
108+
normalize();
77109
StringBuilder sb = new StringBuilder("hwb(");
78-
sb.append(Util.displayFloat(fh)).append(", ");
79-
sb.append(Util.displayFloat(fw)).append("%, ");
80-
sb.append(Util.displayFloat(fb));
110+
sb.append(vh).append(", ");
111+
sb.append(vw).append(", ");
112+
sb.append(vb);
81113
if (faSet) {
82-
sb.append("%)");
114+
sb.append(")");
83115
} else {
84-
sb.append("%, ").append(Util.displayFloat(fa)).append(')');
116+
sb.append(", ").append(va).append(')');
85117
}
86118
output = sb.toString();
87119
}

0 commit comments

Comments
 (0)