Skip to content

Commit 9dbad60

Browse files
committed
more progress on color rewrite, rb and rgba done, including relative color handling
1 parent b710fd6 commit 9dbad60

File tree

15 files changed

+778
-685
lines changed

15 files changed

+778
-685
lines changed

org/w3c/css/values/CssColor.java

Lines changed: 10 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import org.w3c.css.util.CssVersion;
1212
import org.w3c.css.util.InvalidParamException;
1313
import org.w3c.css.values.color.ColorMix;
14+
import org.w3c.css.values.color.CssColorCSS1;
15+
import org.w3c.css.values.color.CssColorCSS2;
16+
import org.w3c.css.values.color.CssColorCSS21;
17+
import org.w3c.css.values.color.CssColorCSS3;
1418
import org.w3c.css.values.color.DeviceCMYK;
1519
import org.w3c.css.values.color.HSL;
1620
import org.w3c.css.values.color.HWB;
@@ -19,11 +23,12 @@
1923
import org.w3c.css.values.color.LightDark;
2024
import org.w3c.css.values.color.OKLAB;
2125
import org.w3c.css.values.color.OKLCH;
26+
import org.w3c.css.values.color.RGB;
27+
import org.w3c.css.values.color.RGBA;
2228

2329
import java.util.Locale;
2430

2531
import static org.w3c.css.values.CssOperator.COMMA;
26-
import static org.w3c.css.values.CssOperator.SPACE;
2732

