]>
git.saurik.com Git - wxWidgets.git/blob - src/common/colourcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/colourcmn.cpp
3 // Purpose: wxColourBase implementation
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/colour.h"
25 #include "wx/gdicmn.h"
26 #include "wx/wxcrtvararg.h"
30 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxColour
,WXDLLEXPORT
)
33 // ============================================================================
34 // wxString <-> wxColour conversions
35 // ============================================================================
37 bool wxColourBase::FromString(const wxChar
*str
)
39 if ( str
== NULL
|| str
[0] == wxT('\0'))
40 return false; // invalid or empty string
42 if ( wxStrncmp(str
, wxT("RGB"), 3) == 0 ||
43 wxStrncmp(str
, wxT("rgb"), 3) == 0 )
45 // CSS-like RGB specification
46 // according to http://www.w3.org/TR/REC-CSS2/syndata.html#color-units
47 // values outside 0-255 range are allowed but should be clipped
49 if (wxSscanf(&str
[3], wxT("(%d, %d, %d)"), &red
, &green
, &blue
) != 3)
52 Set((unsigned char)wxClip(red
,0,255),
53 (unsigned char)wxClip(green
,0,255),
54 (unsigned char)wxClip(blue
,0,255));
56 else if ( str
[0] == wxT('#') && wxStrlen(str
) == 7 )
58 // hexadecimal prefixed with # (HTML syntax)
60 if (wxSscanf(&str
[1], wxT("%lx"), &tmp
) != 1)
63 Set((unsigned char)(tmp
>> 16),
64 (unsigned char)(tmp
>> 8),
67 else if (wxTheColourDatabase
) // a colour name ?
70 // *this = wxTheColourDatabase->Find(str)
71 // because this place can be called from constructor
72 // and 'this' could not be available yet
73 wxColour clr
= wxTheColourDatabase
->Find(str
);
75 Set((unsigned char)clr
.Red(),
76 (unsigned char)clr
.Green(),
77 (unsigned char)clr
.Blue());
83 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str
);
87 wxString
wxColourBase::GetAsString(long flags
) const
91 if (flags
& wxC2S_NAME
)
92 colName
= wxTheColourDatabase
->FindName((const wxColour
&)(*this)).MakeLower();
94 if ( colName
.empty() && (flags
& wxC2S_CSS_SYNTAX
) )
96 // no name for this colour; return it in CSS syntax
97 colName
.Printf(wxT("rgb(%d, %d, %d)"),
98 Red(), Green(), Blue());
100 else if ( colName
.empty() && (flags
& wxC2S_HTML_SYNTAX
) )
102 // no name for this colour; return it in HTML syntax
103 colName
.Printf(wxT("#%02X%02X%02X"),
104 Red(), Green(), Blue());
107 // this function always returns a non-empty string
108 wxASSERT_MSG(!colName
.empty(),
109 wxT("Invalid wxColour -> wxString conversion flags"));
114 #if WXWIN_COMPATIBILITY_2_6
117 wxColour
wxColourBase::CreateByName(const wxString
& name
)
119 return wxColour(name
);
122 void wxColourBase::InitFromName(const wxString
& col
)
127 #endif // WXWIN_COMPATIBILITY_2_6