Skip to content

Commit d56e63d

Browse files
committed
new parsing of rgb() and rgba() based on css-color-4 (no stable pointer yet)
1 parent cb334fe commit d56e63d

File tree

1 file changed

+133
-35
lines changed

1 file changed

+133
-35
lines changed

org/w3c/css/values/CssColor.java

Lines changed: 133 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import org.w3c.css.util.ApplContext;
1111
import org.w3c.css.util.CssVersion;
1212
import org.w3c.css.util.InvalidParamException;
13-
import org.w3c.css.util.Util;
1413

1514
import java.util.HashMap;
1615

1716
import static org.w3c.css.values.CssOperator.COMMA;
17+
import static org.w3c.css.values.CssOperator.SPACE;
1818

1919
/**
2020
* @version $Revision$
@@ -96,11 +96,22 @@ public String toString() {
9696
}
9797
}
9898

99+
100+
public void setRGBColor(CssExpression exp, ApplContext ac)
101+
throws InvalidParamException {
102+
boolean isCss3 = (ac.getCssVersion().compareTo(CssVersion.CSS3) >= 0);
103+
if (!isCss3) {
104+
setLegacyRGBColor(exp, ac);
105+
} else {
106+
setModernRGBColor(exp, ac);
107+
}
108+
}
109+
99110
/**
100111
* Parse a RGB color.
101112
* format rgb(<num>%?, <num>%?, <num>%?)
102113
*/
103-
public void setRGBColor(CssExpression exp, ApplContext ac)
114+
public void setLegacyRGBColor(CssExpression exp, ApplContext ac)
104115
throws InvalidParamException {
105116
CssValue val = exp.getValue();
106117
char op = exp.getOperator();
@@ -180,6 +191,124 @@ public void setRGBColor(CssExpression exp, ApplContext ac)
180191
}
181192
}
182193

