X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6b5d2431fc188c03bd43ef69b5ad3197646c9289..472137baebae6c011ad7d70285de454b9ce7796d:/src/common/colourcmn.cpp diff --git a/src/common/colourcmn.cpp b/src/common/colourcmn.cpp index 7cc62a7a7f..99243dd383 100644 --- a/src/common/colourcmn.cpp +++ b/src/common/colourcmn.cpp @@ -19,38 +19,65 @@ #include "wx/colour.h" -#include "wx/gdicmn.h" -#include "wx/log.h" - +#ifndef WX_PRECOMP + #include "wx/log.h" + #include "wx/utils.h" + #include "wx/gdicmn.h" + #include "wx/wxcrtvararg.h" +#endif +#if wxUSE_VARIANT +IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLEXPORT) +#endif // ============================================================================ // wxString <-> wxColour conversions // ============================================================================ -bool wxColourBase::FromString(const wxChar *str) +bool wxColourBase::FromString(const wxString& str) { - if ( str == NULL || str[0] == wxT('\0')) + if ( str.empty() ) return false; // invalid or empty string - if ( wxStrncmp(str, wxT("RGB"), 3) == 0 || - wxStrncmp(str, wxT("rgb"), 3) == 0 ) + if ( wxStrnicmp(str, wxT("RGB"), 3) == 0 ) { - // 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)); } else if ( str[0] == wxT('#') && wxStrlen(str) == 7 ) { // hexadecimal prefixed with # (HTML syntax) unsigned long tmp; - if (wxSscanf(&str[1], wxT("%lX"), &tmp) != 1) + if (wxSscanf(str.wx_str() + 1, wxT("%lx"), &tmp) != 1) return false; - Set(tmp); // set from packed long + Set((unsigned char)(tmp >> 16), + (unsigned char)(tmp >> 8), + (unsigned char)tmp); } else if (wxTheColourDatabase) // a colour name ? { @@ -59,7 +86,10 @@ bool wxColourBase::FromString(const wxChar *str) // because this place can be called from constructor // and 'this' could not be available yet wxColour clr = wxTheColourDatabase->Find(str); - Set((unsigned char)clr.Red(), (unsigned char)clr.Green(), (unsigned char)clr.Blue()); + if (clr.Ok()) + Set((unsigned char)clr.Red(), + (unsigned char)clr.Green(), + (unsigned char)clr.Blue()); } if (Ok()) @@ -73,30 +103,85 @@ wxString wxColourBase::GetAsString(long flags) const { wxString colName; - if (flags & wxC2S_NAME) - colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower(); + const bool isOpaque = Alpha() == wxALPHA_OPAQUE; - if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) ) + // we can't use the name format if the colour is not opaque as the alpha + // information would be lost + if ( (flags & wxC2S_NAME) && isOpaque ) { - // no name for this colour; return it in CSS syntax - colName.Printf(wxT("rgb(%d, %d, %d)"), - Red(), Green(), Blue()); + colName = wxTheColourDatabase->FindName( + wx_static_cast(const wxColour &, *this)).MakeLower(); } - else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) ) + + if ( colName.empty() ) { - // no name for this colour; return it in HTML syntax - colName.Printf(wxT("#%02X%02X%02X"), - Red(), Green(), Blue()); + const int red = Red(), + blue = Blue(), + green = Green(); + + if ( flags & wxC2S_CSS_SYNTAX ) + { + // no name for this colour; return it in CSS syntax + if ( isOpaque ) + { + colName.Printf(wxT("rgb(%d, %d, %d)"), red, green, blue); + } + else // use rgba() form + { + // TODO: use locale-independent function + colName.Printf(wxT("rgba(%d, %d, %d, %.3f)"), + red, green, blue, Alpha() / 255.); + } + } + else if ( flags & wxC2S_HTML_SYNTAX ) + { + wxASSERT_MSG( isOpaque, "alpha is lost in HTML syntax" ); + + // no name for this colour; return it in HTML syntax + colName.Printf(wxT("#%02X%02X%02X"), red, green, blue); + } } - // this function always returns a non-empty string + // this function should alway returns a non-empty string wxASSERT_MSG(!colName.empty(), wxT("Invalid wxColour -> wxString conversion flags")); return colName; } +#if WXWIN_COMPATIBILITY_2_6 + +// static wxColour wxColourBase::CreateByName(const wxString& name) { return wxColour(name); } + +void wxColourBase::InitFromName(const wxString& col) +{ + Set(col); +} + +#endif // WXWIN_COMPATIBILITY_2_6 + +// wxColour <-> wxString utilities, used by wxConfig +wxString wxToString(const wxColourBase& col) +{ + return col.IsOk() ? col.GetAsString(wxC2S_CSS_SYNTAX) + : wxString(); +} + +bool wxFromString(const wxString& str, wxColourBase *col) +{ + wxCHECK_MSG( col, false, _T("NULL output parameter") ); + + if ( str.empty() ) + { + *col = wxNullColour; + return true; + } + + return col->Set(str); +} + +