2833
/**
2934
* @version $Revision$
@@ -143,7 +148,7 @@ public void setRGBColor(ApplContext ac, CssExpression exp)
143148
if (!isCss3) {
144149
setLegacyRGBColor(ac, exp);
145150
} else {
146-
setModernRGBColor(ac, exp);
151+
rgba = RGBA.parseModernRGBA(ac, exp, this, "rgb(");
147152
}
148153
}
149154

@@ -238,204 +243,8 @@ public void setLegacyRGBColor(ApplContext ac, CssExpression exp)
238243
*/
239244
public void setModernRGBColor(ApplContext ac, CssExpression exp)
240245
throws InvalidParamException {
241-
CssValue val;
242-
char op;
243-
color = null;
244-
rgba = new RGBA("rgb");
245-
boolean separator_space = true;
246-
247-
// check for variables
248-
if (exp.hasCssVariable()) {
249-
// don't check value then
250-
markCssVariable();
251-
252-
val = exp.getValue();
253-
op = exp.getOperator();
254-
separator_space = (op == SPACE);
255-
rgba.setModernStyle(separator_space);
256-
257-
if (val == null || (!separator_space && (op != COMMA))) {
258-
// don't throw, perhaps a warning instead? FIXME
259-
// throw new InvalidParamException("invalid-color", ac);
260-
}
261-
rgba.vr = val;
262-
263-
exp.next();
264-
val = exp.getValue();
265-
op = exp.getOperator();
266-
267-
if (val == null || (separator_space && (op != SPACE)) ||
268-
(!separator_space && (op != COMMA))) {
269-
// don't throw, perhaps a warning instead? FIXME
270-
// throw new InvalidParamException("invalid-color", ac);
271-
}
272-
rgba.vg = val;
273-
exp.next();
274-
val = exp.getValue();
275-
op = exp.getOperator();
276-
277-
rgba.vb = val;
278-
exp.next();
279-
280-
if (!exp.end()) {
281-
// care for old syntax
282-
if (op == COMMA && !separator_space) {
283-
val = exp.getValue();
284-
rgba.va = val;
285-
} else {
286-
// otherwise modern syntax
287-
if (op != SPACE) {
288-
// don't throw, perhaps a warning instead? FIXME
289-
// throw new InvalidParamException("invalid-color", ac);
290-
}
291-
// now we need an alpha.
292-
val = exp.getValue();
293-
op = exp.getOperator();
294-
295-
if (val.getType() != CssTypes.CSS_SWITCH) {
296-
// don't throw, perhaps a warning instead? FIXME
297-
// throw new InvalidParamException("rgb", val, ac);
298-
}
299-
if (op != SPACE) {
300-
// don't throw, perhaps a warning instead? FIXME
301-
// throw new InvalidParamException("invalid-color", ac);
302-
}
303-
exp.next();
304-
// now we get the alpha value
305-
if (exp.end()) {
306-
// don't throw, perhaps a warning instead? FIXME
307-
// throw new InvalidParamException("rgb", exp.getValue(), ac);
308-
}
309-
val = exp.getValue();
310-
rgba.va = val;
311-
}
312-
exp.next();
313-
}
314-
return;
315-
}
316-
317-
val = exp.getValue();
318-
op = exp.getOperator();
319-
separator_space = (op == SPACE);
320-
rgba.setModernStyle(separator_space);
321-
322-
if (val == null || (!separator_space && (op != COMMA))) {
323-
throw new InvalidParamException("invalid-color", ac);
324-
}
325-
switch (val.getType()) {
326-
case CssTypes.CSS_NUMBER:
327-
rgba.setPercent(false);
328-
rgba.setRed(ac, val);
329-
break;
330-
case CssTypes.CSS_PERCENTAGE:
331-
rgba.setPercent(true);
332-
rgba.setRed(ac, val);
333-
break;
334-
default:
335-
throw new InvalidParamException("rgb", val, ac);
336-
}
337-
338-
exp.next();
339-
val = exp.getValue();
340-
op = exp.getOperator();
341-
342-
if (val == null || (separator_space && (op != SPACE)) ||
343-
(!separator_space && (op != COMMA))) {
344-
throw new InvalidParamException("invalid-color", ac);
345-
}
346-
347-
switch (val.getType()) {
348-
case CssTypes.CSS_NUMBER:
349-
if (rgba.isPercent()) {
350-
throw new InvalidParamException("percent", val, ac);
351-
}
352-
rgba.setGreen(ac, val);
353-
break;
354-
case CssTypes.CSS_PERCENTAGE:
355-
if (!rgba.isPercent()) {
356-
throw new InvalidParamException("integer", val, ac);
357-
}
358-
rgba.setGreen(ac, val);
359-
break;
360-
default:
361-
throw new InvalidParamException("rgb", val, ac);
362-
}
363246

364-
exp.next();
365-
val = exp.getValue();
366-
op = exp.getOperator();
367-
368-
if (val == null) {
369-
throw new InvalidParamException("invalid-color", ac);
370-
}
371-
372-
switch (val.getType()) {
373-
case CssTypes.CSS_NUMBER:
374-
if (rgba.isPercent()) {
375-
throw new InvalidParamException("percent", val, ac);
376-
}
377-
rgba.setBlue(ac, val);
378-
break;
379-
case CssTypes.CSS_PERCENTAGE:
380-
if (!rgba.isPercent()) {
381-
throw new InvalidParamException("integer", val, ac);
382-
}
383-
rgba.setBlue(ac, val);
384-
break;
385-
default:
386-
throw new InvalidParamException("rgb", val, ac);
387-
}
388-
exp.next();
389-
390-
// check for optional alpha channel
391-
if (!exp.end()) {
392-
// care for old syntax
393-
if (op == COMMA && !separator_space) {
394-
val = exp.getValue();
395-
switch (val.getType()) {
396-
case CssTypes.CSS_NUMBER:
397-
case CssTypes.CSS_PERCENTAGE:
398-
rgba.setAlpha(ac, val);
399-
break;
400-
default:
401-
throw new InvalidParamException("rgb", val, ac);
402-
}
403-
} else {
404-
// otherwise modern syntax
405-
if (op != SPACE) {
406-
throw new InvalidParamException("invalid-color", ac);
407-
}
408-
// now we need an alpha.
409-
val = exp.getValue();
410-
op = exp.getOperator();
411-
412-
if (val.getType() != CssTypes.CSS_SWITCH) {
413-
throw new InvalidParamException("rgb", val, ac);
414-
}
415-
if (op != SPACE) {
416-
throw new InvalidParamException("invalid-color", ac);
417-
}
418-
exp.next();
419-
// now we get the alpha value
420-
if (exp.end()) {
421-
throw new InvalidParamException("rgb", exp.getValue(), ac);
422-
}
423-
val = exp.getValue();
424-
switch (val.getType()) {
425-
case CssTypes.CSS_NUMBER:
426-
case CssTypes.CSS_PERCENTAGE:
427-
rgba.setAlpha(ac, val);
428-
break;
429-
default:
430-
throw new InvalidParamException("rgb", val, ac);
431-
}
432-
}
433-
exp.next();
434-
435-
if (!exp.end()) {
436-
throw new InvalidParamException("rgb", exp.getValue(), ac);
437-
}
438-
}
247+
rgba = RGBA.parseModernRGBA(ac, exp, this, "rgb");
439248
}
440249

