- // RGB specification CSS-like
- int red, green, blue;
- if (wxSscanf(&str[3], wxT("(%d, %d, %d)"), &red, &green, &blue) != 3)
- return false;
-
- Set((unsigned char)red, (unsigned char)green, (unsigned char)blue);
+ // CSS-like RGB specification
+ // according to http://www.w3.org/TR/css3-color/#colorunits
+ // values outside 0-255 range are allowed but should be clipped
+ int red, green, blue,
+ alpha = wxALPHA_OPAQUE;
+ if ( str.length() > 3 && (str[3] == wxT('a') || str[3] == wxT('A')) )
+ {
+ float a;
+ // TODO: use locale-independent function
+ if ( wxSscanf(str.wx_str() + 4, wxT("( %d , %d , %d , %f )"),
+ &red, &green, &blue, &a) != 4 )
+ return false;
+
+ alpha = wxRound(a * 255);
+ }
+ else // no 'a' following "rgb"
+ {
+ if ( wxSscanf(str.wx_str() + 3, wxT("( %d , %d , %d )"),
+ &red, &green, &blue) != 3 )
+ return false;
+ }
+
+ Set((unsigned char)wxClip(red, 0, 255),
+ (unsigned char)wxClip(green, 0, 255),
+ (unsigned char)wxClip(blue, 0, 255),
+ (unsigned char)wxClip(alpha, 0, 255));