]>
git.saurik.com Git - wxWidgets.git/blob - src/common/colourcmn.cpp
efd9cad8906106dddc4827b1888c1448a49d0c9b
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"
22 #include "wx/gdicmn.h"
27 // ============================================================================
28 // wxString <-> wxColour conversions
29 // ============================================================================
31 bool wxColourBase::FromString(const wxChar
*str
)
33 if ( str
== NULL
|| str
[0] == wxT('\0'))
34 return false; // invalid or empty string
36 if ( wxStrncmp(str
, wxT("RGB"), 3) == 0 ||
37 wxStrncmp(str
, wxT("rgb"), 3) == 0 )
39 // RGB specification CSS-like
41 if (wxSscanf(&str
[3], wxT("(%d, %d, %d)"), &red
, &green
, &blue
) != 3)
44 Set((unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
46 else if ( str
[0] == wxT('#') && wxStrlen(str
) == 7 )
48 // hexadecimal prefixed with # (HTML syntax)
50 if (wxSscanf(&str
[1], wxT("%lX"), &tmp
) != 1)
53 Set(tmp
); // set from packed long
55 else if (wxTheColourDatabase
) // a colour name ?
58 // *this = wxTheColourDatabase->Find(str)
59 // because this place can be called from constructor
60 // and 'this' could not be available yet
61 wxColour clr
= wxTheColourDatabase
->Find(str
);
63 Set((unsigned char)clr
.Red(),
64 (unsigned char)clr
.Green(),
65 (unsigned char)clr
.Blue());
71 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str
);
75 wxString
wxColourBase::GetAsString(long flags
) const
79 if (flags
& wxC2S_NAME
)
80 colName
= wxTheColourDatabase
->FindName((const wxColour
&)(*this)).MakeLower();
82 if ( colName
.empty() && (flags
& wxC2S_CSS_SYNTAX
) )
84 // no name for this colour; return it in CSS syntax
85 colName
.Printf(wxT("rgb(%d, %d, %d)"),
86 Red(), Green(), Blue());
88 else if ( colName
.empty() && (flags
& wxC2S_HTML_SYNTAX
) )
90 // no name for this colour; return it in HTML syntax
91 colName
.Printf(wxT("#%02X%02X%02X"),
92 Red(), Green(), Blue());
95 // this function always returns a non-empty string
96 wxASSERT_MSG(!colName
.empty(),
97 wxT("Invalid wxColour -> wxString conversion flags"));
102 wxColour
wxColourBase::CreateByName(const wxString
& name
)
104 return wxColour(name
);