194+
/**
195+
* Parse a RGB color.
196+
* format rgb( <percentage>{3} [ / <alpha-value> ]? ) |
197+
* rgb( <number>{3} [ / <alpha-value> ]? )
198+
*/
199+
public void setModernRGBColor(CssExpression exp, ApplContext ac)
200+
throws InvalidParamException {
201+
CssValue val = exp.getValue();
202+
char op = exp.getOperator();
203+
color = null;
204+
rgba = new RGBA("rgb");
205+
boolean separator_space = (op == SPACE);
206+
207+
if (val == null || (!separator_space && (op != COMMA))) {
208+
throw new InvalidParamException("invalid-color", ac);
209+
}
210+
211+
switch (val.getType()) {
212+
case CssTypes.CSS_NUMBER:
213+
rgba.setPercent(false);
214+
rgba.setRed(ac, val);
215+
break;
216+
case CssTypes.CSS_PERCENTAGE:
217+
rgba.setPercent(true);
218+
rgba.setRed(ac, val);
219+
break;
220+
default:
221+
throw new InvalidParamException("rgb", val, ac);
222+
}
223+
224+
exp.next();
225+
val = exp.getValue();
226+
op = exp.getOperator();
227+
228+
if (val == null || (separator_space && (op != SPACE)) ||
229+
(!separator_space && (op != COMMA))) {
230+
throw new InvalidParamException("invalid-color", ac);
231+
}
232+
233+
switch (val.getType()) {
234+
case CssTypes.CSS_NUMBER:
235+
if (rgba.isPercent()) {
236+
throw new InvalidParamException("percent", val, ac);
237+
}
238+
rgba.setGreen(ac, val);
239+
break;
240+
case CssTypes.CSS_PERCENTAGE:
241+
if (!rgba.isPercent()) {
242+
throw new InvalidParamException("integer", val, ac);
243+
}
244+
rgba.setGreen(ac, val);
245+
break;
246+
default:
247+
throw new InvalidParamException("rgb", val, ac);
248+
}
249+
250+
exp.next();
251+
val = exp.getValue();
252+
op = exp.getOperator();
253+
254+
if (val == null) {
255+
throw new InvalidParamException("invalid-color", ac);
256+
}
257+
258+
switch (val.getType()) {
259+
case CssTypes.CSS_NUMBER:
260+
if (rgba.isPercent()) {
261+
throw new InvalidParamException("percent", val, ac);
262+
}
263+
rgba.setBlue(ac, val);
264+
break;
265+
case CssTypes.CSS_PERCENTAGE:
266+
if (!rgba.isPercent()) {
267+
throw new InvalidParamException("integer", val, ac);
268+
}
269+
rgba.setBlue(ac, val);
270+
break;
271+
default:
272+
throw new InvalidParamException("rgb", val, ac);
273+
}
274+
exp.next();
275+
276+
if (!exp.end()) {
277+
if (op != SPACE) {
278+
throw new InvalidParamException("invalid-color", ac);
279+
}
280+
// now we need an alpha.
281+
val = exp.getValue();
282+
op = exp.getOperator();
283+
284+
if (val.getType() != CssTypes.CSS_SWITCH) {
285+
throw new InvalidParamException("rgb", val, ac);
286+
}
287+
if (op != SPACE) {
288+
throw new InvalidParamException("invalid-color", ac);
289+
}
290+
exp.next();
291+
// now we get the alpha value
292+
if (exp.end()) {
293+
throw new InvalidParamException("rgb", exp.getValue(), ac);
294+
}
295+
val = exp.getValue();
296+
switch (val.getType()) {
297+
case CssTypes.CSS_NUMBER:
298+
case CssTypes.CSS_PERCENTAGE:
299+
rgba.setAlpha(ac, val);
300+
break;
301+
default:
302+
throw new InvalidParamException("rgb", val, ac);
303+
}
304+
exp.next();
305+
306+
if (!exp.end()) {
307+
throw new InvalidParamException("rgb", exp.getValue(), ac);
308+
}
309+
}
310+
}
311+
183312
/**
184313
* Parse a RGB color.
185314
* format #(3-6)<hexnum>
@@ -372,10 +501,10 @@ public void setRGBAColor(CssExpression exp, ApplContext ac)
372501
throw new InvalidParamException("notversion", sb.toString(),
373502
ac.getCssVersionString(), ac);
374503
}
375-
rgba = new RGBA();
376-
__setRGBAColor(rgba, exp, ac);
504+
setModernRGBColor(exp,ac);
377505
}
378506

507+
// use only for atsc profile, superseded by setModernRGBColor
379508
private void __setRGBAColor(RGBA rgba, CssExpression exp, ApplContext ac)
380509
throws InvalidParamException {
381510
CssValue val;
@@ -709,36 +838,5 @@ public void setHWBColor(CssExpression exp, ApplContext ac)
709838
}
710839
}
711840

712-
private int clippedIntValue(int rgb, ApplContext ac) {
713-
if (rgb < 0 || rgb > 255) {
714-
ac.getFrame().addWarning("out-of-range", Util.displayFloat(rgb));
715-
return (rgb < 0) ? 0 : 255;
716-
}
717-
return rgb;
718-
}
719-
720-
private float clippedPercentValue(float p, ApplContext ac) {
721-
if (p < 0. || p > 100.) {
722-
ac.getFrame().addWarning("out-of-range", Util.displayFloat(p));
723-
return (p < 0.) ? (float) 0 : (float) 100;
724-
}
725-
return p;
726-
}
727-
728-
private float clippedAlphaValue(float p, ApplContext ac) {
729-
if (p < 0. || p > 1.) {
730-
ac.getFrame().addWarning("out-of-range", Util.displayFloat(p));
731-
return (float) ((p < 0.) ? 0. : 1.);
732-
}
733-
return p;
734-
}
735-
736-
private float angleValue(float p, ApplContext ac) {
737-
if (p < 0.0 || p >= 360.0) {
738-
ac.getFrame().addWarning("out-of-range", Util.displayFloat(p));
739-
}
740-
return p;
741-
}
742-
743841
}
744842

0 commit comments

Comments
 (0)