]>
Commit | Line | Data |
---|---|---|
40989e46 WS |
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 | ||
6b5d2431 WS |
22 | #include "wx/gdicmn.h" |
23 | #include "wx/log.h" | |
24 | ||
40989e46 WS |
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); | |
81853b98 WS |
62 | if (clr.Ok()) |
63 | Set((unsigned char)clr.Red(), | |
64 | (unsigned char)clr.Green(), | |
65 | (unsigned char)clr.Blue()); | |
40989e46 WS |
66 | } |
67 | ||
68 | if (Ok()) | |
69 | return true; | |
70 | ||
71 | wxLogDebug(wxT("wxColour::Set - couldn't set to colour string '%s'"), str); | |
72 | return false; | |
73 | } | |
74 | ||
75 | wxString wxColourBase::GetAsString(long flags) const | |
76 | { | |
77 | wxString colName; | |
78 | ||
79 | if (flags & wxC2S_NAME) | |
80 | colName = wxTheColourDatabase->FindName((const wxColour &)(*this)).MakeLower(); | |
81 | ||
82 | if ( colName.empty() && (flags & wxC2S_CSS_SYNTAX) ) | |
83 | { | |
84 | // no name for this colour; return it in CSS syntax | |
85 | colName.Printf(wxT("rgb(%d, %d, %d)"), | |
86 | Red(), Green(), Blue()); | |
87 | } | |
88 | else if ( colName.empty() && (flags & wxC2S_HTML_SYNTAX) ) | |
89 | { | |
90 | // no name for this colour; return it in HTML syntax | |
91 | colName.Printf(wxT("#%02X%02X%02X"), | |
92 | Red(), Green(), Blue()); | |
93 | } | |
94 | ||
95 | // this function always returns a non-empty string | |
96 | wxASSERT_MSG(!colName.empty(), | |
97 | wxT("Invalid wxColour -> wxString conversion flags")); | |
98 | ||
99 | return colName; | |
100 | } | |
101 | ||
102 | wxColour wxColourBase::CreateByName(const wxString& name) | |
103 | { | |
104 | return wxColour(name); | |
105 | } |