17
17
import java .util .HashMap ;
18
18
import java .util .Map ;
19
19
20
+ import org .w3c .dom .DOMException ;
21
+
20
22
import io .sf .carte .doc .style .css .CSSComputedProperties ;
21
23
import io .sf .carte .doc .style .css .CSSPrimitiveValue ;
22
24
import io .sf .carte .doc .style .css .CSSTypedValue ;
23
25
import io .sf .carte .doc .style .css .CSSUnit ;
24
26
import io .sf .carte .doc .style .css .CSSValue .CssType ;
27
+ import io .sf .carte .doc .style .css .CSSValue .Type ;
25
28
import io .sf .carte .doc .style .css .RGBAColor ;
26
29
import io .sf .carte .doc .style .css .property .CSSPropertyValueException ;
27
- import io .sf .carte .doc .style .css .property .ColorIdentifiers ;
28
30
29
31
/**
30
32
* AWT helper methods.
@@ -86,61 +88,53 @@ public static Font createFont(CSSComputedProperties computedStyle) {
86
88
* number or an identifier.
87
89
* @return the AWT color object, or null if the color was specified as an
88
90
* unknown identifier.
89
- * @throws CSSPropertyValueException if the color declaration is malformed or a
90
- * color identifier is unknown .
91
+ * @throws CSSPropertyValueException if a color cannot be derived from the CSS
92
+ * value .
91
93
*/
92
94
public static Color getAWTColor (CSSTypedValue cssColor ) throws CSSPropertyValueException {
93
95
Color awtcolor = null ;
94
96
if (cssColor != null ) {
95
97
switch (cssColor .getPrimitiveType ()) {
96
98
case COLOR :
97
- RGBAColor color = cssColor .toRGBColor ();
98
- CSSPrimitiveValue red = color .getRed ();
99
- CSSPrimitiveValue green = color .getGreen ();
100
- CSSPrimitiveValue blue = color .getBlue ();
101
- CSSPrimitiveValue prialpha = color .getAlpha ();
102
- //
103
- if (red .getCssValueType () != CssType .TYPED || green .getCssValueType () != CssType .TYPED
104
- || blue .getCssValueType () != CssType .TYPED || prialpha .getCssValueType () != CssType .TYPED ) {
105
- CSSPropertyValueException ex = new CSSPropertyValueException ("Unknown color." );
99
+ case IDENT :
100
+ RGBAColor color ;
101
+ try {
102
+ color = cssColor .toRGBColor ();
103
+ } catch (RuntimeException e ) {
104
+ CSSPropertyValueException ex = new CSSPropertyValueException (
105
+ "Cannot obtain a RGB color." , e );
106
106
ex .setValueText (cssColor .getCssText ());
107
107
throw ex ;
108
108
}
109
- float alpha = ((CSSTypedValue ) prialpha ).getFloatValue (CSSUnit .CSS_NUMBER );
110
- switch (red .getUnitType ()) {
111
- case CSSUnit .CSS_PERCENTAGE :
112
- awtcolor = new Color (clipColorValue (((CSSTypedValue ) red ).getFloatValue (CSSUnit .CSS_PERCENTAGE ) / 100f ),
113
- clipColorValue (((CSSTypedValue ) green ).getFloatValue (CSSUnit .CSS_PERCENTAGE ) / 100f ),
114
- clipColorValue (((CSSTypedValue ) blue ).getFloatValue (CSSUnit .CSS_PERCENTAGE ) / 100f ), alpha );
115
- break ;
116
- case CSSUnit .CSS_NUMBER :
117
- try {
118
- awtcolor = new Color (clipColorValue ((int ) ((CSSTypedValue ) red ).getFloatValue (CSSUnit .CSS_NUMBER )),
119
- clipColorValue ((int ) ((CSSTypedValue ) green ).getFloatValue (CSSUnit .CSS_NUMBER )),
120
- clipColorValue ((int ) ((CSSTypedValue ) blue ).getFloatValue (CSSUnit .CSS_NUMBER )),
121
- Math .round (alpha * 255f ));
122
- } catch (IllegalArgumentException e ) {
123
- CSSPropertyValueException ex = new CSSPropertyValueException ("Unknown color." , e );
124
- ex .setValueText (cssColor .getCssText ());
125
- throw ex ;
126
- }
109
+
110
+ double [] rgb ;
111
+ try {
112
+ rgb = color .toNumberArray ();
113
+ } catch (RuntimeException e ) {
114
+ CSSPropertyValueException ex = new CSSPropertyValueException (
115
+ "Cannot obtain the color components." , e );
116
+ ex .setValueText (cssColor .getCssText ());
117
+ throw ex ;
127
118
}
128
- break ;
129
- case IDENT :
130
- String sv = cssColor .getStringValue ();
131
- String s = ColorIdentifiers .getInstance ().getColor (sv );
132
- if (s != null ) {
133
- try {
134
- awtcolor = Color .decode (s );
135
- } catch (NumberFormatException e ) {
136
- CSSPropertyValueException ex = new CSSPropertyValueException ("Unknown color" , e );
137
- ex .setValueText (sv );
138
- throw ex ;
139
- }
140
- } else if ("transparent" .equals (sv )) {
141
- return new Color (0 , 0 , 0 , 0 );
142
- } else {
143
- return Color .getColor (sv );
119
+
120
+ CSSPrimitiveValue prialpha = color .getAlpha ();
121
+
122
+ if (prialpha .getCssValueType () != CssType .TYPED ) {
123
+ CSSPropertyValueException ex = new CSSPropertyValueException (
124
+ "Unsupported alpha channel." );
125
+ ex .setValueText (cssColor .getCssText ());
126
+ throw ex ;
127
+ }
128
+
129
+ float alpha = normalizedAlphaComponent ((CSSTypedValue ) prialpha );
130
+
131
+ try {
132
+ awtcolor = new Color ((float ) rgb [0 ], (float ) rgb [1 ], (float ) rgb [2 ], alpha );
133
+ } catch (IllegalArgumentException e ) {
134
+ CSSPropertyValueException ex = new CSSPropertyValueException ("Unknown color." ,
135
+ e );
136
+ ex .setValueText (cssColor .getCssText ());
137
+ throw ex ;
144
138
}
145
139
break ;
146
140
case NUMERIC :
@@ -156,12 +150,26 @@ public static Color getAWTColor(CSSTypedValue cssColor) throws CSSPropertyValueE
156
150
return awtcolor ;
157
151
}
158
152
159
- static int clipColorValue (int color ) {
160
- return Math .max (Math .min (255 , color ), 0 );
161
- }
162
-
163
- static float clipColorValue (float color ) {
164
- return Math .max (Math .min (1f , color ), 0f );
153
+ /**
154
+ * Normalize the alpha component to a [0,1] interval.
155
+ *
156
+ * @param typed the component.
157
+ * @return the normalized component.
158
+ */
159
+ private static float normalizedAlphaComponent (CSSTypedValue typed ) {
160
+ float comp ;
161
+ short unit = typed .getUnitType ();
162
+ if (unit == CSSUnit .CSS_PERCENTAGE ) {
163
+ comp = typed .getFloatValue (CSSUnit .CSS_PERCENTAGE ) * 0.01f ;
164
+ } else if (unit == CSSUnit .CSS_NUMBER ) {
165
+ comp = typed .getFloatValue (CSSUnit .CSS_NUMBER );
166
+ } else if (typed .getPrimitiveType () == Type .IDENT ) {
167
+ comp = 0f ;
168
+ } else {
169
+ throw new DOMException (DOMException .TYPE_MISMATCH_ERR ,
170
+ "Wrong component: " + typed .getCssText ());
171
+ }
172
+ return Math .max (Math .min (1f , comp ), 0f );
165
173
}
166
174
167
175
}
0 commit comments