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
18- public class RGBA {
20+ public class RGBA extends RGB {
1921 static final String functionname = "rgba" ;
2022
23+ private String output = null ;
2124 String fname ;
22- String output = null ;
23- int r , g , b ;
24- float fr , fg , fb , a ;
2525
26- boolean percent = false ;
27-
28- /**
29- * @return Returns the percent.
30- */
31- public boolean isPercent () {
32- return percent ;
33- }
34-
35- /**
36- * @param percent The percent to set.
37- */
38- public void setPercent (boolean percent ) {
39- this .percent = percent ;
40- }
41-
42- public final void setRed (int r ) {
43- this .r = r ;
44- this .fr = r ;
45- }
46-
47- public final void setRed (float fr ) {
48- this .fr = fr ;
49- }
50-
51- public final void setGreen (int g ) {
52- this .g = g ;
53- this .fg = g ;
54- }
55-
56- public final void setGreen (float fg ) {
57- this .fg = fg ;
58- }
59-
60- public final void setBlue (int b ) {
61- this .b = b ;
62- this .fb = b ;
63- }
64-
65- public final void setBlue (float fb ) {
66- this .fb = fb ;
26+ CssValue va ;
27+
28+ public final void setAlpha (ApplContext ac , CssValue val )
29+ throws InvalidParamException {
30+ CssValue nv = val ;
31+ output = null ;
32+ if (val .getRawType () == CssTypes .CSS_CALC ) {
33+ // TODO add warning about uncheckability
34+ // might need to extend...
35+ } else {
36+ if (val .getType () == CssTypes .CSS_NUMBER ) {
37+ CssCheckableValue v = val .getCheckableValue ();
38+ if (!v .warnPositiveness (ac , "RGBA" )) {
39+ CssNumber nb = new CssNumber ();
40+ nb .setIntValue (0 );
41+ nv = nb ;
42+ }
43+ if (val .getRawType () == CssTypes .CSS_NUMBER ) {
44+ float p = ((CssNumber ) val ).getValue ();
45+ if (p > 255. ) {
46+ ac .getFrame ().addWarning ("out-of-range" , Util .displayFloat (p ));
47+ CssNumber nb = new CssNumber ();
48+ nb .setIntValue (255 );
49+ nv = nb ;
50+ }
51+ }
52+ }
53+ }
54+ va = nv ;
6755 }
6856
69- public final void setAlpha (float a ) {
70- this .a = a ;
71- }
7257
7358 public boolean equals (RGBA other ) {
7459 if (other != null ) {
75- if (percent ) {
76- if (other .percent ) {
77- return ((fr == other .fr ) &&
78- (fg == other .fg ) &&
79- (fb == other .fb ) &&
80- (a == other .a ));
81- }
82- } else {
83- if (!other .percent ) {
84- return ((r == other .r ) &&
85- (g == other .g ) &&
86- (b == other .b ) &&
87- (a == other .a ));
88- }
89- }
60+ return super .equals (other ) && va .equals (other .va );
9061 }
9162 return false ;
9263 }
@@ -115,26 +86,11 @@ public RGBA(String fname) {
11586 * @param a the alpha channel
11687 */
11788 public RGBA (int r , int g , int b , float a ) {
118- this .r = r ;
119- this .g = g ;
120- this .b = b ;
121- this .a = a ;
122- this .percent = false ;
123- }
124-
125- /**
126- * @param r the red channel
127- * @param g the green channel
128- * @param b the blue channel
129- * @param a the alpha channel
130- * Create a new RGBA with default values
131- */
132- public RGBA (float r , float g , float b , float a ) {
133- this .fr = r ;
134- this .fg = g ;
135- this .fb = b ;
136- this .a = a ;
137- this .percent = true ;
89+ super (r , g , b );
90+ CssNumber n = new CssNumber ();
91+ n .setFloatValue (a );
92+ va = n ;
93+ setPercent (false );
13894 }
13995
14096
@@ -146,15 +102,16 @@ public String toString() {
146102 StringBuilder sb = new StringBuilder ();
147103 sb .append (fname ).append ('(' );
148104 if (isPercent ()) {
149- sb .append (Util . displayFloat ( fr ) ).append ("%, " );
150- sb .append (Util . displayFloat ( fg ) ).append ("%, " );
151- sb .append (Util . displayFloat ( fb ) ).append ("%, " );
105+ sb .append (vr ).append ("%, " );
106+ sb .append (vg ).append ("%, " );
107+ sb .append (vb ).append ("%, " );
152108 } else {
153- sb .append (r ).append (", " );
154- sb .append (g ).append (", " );
155- sb .append (b ).append (", " );
109+ sb .append (vr ).append (", " );
110+ sb .append (vg ).append (", " );
111+ sb .append (vb ).append (", " );
112+
156113 }
157- sb .append (Util . displayFloat ( a ) ).append (')' );
114+ sb .append (va ).append (')' );
158115 output = sb .toString ();
159116 }
160117 return output ;
0 commit comments