441250
/**
@@ -461,75 +270,8 @@ public void setHSLAColor(ApplContext ac, CssExpression exp)
461270
*/
462271
public void setShortRGBColor(ApplContext ac, String s)
463272
throws InvalidParamException {
464-
int r;
465-
int g;
466-
int b;
467-
int a;
468-
int v;
469-
int idx;
470-
boolean islong;
471-
boolean hasAlpha = false;
472-
boolean isCss3;
473-
474-
isCss3 = (ac.getCssVersion().compareTo(CssVersion.CSS3) >= 0);
475-
idx = 1;
476-
a = 0;
477-
switch (s.length()) {
478-
case 5:
479-
a = Character.digit(s.charAt(4), 16);
480-
if (!isCss3 || a < 0) {
481-
throw new InvalidParamException("rgb", s, ac);
482-
}
483-
hasAlpha = true;
484-
case 4:
485-
r = Character.digit(s.charAt(idx++), 16);
486-
g = Character.digit(s.charAt(idx++), 16);
487-
b = Character.digit(s.charAt(idx++), 16);
488-
if (r < 0 || g < 0 || b < 0) {
489-
throw new InvalidParamException("rgb", s, ac);
490-
}
491-
break;
492-
case 9:
493-
a = Character.digit(s.charAt(7), 16);
494-
v = Character.digit(s.charAt(8), 16);
495-
if (!isCss3 || a < 0 || v < 0) {
496-
throw new InvalidParamException("rgb", s, ac);
497-
}
498-
a = (a << 4) + v;
499-
hasAlpha = true;
500-
case 7:
501-
r = Character.digit(s.charAt(idx++), 16);
502-
v = Character.digit(s.charAt(idx++), 16);
503-
if (r < 0 || v < 0) {
504-
throw new InvalidParamException("rgb", s, ac);
505-
}
506-
r = (r << 4) + v;
507-
g = Character.digit(s.charAt(idx++), 16);
508-
v = Character.digit(s.charAt(idx++), 16);
509-
if (g < 0 || v < 0) {
510-
throw new InvalidParamException("rgb", s, ac);
511-
}
512-
g = (g << 4) + v;
513-
b = Character.digit(s.charAt(idx++), 16);
514-
v = Character.digit(s.charAt(idx++), 16);
515-
if (b < 0 || v < 0) {
516-
throw new InvalidParamException("rgb", s, ac);
517-
}
518-
b = (b << 4) + v;
519-
break;
520-
default:
521-
throw new InvalidParamException("rgb", s, ac);
522-
}
523-
273+
rgba = RGBA.parseShortRGBColor(ac, s);
524274
color = null;
525-
if (hasAlpha) {
526-
rgba = new RGBA(isCss3, r, g, b, a);
527-
rgba.setRepresentationString(s);
528-
} else {
529-
rgb = new RGB(isCss3, r, g, b);
530-
// we force the specific display of it
531-
rgb.setRepresentationString(s);
532-
}
533275
}
534276

535277
/**
@@ -572,11 +314,6 @@ protected void setIdentColor(ApplContext ac, String s)
572314
case CSS_2015:
573315
case CSS:
574316
// test RGB colors, RGBA colors (transparent), deprecated system colors
575-
rgb = CssColorCSS3.getRGB(lower_s);
576-
if (rgb != null) {
577-
color = lower_s;
578-
break;
579-
}
580317
rgba = CssColorCSS3.getRGBA(lower_s);
581318
if (rgba != null) {
582319
color = lower_s;
@@ -644,14 +381,7 @@ public void setATSCRGBAColor(ApplContext ac, CssExpression exp)
644381

645382
public void setRGBAColor(ApplContext ac, CssExpression exp)
646383
throws InvalidParamException {
647-
// RGBA defined in CSS3 and onward
648-
if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
649-
StringBuilder sb = new StringBuilder();
650-
sb.append("rgba(").append(exp.toStringFromStart()).append(')');
651-
throw new InvalidParamException("notversion", sb.toString(),
652-
ac.getCssVersionString(), ac);
653-
}
654-
setModernRGBColor(ac, exp);
384+
rgba = RGBA.parseModernRGBA(ac, exp, this, "rgba(");
655385
}
656386

657387
// use only for atsc profile, superseded by setModernRGBColor

0 commit comments

Comments
 (0)