|
13 | 13 | */ |
14 | 14 | package org.w3c.css.values; |
15 | 15 |
|
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; |
17 | 20 |
|
18 | 21 | public class HWB { |
19 | 22 | String output = null; |
20 | | - float fh; |
21 | | - float fw = 100.f; |
22 | | - float fb = 100.f; |
23 | | - float fa = 100.f; |
24 | 23 | boolean faSet = false; |
25 | 24 |
|
| 25 | + CssValue vh, vw, vb, va; |
| 26 | + |
| 27 | + static final BigDecimal s100 = new BigDecimal(100); |
| 28 | + |
26 | 29 | /** |
27 | 30 | * Create a new HWB |
28 | 31 | */ |
29 | 32 | public HWB() { |
30 | 33 | } |
31 | 34 |
|
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 | | - } |
43 | 35 |
|
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; |
46 | 54 | } |
47 | 55 |
|
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); |
50 | 60 | } |
51 | 61 |
|
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); |
54 | 66 | } |
55 | 67 |
|
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); |
59 | 72 | } |
60 | 73 |
|
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); |
63 | 79 | } |
64 | 80 |
|
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 | + } |
68 | 100 | } |
69 | | - |
70 | 101 | } |
71 | 102 |
|
72 | 103 | /** |
73 | 104 | * Returns a string representation of the object. |
74 | 105 | */ |
75 | 106 | public String toString() { |
76 | 107 | if (output == null) { |
| 108 | + normalize(); |
77 | 109 | 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); |
81 | 113 | if (faSet) { |
82 | | - sb.append("%)"); |
| 114 | + sb.append(")"); |
83 | 115 | } else { |
84 | | - sb.append("%, ").append(Util.displayFloat(fa)).append(')'); |
| 116 | + sb.append(", ").append(va).append(')'); |
85 | 117 | } |
86 | 118 | output = sb.toString(); |
87 | 119 | } |
|
0 commit comments