Skip to content

Commit ee35027

Browse files
committed
Redone HSLA support to use CssValues
1 parent a23a1b4 commit ee35027

2 files changed

Lines changed: 34 additions & 59 deletions

File tree

org/w3c/css/values/CssColor.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public void setHSLColor(CssExpression exp, ApplContext ac)
499499
switch (val.getType()) {
500500
case CssTypes.CSS_ANGLE:
501501
case CssTypes.CSS_NUMBER:
502-
hsl.setHue(ac, val);
502+
hsl.setHue(ac, val);
503503
break;
504504
default:
505505
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
@@ -564,11 +564,13 @@ public void setHSLAColor(CssExpression exp, ApplContext ac)
564564
if (val == null || op != COMMA) {
565565
throw new InvalidParamException("invalid-color", ac);
566566
}
567-
if (val.getType() == CssTypes.CSS_NUMBER) {
568-
CssNumber number = (CssNumber) val;
569-
hsla.setHue(angleValue(number.getValue(), ac));
570-
} else {
571-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
567+
switch (val.getType()) {
568+
case CssTypes.CSS_ANGLE:
569+
case CssTypes.CSS_NUMBER:
570+
hsla.setHue(ac, val);
571+
break;
572+
default:
573+
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
572574
}
573575

574576
// S
@@ -580,8 +582,7 @@ public void setHSLAColor(CssExpression exp, ApplContext ac)
580582
throw new InvalidParamException("invalid-color", ac);
581583
}
582584
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
583-
CssPercentage percent = (CssPercentage) val;
584-
hsla.setSaturation(clippedPercentValue(percent.floatValue(), ac));
585+
hsla.setSaturation(ac, val);
585586
} else {
586587
exp.starts();
587588
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
@@ -596,8 +597,7 @@ public void setHSLAColor(CssExpression exp, ApplContext ac)
596597
throw new InvalidParamException("invalid-color", ac);
597598
}
598599
if (val.getType() == CssTypes.CSS_PERCENTAGE) {
599-
CssPercentage percent = (CssPercentage) val;
600-
hsla.setLightness(clippedPercentValue(percent.floatValue(), ac));
600+
hsla.setLightness(ac, val);
601601
} else {
602602
exp.starts();
603603
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
@@ -610,12 +610,15 @@ public void setHSLAColor(CssExpression exp, ApplContext ac)
610610
exp.starts();
611611
throw new InvalidParamException("invalid-color", ac);
612612
}
613-
if (val.getType() == CssTypes.CSS_NUMBER) {
614-
CssNumber number = (CssNumber) val;
615-
hsla.setAlpha(clippedAlphaValue(number.getValue(), ac));
616-
} else {
617-
exp.starts();
618-
throw new InvalidParamException("rgb", val, ac); // FIXME hsl
613+
switch (val.getType()) {
614+
case CssTypes.CSS_NUMBER:
615+
// starting with CSS Color 4
616+
case CssTypes.CSS_PERCENTAGE:
617+
hsla.setAlpha(ac, val);
618+
break;
619+
default:
620+
exp.starts();
621+
throw new InvalidParamException("rgb", val, ac); // FIXME rgba
619622
}
620623
// extra values?
621624
exp.next();

org/w3c/css/values/HSLA.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,37 @@
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;
1718

18-
public class HSLA {
19-
String output = null;
20-
float fh;
21-
float fs;
22-
float fl;
23-
float fa;
19+
public class HSLA extends HSL {
20+
CssValue va;
2421

2522
/**
26-
* Create a new HSL
23+
* Create a new HSLA
2724
*/
2825
public HSLA() {
2926
}
3027

31-
public void setHue(float hue) {
32-
this.fh = (float) ((((double) hue % 360.0) + 360.0) % 360.0);
33-
}
34-
35-
public void setHue(CssNumber hue) {
36-
setHue(hue.getValue());
37-
}
38-
39-
public void setSaturation(float sat) {
40-
this.fs = sat;
41-
}
42-
43-
public void setSaturation(CssNumber sat) {
44-
setSaturation(sat.getValue());
45-
}
46-
47-
public void setLightness(float light) {
48-
this.fl = light;
49-
}
50-
51-
public void setLightness(CssNumber light) {
52-
setLightness(light.getValue());
53-
}
54-
55-
public void setAlpha(float alpha) {
56-
this.fa = alpha;
57-
}
58-
59-
public void setAlpha(CssNumber alpha) {
60-
setAlpha(alpha.getValue());
28+
public void setAlpha(ApplContext ac, CssValue alpha)
29+
throws InvalidParamException {
30+
output = null;
31+
va = RGBA.filterAlpha(ac, alpha);
6132
}
6233

6334
/**
6435
* Returns a string representation of the object.
6536
*/
6637
public String toString() {
6738
if (output == null) {
68-
StringBuilder sb = new StringBuilder("hsla(");
69-
sb.append(Util.displayFloat(fh)).append(", ");
70-
sb.append(Util.displayFloat(fs)).append("%, ");
71-
sb.append(Util.displayFloat(fl)).append("%, ");
72-
sb.append(Util.displayFloat(fa)).append(')');
39+
StringBuilder sb = new StringBuilder("hsl(");
40+
sb.append(vh).append(", ");
41+
sb.append(vs).append(", ");
42+
sb.append(vl).append(", ");
43+
sb.append(va).append(")");
7344
output = sb.toString();
7445
}
7546
return output;
47+
7648
}
7749
}

0 commit comments

Comments
 (0)