[ 1473731 ] 'wxColourBase and wxString <-> wxColour implementation' with minor modifi...
[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
23
24 // ============================================================================
25 // wxString <-> wxColour conversions
26 // ============================================================================
27
28 bool wxColourBase::FromString(const wxChar *str)
29 {
30 if ( str == NULL || str[0] == wxT('\0'))
31 return false; // invalid or empty string
32
33 if ( wxStrncmp(str, wxT("RGB"), 3) == 0 ||
34 wxStrncmp(str, wxT("rgb"), 3) == 0 )
35 {
36 // RGB specification CSS-like
37 int red, green, blue;
38 if (wxSscanf(&str[3], wxT("(%d, %d, %d)"), &red, &green, &blue) != 3)
39 return false;
40
41 Set((unsigned char)red, (unsigned char)green, (unsigned char)blue);
42 }
43 else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
44 {
45 // hexadecimal prefixed with # (HTML syntax)
46 unsigned long tmp;
47 if (wxSscanf(&str[1], wxT("%lX"), &tmp) != 1)
48 return false;
49
50 Set(tmp); // set from packed long
51 }
52 else if (wxTheColourDatabase) // a colour name ?
53 {
54 // we can't do
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());
60 }
61
62 if (Ok())
63 return true;
64
65 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str);
66 return false;
67 }
68
69 wxString wxColourBase::GetAsString(long flags) const
70 {
71 wxString colName;
72
73 if (flags & wxC2S_NAME)
74 colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower();
75
76 if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) )
77 {
78 // no name for this colour; return it in CSS syntax
79 colName.Printf(wxT("rgb(%d, %d, %d)"),
80 Red(), Green(), Blue());
81 }
82 else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) )
83 {
84 // no name for this colour; return it in HTML syntax
85 colName.Printf(wxT("#%02X%02X%02X"),
86 Red(), Green(), Blue());
87 }
88
89 // this function always returns a non-empty string
90 wxASSERT_MSG(!colName.empty(),
91 wxT("Invalid wxColour -> wxString conversion flags"));
92
93 return colName;
94 }
95
96 wxColour wxColourBase::CreateByName(const wxString& name)
97 {
98 return wxColour(name);
99 }