CSS colour compatibility after #1473731.
[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 #ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/utils.h"
25 #endif
26
27 #include "wx/gdicmn.h"
28
29
30 // ============================================================================
31 // wxString <-> wxColour conversions
32 // ============================================================================
33
34 bool wxColourBase::FromString(const wxChar *str)
35 {
36 if ( str == NULL || str[0] == wxT('\0'))
37 return false; // invalid or empty string
38
39 if ( wxStrncmp(str, wxT("RGB"), 3) == 0 ||
40 wxStrncmp(str, wxT("rgb"), 3) == 0 )
41 {
42 // CSS-like RGB specification
43 // according to http://www.w3.org/TR/REC-CSS2/syndata.html#color-units
44 // values outside 0-255 range are allowed but should be clipped
45 int red, green, blue;
46 if (wxSscanf(&str[3], wxT("(%d, %d, %d)"), &red, &green, &blue) != 3)
47 return false;
48
49 Set((unsigned char)wxClip(red,0,255),
50 (unsigned char)wxClip(green,0,255),
51 (unsigned char)wxClip(blue,0,255));
52 }
53 else if ( str[0] == wxT('#') && wxStrlen(str) == 7 )
54 {
55 // hexadecimal prefixed with # (HTML syntax)
56 unsigned long tmp;
57 if (wxSscanf(&str[1], wxT("%lX"), &tmp) != 1)
58 return false;
59
60 Set(tmp); // set from packed long
61 }
62 else if (wxTheColourDatabase) // a colour name ?
63 {
64 // we can't do
65 // *this = wxTheColourDatabase->Find(str)
66 // because this place can be called from constructor
67 // and 'this' could not be available yet
68 wxColour clr = wxTheColourDatabase->Find(str);
69 if (clr.Ok())
70 Set((unsigned char)clr.Red(),
71 (unsigned char)clr.Green(),
72 (unsigned char)clr.Blue());
73 }
74
75 if (Ok())
76 return true;
77
78 wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str);
79 return false;
80 }
81
82 wxString wxColourBase::GetAsString(long flags) const
83 {
84 wxString colName;
85
86 if (flags & wxC2S_NAME)
87 colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower();
88
89 if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) )
90 {
91 // no name for this colour; return it in CSS syntax
92 colName.Printf(wxT("rgb(%d, %d, %d)"),
93 Red(), Green(), Blue());
94 }
95 else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) )
96 {
97 // no name for this colour; return it in HTML syntax
98 colName.Printf(wxT("#%02X%02X%02X"),
99 Red(), Green(), Blue());
100 }
101
102 // this function always returns a non-empty string
103 wxASSERT_MSG(!colName.empty(),
104 wxT("Invalid wxColour -> wxString conversion flags"));
105
106 return colName;
107 }
108
109 #if WXWIN_COMPATIBILITY_2_6
110
111 // static
112 wxColour wxColourBase::CreateByName(const wxString& name)
113 {
114 return wxColour(name);
115 }
116
117 void wxColourBase::InitFromName(const wxString& col)
118 {
119 Set(col);
120 }
121
122 #endif // WXWIN_COMPATIBILITY_2_6