]>
git.saurik.com Git - wxWidgets.git/blob - src/common/colourcmn.cpp
5770961fb0569b6d215c9b443878c30eb50450b7
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"
24 // ============================================================================
25 // wxString <-> wxColour conversions
26 // ============================================================================
28 bool wxColourBase::FromString(const wxChar
*str
)
30 if ( str
== NULL
|| str
[0] == wxT('\0'))
31 return false; // invalid or empty string
33 if ( wxStrncmp(str
, wxT("RGB"), 3) == 0 ||
34 wxStrncmp(str
, wxT("rgb"), 3) == 0 )
36 // RGB specification CSS-like
38 if (wxSscanf(&str
[3], wxT("(%d, %d, %d)"), &red
, &green
, &blue
) != 3)
41 Set((unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
43 else if ( str
[0] == wxT('#') && wxStrlen(str
) == 7 )
45 // hexadecimal prefixed with # (HTML syntax)
47 if (wxSscanf(&str
[1], wxT("%lX"), &tmp
) != 1)
50 Set(tmp
); // set from packed long
52 else if (wxTheColourDatabase
) // a colour name ?
55 // *this = wxTheColourDatabase->Find(str)
56 // because this place can be called from constructor
57 // and 'this' could not be available yet
58 wxColour clr
= wxTheColourDatabase
->Find(str
);
59 Set((unsigned char)clr
.Red(), (unsigned char)clr
.Green(), (unsigned char)clr
.Blue());
65 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str
);
69 wxString
wxColourBase::GetAsString(long flags
) const
73 if (flags
& wxC2S_NAME
)
74 colName
= wxTheColourDatabase
->FindName((const wxColour
&)(*this)).MakeLower();
76 if ( colName
.empty() && (flags
& wxC2S_CSS_SYNTAX
) )
78 // no name for this colour; return it in CSS syntax
79 colName
.Printf(wxT("rgb(%d, %d, %d)"),
80 Red(), Green(), Blue());
82 else if ( colName
.empty() && (flags
& wxC2S_HTML_SYNTAX
) )
84 // no name for this colour; return it in HTML syntax
85 colName
.Printf(wxT("#%02X%02X%02X"),
86 Red(), Green(), Blue());
89 // this function always returns a non-empty string
90 wxASSERT_MSG(!colName
.empty(),
91 wxT("Invalid wxColour -> wxString conversion flags"));
96 wxColour
wxColourBase::CreateByName(const wxString
& name
)
98 return wxColour(name
);