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