wxOS2 build fixes after wxColourBase introduction.
[wxWidgets.git] / src / common / colourcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/colourcmn.cpp
3 // Purpose: wxColourBase implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 20/4/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/colour.h"
21
22 #include "wx/gdicmn.h"
23 #include "wx/log.h"
24
25
26
27 // ============================================================================
28 // wxString <-> wxColour conversions
29 // ============================================================================
30
31 bool wxColourBase::FromString(const wxChar *str)
32 {
33 if ( str == NULL || str[0] == wxT('\0'))
34 return false; // invalid or empty string
35
36 if ( wxStrncmp(str, wxT("RGB"), 3) == 0 ||
37 wxStrncmp(str, wxT("rgb"), 3) == 0 )
38 {
39 // RGB specification CSS-like
40 int red, green, blue;
41 if (wxSscanf(&str[3], wxT("(%d, %d, %d)"), &red, &green, &blue) != 3)
42 return false;
43
44 Set((unsigned char)red, (unsigned char)green, (unsigned char)blue);
45 }
46 else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
47 {
48 // hexadecimal prefixed with # (HTML syntax)
49 unsigned long tmp;
50 if (wxSscanf(&str[1], wxT("%lX"), &tmp) != 1)
51 return false;
52
53 Set(tmp); // set from packed long
54 }
55 else if (wxTheColourDatabase) // a colour name ?
56 {
57 // we can't do
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);
62 Set((unsigned char)clr.Red(), (unsigned char)clr.Green(), (unsigned char)clr.Blue());
63 }
64
65 if (Ok())
66 return true;
67
68 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str);
69 return false;
70 }
71
72 wxString wxColourBase::GetAsString(long flags) const
73 {
74 wxString colName;
75
76 if (flags & wxC2S_NAME)
77 colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower();
78
79 if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) )
80 {
81 // no name for this colour; return it in CSS syntax
82 colName.Printf(wxT("rgb(%d, %d, %d)"),
83 Red(), Green(), Blue());
84 }
85 else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) )
86 {
87 // no name for this colour; return it in HTML syntax
88 colName.Printf(wxT("#%02X%02X%02X"),
89 Red(), Green(), Blue());
90 }
91
92 // this function always returns a non-empty string
93 wxASSERT_MSG(!colName.empty(),
94 wxT("Invalid wxColour -> wxString conversion flags"));
95
96 return colName;
97 }
98
99 wxColour wxColourBase::CreateByName(const wxString& name)
100 {
101 return wxColour(name);
102 }