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