1313 */
1414package org .w3c .css .values ;
1515
16+ import org .w3c .css .util .ApplContext ;
17+ import org .w3c .css .util .InvalidParamException ;
1618import org .w3c .css .util .Util ;
1719
20+ import java .math .BigDecimal ;
21+
1822public class HSL {
1923 String output = null ;
20- float fh ;
21- float fs ;
22- float fl ;
24+ CssValue vh , vs , vl ;
2325
2426 /**
2527 * Create a new HSL
2628 */
2729 public HSL () {
2830 }
2931
30- public void setHue (float hue ) {
31- this .fh = (float ) ((((double ) hue % 360.0 ) + 360.0 ) % 360.0 );
32- }
33-
34- public void setHue (CssNumber hue ) {
35- setHue (hue .getValue ());
36- }
37-
38- public void setSaturation (float sat ) {
39- this .fs = sat ;
32+ public final CssValue filterValue (ApplContext ac , CssValue val )
33+ throws InvalidParamException {
34+ output = null ;
35+ if (val .getRawType () == CssTypes .CSS_CALC ) {
36+ // TODO add warning about uncheckability
37+ // might need to extend...
38+ } else {
39+ if (val .getType () == CssTypes .CSS_PERCENTAGE ) {
40+ CssCheckableValue v = val .getCheckableValue ();
41+ if (!v .warnPositiveness (ac , "RGB" )) {
42+ CssNumber nb = new CssNumber ();
43+ nb .setIntValue (0 );
44+ return nb ;
45+ }
46+ if (val .getRawType () == CssTypes .CSS_PERCENTAGE ) {
47+ float p = ((CssPercentage ) val ).floatValue ();
48+ if (p > 100. ) {
49+ ac .getFrame ().addWarning ("out-of-range" , Util .displayFloat (p ));
50+ return new CssPercentage (100 );
51+ }
52+ }
53+ }
54+ }
55+ return val ;
4056 }
4157
42- public void setSaturation (CssNumber sat ) {
43- setSaturation (sat .getValue ());
58+ public final void setHue (ApplContext ac , CssValue val )
59+ throws InvalidParamException {
60+ output = null ;
61+ if (val .getRawType () == CssTypes .CSS_CALC ) {
62+ // TODO add warning about uncheckability
63+ // might need to extend...
64+ } else {
65+ if (val .getType () == CssTypes .CSS_NUMBER ) {
66+ // numbers are treated as degrees
67+ CssCheckableValue v = val .getCheckableValue ();
68+ if (!v .isPositive ()) {
69+ ac .getFrame ().addWarning ("out-of-range" , val .toString ());
70+ if (val .getRawType () == CssTypes .CSS_NUMBER ) {
71+ float p = ((CssNumber ) val ).getValue ();
72+ CssNumber nb = new CssNumber ();
73+ nb .setFloatValue ((float ) ((((double ) p % 360.0 ) + 360.0 ) % 360.0 ));
74+ vh = nb ;
75+ return ;
76+ }
77+ }
78+ if (val .getRawType () == CssTypes .CSS_NUMBER ) {
79+ float p = ((CssNumber ) val ).getValue ();
80+ if (p > 360. ) {
81+ ac .getFrame ().addWarning ("out-of-range" , Util .displayFloat (p ));
82+ CssNumber nb = new CssNumber ();
83+ nb .setFloatValue ((float ) ((((double ) p % 360.0 ) + 360.0 ) % 360.0 ));
84+ vh = nb ;
85+ return ;
86+ }
87+ }
88+ } else if (val .getType () == CssTypes .CSS_ANGLE ) {
89+ // since css-color-4
90+ CssCheckableValue v = val .getCheckableValue ();
91+ if (!v .isPositive ()) {
92+ ac .getFrame ().addWarning ("out-of-range" , val .toString ());
93+ }
94+ if (val .getRawType () == CssTypes .CSS_ANGLE ) {
95+ CssAngle a = (CssAngle ) val ;
96+ float p = a .getValue ();
97+ if (p > a .deg360 .divide (a .factor , 2 , BigDecimal .ROUND_HALF_DOWN ).floatValue ()) {
98+ ac .getFrame ().addWarning ("out-of-range" , Util .displayFloat (p ));
99+ }
100+ // if a proper angle we normalize it after checking everything.
101+ a .normalizeValue ();
102+ }
103+ }
104+ }
105+ vh = val ;
44106 }
45107
46- public void setLightness (float light ) {
47- this .fl = light ;
108+ public final void setSaturation (ApplContext ac , CssValue val )
109+ throws InvalidParamException {
110+ vs = filterValue (ac , val );
48111 }
49112
50- public void setLightness (CssNumber light ) {
51- setLightness (light .getValue ());
113+ public final void setLightness (ApplContext ac , CssValue val )
114+ throws InvalidParamException {
115+ vl = filterValue (ac , val );
52116 }
53117
54118 /**
@@ -57,9 +121,9 @@ public void setLightness(CssNumber light) {
57121 public String toString () {
58122 if (output == null ) {
59123 StringBuilder sb = new StringBuilder ("hsl(" );
60- sb .append (Util . displayFloat ( fh ) ).append (", " );
61- sb .append (Util . displayFloat ( fs )) .append ("% , " );
62- sb .append (Util . displayFloat ( fl )) .append ("% )" );
124+ sb .append (vh ).append (", " );
125+ sb .append (vs ) .append (", " );
126+ sb .append (vl ) .append (")" );
63127 output = sb .toString ();
64128 }
65129 return output ;
0 commit comments