|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2024 Ronald Brill. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.htmlunit.cssparser.dom; |
| 16 | + |
| 17 | +import java.io.Serializable; |
| 18 | +import java.util.Locale; |
| 19 | + |
| 20 | +import org.htmlunit.cssparser.parser.LexicalUnit; |
| 21 | +import org.htmlunit.cssparser.parser.LexicalUnit.LexicalUnitType; |
| 22 | +import org.w3c.dom.DOMException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Implementation of HWBColor. |
| 26 | + * |
| 27 | + * @author Ronald Brill |
| 28 | + */ |
| 29 | +public class HWBColorImpl implements Serializable { |
| 30 | + private CSSValueImpl hue_; |
| 31 | + private CSSValueImpl whiteness_; |
| 32 | + private CSSValueImpl blackness_; |
| 33 | + private CSSValueImpl alpha_; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor that reads the values from the given |
| 37 | + * chain of LexicalUnits. |
| 38 | + * @param function the name of the function; hwb |
| 39 | + * @param lu the values |
| 40 | + * @throws DOMException in case of error |
| 41 | + */ |
| 42 | + public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMException { |
| 43 | + if (function == null) { |
| 44 | + throw new DOMException(DOMException.SYNTAX_ERR, "Color space hwb is required."); |
| 45 | + } |
| 46 | + final String functionLC = function.toLowerCase(Locale.ROOT); |
| 47 | + if (!"hwb".equals(functionLC)) { |
| 48 | + throw new DOMException(DOMException.SYNTAX_ERR, "Color space '" + functionLC + "' not supported."); |
| 49 | + } |
| 50 | + |
| 51 | + LexicalUnit next = lu; |
| 52 | + if (next == null) { |
| 53 | + throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values."); |
| 54 | + } |
| 55 | + hue_ = new CSSValueImpl(next, true); |
| 56 | + |
| 57 | + next = next.getNextLexicalUnit(); |
| 58 | + if (next == null) { |
| 59 | + throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values."); |
| 60 | + } |
| 61 | + |
| 62 | + if (LexicalUnitType.NONE != next.getLexicalUnitType() |
| 63 | + && LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) { |
| 64 | + throw new DOMException(DOMException.SYNTAX_ERR, "Whiteness part has to be percentage."); |
| 65 | + } |
| 66 | + whiteness_ = new CSSValueImpl(next, true); |
| 67 | + |
| 68 | + next = next.getNextLexicalUnit(); |
| 69 | + if (next == null) { |
| 70 | + throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values."); |
| 71 | + } |
| 72 | + if (next.getLexicalUnitType() == LexicalUnitType.OPERATOR_COMMA) { |
| 73 | + throw new DOMException(DOMException.SYNTAX_ERR, |
| 74 | + "hwb requires consitent separators (blank or comma)."); |
| 75 | + } |
| 76 | + |
| 77 | + if (LexicalUnitType.NONE != next.getLexicalUnitType() |
| 78 | + && LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) { |
| 79 | + throw new DOMException(DOMException.SYNTAX_ERR, "Blackness part has to be percentage."); |
| 80 | + } |
| 81 | + blackness_ = new CSSValueImpl(next, true); |
| 82 | + next = next.getNextLexicalUnit(); |
| 83 | + if (next == null) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) { |
| 88 | + throw new DOMException(DOMException.SYNTAX_ERR, "hwb alpha value must be separated by '/'."); |
| 89 | + } |
| 90 | + next = next.getNextLexicalUnit(); |
| 91 | + if (next == null) { |
| 92 | + throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value."); |
| 93 | + } |
| 94 | + |
| 95 | + if (LexicalUnitType.INTEGER != next.getLexicalUnitType() |
| 96 | + && LexicalUnitType.REAL != next.getLexicalUnitType() |
| 97 | + && LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) { |
| 98 | + throw new DOMException(DOMException.SYNTAX_ERR, "Alpha part has to be numeric or percentage."); |
| 99 | + } |
| 100 | + alpha_ = new CSSValueImpl(next, true); |
| 101 | + |
| 102 | + next = next.getNextLexicalUnit(); |
| 103 | + if (next != null) { |
| 104 | + throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for hwb function."); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return the hue part. |
| 110 | + */ |
| 111 | + public CSSValueImpl getHue() { |
| 112 | + return hue_; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the hue part to a new value. |
| 117 | + * @param hue the new CSSValueImpl |
| 118 | + */ |
| 119 | + public void setHue(final CSSValueImpl hue) { |
| 120 | + hue_ = hue; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @return the whiteness part. |
| 125 | + */ |
| 126 | + public CSSValueImpl getWhiteness() { |
| 127 | + return whiteness_; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Sets the whiteness part to a new value. |
| 132 | + * @param whiteness the new CSSValueImpl |
| 133 | + */ |
| 134 | + public void setWhiteness(final CSSValueImpl whiteness) { |
| 135 | + whiteness_ = whiteness; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return the blackness part. |
| 140 | + */ |
| 141 | + public CSSValueImpl getBlackness() { |
| 142 | + return blackness_; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Sets the blackness part to a new value. |
| 147 | + * @param blackness the new CSSValueImpl |
| 148 | + */ |
| 149 | + public void setBlackness(final CSSValueImpl blackness) { |
| 150 | + blackness_ = blackness; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @return the alpha part. |
| 155 | + */ |
| 156 | + public CSSValueImpl getAlpha() { |
| 157 | + return alpha_; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the alpha part to a new value. |
| 162 | + * @param alpha the new CSSValueImpl |
| 163 | + */ |
| 164 | + public void setAlpha(final CSSValueImpl alpha) { |
| 165 | + alpha_ = alpha; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * {@inheritDoc} |
| 170 | + */ |
| 171 | + @Override |
| 172 | + public String toString() { |
| 173 | + final StringBuilder sb = new StringBuilder(); |
| 174 | + |
| 175 | + sb |
| 176 | + .append("hwb(") |
| 177 | + .append(hue_) |
| 178 | + .append(" ") |
| 179 | + .append(whiteness_) |
| 180 | + .append(" ") |
| 181 | + .append(blackness_); |
| 182 | + |
| 183 | + if (null != alpha_) { |
| 184 | + sb.append(" / ").append(alpha_); |
| 185 | + } |
| 186 | + |
| 187 | + sb.append(")"); |
| 188 | + |
| 189 | + return sb.toString(); |
| 190 | + } |
| 191 | +} |
0 commit comments