]>
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"
26 #include "wx/gdicmn.h"
29 // ============================================================================
30 // wxString <-> wxColour conversions
31 // ============================================================================
33 bool wxColourBase::FromString(const wxChar
*str
)
35 if ( str
== NULL
|| str
[0] == wxT('\0'))
36 return false; // invalid or empty string
38 if ( wxStrncmp(str
, wxT("RGB"), 3) == 0 ||
39 wxStrncmp(str
, wxT("rgb"), 3) == 0 )
41 // RGB specification CSS-like
43 if (wxSscanf(&str
[3], wxT("(%d, %d, %d)"), &red
, &green
, &blue
) != 3)
46 Set((unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
48 else if ( str
[0] == wxT('#') && wxStrlen(str
) == 7 )
50 // hexadecimal prefixed with # (HTML syntax)
52 if (wxSscanf(&str
[1], wxT("%lX"), &tmp
) != 1)
55 Set(tmp
); // set from packed long
57 else if (wxTheColourDatabase
) // a colour name ?
60 // *this = wxTheColourDatabase->Find(str)
61 // because this place can be called from constructor
62 // and 'this' could not be available yet
63 wxColour clr
= wxTheColourDatabase
->Find(str
);
65 Set((unsigned char)clr
.Red(),
66 (unsigned char)clr
.Green(),
67 (unsigned char)clr
.Blue());
73 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str
);
77 wxString
wxColourBase::GetAsString(long flags
) const
81 if (flags
& wxC2S_NAME
)
82 colName
= wxTheColourDatabase
->FindName((const wxColour
&)(*this)).MakeLower();
84 if ( colName
.empty() && (flags
& wxC2S_CSS_SYNTAX
) )
86 // no name for this colour; return it in CSS syntax
87 colName
.Printf(wxT("rgb(%d, %d, %d)"),
88 Red(), Green(), Blue());
90 else if ( colName
.empty() && (flags
& wxC2S_HTML_SYNTAX
) )
92 // no name for this colour; return it in HTML syntax
93 colName
.Printf(wxT("#%02X%02X%02X"),
94 Red(), Green(), Blue());
97 // this function always returns a non-empty string
98 wxASSERT_MSG(!colName
.empty(),
99 wxT("Invalid wxColour -> wxString conversion flags"));
104 #if WXWIN_COMPATIBILITY_2_6
107 wxColour
wxColourBase::CreateByName(const wxString
& name
)
109 return wxColour(name
);
112 void wxColourBase::InitFromName(const wxString
& col
)
117 #endif // WXWIN_COMPATIBILITY_2_6