~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/src/_colour.i

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/////////////////////////////////////////////////////////////////////////////
 
2
// Name:        _colour.i
 
3
// Purpose:     SWIG interface for wxColour
 
4
//
 
5
// Author:      Robin Dunn
 
6
//
 
7
// Created:     7-July-1997
 
8
// RCS-ID:      $Id: _colour.i 69040 2011-09-10 01:00:23Z RD $
 
9
// Copyright:   (c) 2003 by Total Control Software
 
10
// Licence:     wxWindows license
 
11
/////////////////////////////////////////////////////////////////////////////
 
12
 
 
13
// Not a %module
 
14
 
 
15
 
 
16
%{
 
17
#ifdef __WXMAC__
 
18
#include <wx/osx/private.h>
 
19
#endif
 
20
%}
 
21
 
 
22
//---------------------------------------------------------------------------
 
23
%newgroup;
 
24
 
 
25
 
 
26
enum {
 
27
    wxC2S_NAME,             // return colour name, when possible
 
28
    wxC2S_CSS_SYNTAX,       // return colour in rgb(r,g,b) syntax
 
29
    wxC2S_HTML_SYNTAX,      // return colour in #rrggbb syntax     
 
30
};
 
31
 
 
32
enum {
 
33
    wxALPHA_TRANSPARENT,
 
34
    wxALPHA_OPAQUE
 
35
};
 
36
 
 
37
 
 
38
DocStr(wxColour,
 
39
"A colour is an object representing a combination of Red, Green, and
 
40
Blue (RGB) intensity values, and is used to determine drawing colours,
 
41
window colours, etc.  Valid RGB values are in the range 0 to 255.
 
42
 
 
43
In wxPython there are typemaps that will automatically convert from a
 
44
colour name, from a '#RRGGBB' colour hex value string, or from a 3 or 4
 
45
integer tuple to a wx.Colour object when calling C++ methods that
 
46
expect a wxColour.  This means that the following are all
 
47
equivallent::
 
48
 
 
49
    win.SetBackgroundColour(wxColour(0,0,255))
 
50
    win.SetBackgroundColour('BLUE')
 
51
    win.SetBackgroundColour('#0000FF')
 
52
    win.SetBackgroundColour((0,0,255))
 
53
 
 
54
In addition to the RGB values, the alpha transparency can optionally
 
55
be set.  This is supported by the typemaps as well as the wx.Colour
 
56
constructors and setters.  (The alpha value is ignored in many places
 
57
that take a wx.Colour object, but it is honored in things like wx.GCDC
 
58
or wx.GraphicsContext.)  Adding an alpha value of 0xC0 (192) to the
 
59
above samples looks like this:
 
60
 
 
61
    win.SetBackgroundColour(wxColour(0,0,255,192))
 
62
    win.SetBackgroundColour('BLUE:C0')
 
63
    win.SetBackgroundColour('#0000FFC0')
 
64
    win.SetBackgroundColour((0,0,255,192))
 
65
 
 
66
Additional colour names and their coresponding values can be added
 
67
using `wx.ColourDatabase`. Also see `wx.lib.colourdb` for a large set
 
68
of colour names and values.  Various system colours (as set in the
 
69
user's system preferences or control panel) can be retrieved with
 
70
`wx.SystemSettings.GetColour`.
 
71
", "");
 
72
 
 
73
 
 
74
MustHaveApp( wxColour(const wxString& colorName) );
 
75
 
 
76
class wxColour : public wxObject {
 
77
public:
 
78
    
 
79
    DocCtorStr(
 
80
        wxColour(byte red=0, byte green=0, byte blue=0, byte alpha=wxALPHA_OPAQUE),
 
81
        "Constructs a colour from red, green, blue and alpha values.
 
82
 
 
83
:see: Alternate constructors `wx.NamedColour`, `wx.ColourRGB` and `MacThemeColour`.
 
84
", "");
 
85
    
 
86
    %RenameADocCtor(
 
87
        NamedColour,
 
88
        "NamedColour(String colourName) -> Colour",
 
89
        "Constructs a colour object using a colour name listed in
 
90
``wx.TheColourDatabase``, or any string format supported by the
 
91
wxColour typemaps.", "",
 
92
        // NOTE: We say String in the docstring but use wxColour for
 
93
        // real so the typemap will be applied.
 
94
        wxColour( const wxColour& colourName ));
 
95
 
 
96
    
 
97
    %RenameDocCtor(
 
98
        ColourRGB,
 
99
        "Constructs a colour from a packed RGB value.", "",
 
100
        wxColour( unsigned long colRGB ));
 
101
 
 
102
    %extend {
 
103
        %RenameDocCtor(
 
104
            MacThemeColour,
 
105
            "Creates a color (or pattern) from a Mac theme brush ID.  Raises a
 
106
NotImplemented exception on other platforms.", "",
 
107
            wxColour( int themeBrushID ))
 
108
            {
 
109
#ifdef __WXMAC__
 
110
                return new wxColour(wxMacCreateCGColorFromHITheme(themeBrushID));
 
111
#else
 
112
                wxPyRaiseNotImplemented(); return NULL;
 
113
#endif
 
114
            }
 
115
    }
 
116
    
 
117
    ~wxColour();
 
118
 
 
119
    
 
120
    DocDeclStr(
 
121
        byte , Red(),
 
122
        "Returns the red intensity.", "");
 
123
    
 
124
    DocDeclStr(
 
125
        byte , Green(),
 
126
        "Returns the green intensity.", "");
 
127
    
 
128
    DocDeclStr(
 
129
        byte , Blue(),
 
130
        "Returns the blue intensity.", "");
 
131
    
 
132
    DocDeclStr(
 
133
        byte , Alpha(),
 
134
        "Returns the Alpha value.", "");
 
135
    
 
136
    DocDeclStr(
 
137
        bool , IsOk(),
 
138
        "Returns True if the colour object is valid (the colour has been
 
139
initialised with RGB values).", "");
 
140
    %pythoncode { Ok = IsOk }
 
141
    
 
142
    DocDeclStr(
 
143
        void , Set(byte red, byte green, byte blue, byte alpha=wxALPHA_OPAQUE),
 
144
        "Sets the RGB intensity values.", "");
 
145
 
 
146
    %pythoncode {
 
147
        def SetFromString(self, colourName):
 
148
            """
 
149
            Sets the RGB intensity values using a colour name listed in
 
150
            ``wx.TheColourDatabase``, or any string format supported by
 
151
            the wxColour typemaps.
 
152
            """
 
153
            c = wx.NamedColour(colourName)
 
154
            self.Set(c.red, c.green, c.blue, c.alpha)
 
155
        SetFromName = SetFromString
 
156
    }
 
157
 
 
158
    // TODO: This needs to be updated to include alpha in the html
 
159
    // syntax to match our typemaps
 
160
    DocDeclStr(
 
161
        wxString , GetAsString(long flags = wxC2S_NAME | wxC2S_CSS_SYNTAX) const,
 
162
        "Return the colour as a string.  Acceptable flags are:
 
163
 
 
164
            =================== ==================================
 
165
            wx.C2S_NAME          return colour name, when possible
 
166
            wx.C2S_CSS_SYNTAX    return colour in rgb(r,g,b) syntax
 
167
            wx.C2S_HTML_SYNTAX   return colour in #rrggbb syntax     
 
168
            =================== ==================================", "");
 
169
 
 
170
    
 
171
    DocDeclStr(
 
172
        void , SetRGB(wxUint32 colRGB),
 
173
        "Sets the RGB colour values from a single 32 bit value.
 
174
 
 
175
The argument colRGB should be of the form 0x00BBGGRR and where 0xRR,
 
176
0xGG and 0xBB are the values of the red, green and blue components.", "");
 
177
    
 
178
    DocDeclStr(
 
179
        void , SetRGBA(wxUint32 colRGBA),
 
180
        "Sets the RGBA colour values from a single 32 bit value.
 
181
 
 
182
The argument colRGBA should be of the form 0xAABBGGRR where 0xRR,
 
183
0xGG, 0xBB and 0xAA are the values of the red, green, blue and alpha
 
184
components.", "");
 
185
    
 
186
    DocDeclStr(
 
187
        wxUint32 , GetRGB() const,
 
188
        "", "");
 
189
    
 
190
    DocDeclStr(
 
191
        wxUint32 , GetRGBA() const,
 
192
        "", "");
 
193
    
 
194
 
 
195
    
 
196
    // TODO: deal with these...
 
197
    
 
198
    // static void MakeMono    (byte* r, byte* g, byte* b, bool on);
 
199
    // static void MakeDisabled(byte* r, byte* g, byte* b, byte brightness = 255);
 
200
    // static void MakeGrey    (byte* r, byte* g, byte* b); // integer version
 
201
    // static void MakeGrey    (byte* r, byte* g, byte* b, 
 
202
    //                          double weight_r, double weight_g, double weight_b); // floating point version
 
203
    // static byte AlphaBlend  (byte fg, byte bg, double alpha);
 
204
    // static void ChangeLightness(byte* r, byte* g, byte* b, int ialpha);
 
205
    // wxColour ChangeLightness(int ialpha) const;
 
206
 
 
207
    
 
208
    DocDeclStr(
 
209
        long , GetPixel() const,
 
210
        "Returns a pixel value which is platform-dependent. On Windows, a
 
211
COLORREF is returned. On X, an allocated pixel value is returned.  -1
 
212
is returned if the pixel is invalid (on X, unallocated).", "");
 
213
    
 
214
    
 
215
    %extend {
 
216
        KeepGIL(__eq__);
 
217
        DocStr(__eq__, "Compare colours for equality.", "");
 
218
        bool __eq__(PyObject* other) {
 
219
            wxColour  temp, *obj = &temp;
 
220
            if ( other == Py_None ) return false;
 
221
            if ( ! wxColour_helper(other, &obj) ) {
 
222
                PyErr_Clear();
 
223
                return false;
 
224
            }
 
225
            return self->operator==(*obj);
 
226
        }
 
227
 
 
228
        
 
229
        KeepGIL(__ne__);
 
230
        DocStr(__ne__, "Compare colours for inequality.", "");
 
231
        bool __ne__(PyObject* other) {
 
232
            wxColour  temp, *obj = &temp;
 
233
            if ( other == Py_None ) return true;
 
234
            if ( ! wxColour_helper(other, &obj)) {
 
235
                PyErr_Clear();
 
236
                return true;
 
237
            }
 
238
            return self->operator!=(*obj);
 
239
        }
 
240
    }
 
241
 
 
242
 
 
243
    %extend {
 
244
        KeepGIL(Get);
 
245
        DocAStr(Get,
 
246
                "Get(self, bool includeAlpha=False) -> (r,g,b) or (r,g,b,a)",
 
247
                "Returns the RGB intensity values as a tuple, optionally the alpha value as well.", "");
 
248
        PyObject* Get(bool includeAlpha=false) {
 
249
            PyObject* rv = PyTuple_New(includeAlpha ? 4 : 3);
 
250
            int red = -1;
 
251
            int green = -1;
 
252
            int blue = -1;
 
253
            int alpha = wxALPHA_OPAQUE;
 
254
            if (self->IsOk()) {
 
255
                red =   self->Red();
 
256
                green = self->Green();
 
257
                blue =  self->Blue();
 
258
                alpha = self->Alpha();
 
259
            }
 
260
            PyTuple_SetItem(rv, 0, PyInt_FromLong(red));
 
261
            PyTuple_SetItem(rv, 1, PyInt_FromLong(green));
 
262
            PyTuple_SetItem(rv, 2, PyInt_FromLong(blue));
 
263
            if (includeAlpha)
 
264
                PyTuple_SetItem(rv, 3, PyInt_FromLong(alpha));                
 
265
            return rv;
 
266
        }
 
267
 
 
268
        KeepGIL(GetRGB);
 
269
        DocStr(GetRGB,
 
270
               "Return the colour as a packed RGB value", "");
 
271
        unsigned long GetRGB() {
 
272
            return self->Red() | (self->Green() << 8) | (self->Blue() << 16);
 
273
        }
 
274
    }
 
275
 
 
276
 
 
277
    %pythoncode {
 
278
        asTuple = wx.deprecated(Get, "asTuple is deprecated, use `Get` instead")
 
279
        def __str__(self):                  return str(self.Get(True))
 
280
 
 
281
        %# help() can access the stock colors before they are created,  
 
282
        %# so make sure there is a this attribute before calling any wrapper method.
 
283
        def __repr__(self): 
 
284
            if hasattr(self, 'this'):
 
285
                return 'wx.Colour' + str(self.Get(True))
 
286
            else:
 
287
                return 'wx.Colour()'
 
288
 
 
289
        def __len__(self):                  return len(self.Get())
 
290
        def __getitem__(self, index):       return self.Get()[index]
 
291
        def __nonzero__(self):              return self.IsOk()
 
292
        __safe_for_unpickling__ = True
 
293
        def __reduce__(self):               return (Colour, self.Get(True))
 
294
        }
 
295
 
 
296
    %property(Pixel, GetPixel, doc="See `GetPixel`");
 
297
    %property(RGB, GetRGB, SetRGB, doc="See `GetRGB` and `SetRGB`");
 
298
    %property(red,   Red);
 
299
    %property(green, Green);
 
300
    %property(blue,  Blue);
 
301
    %property(alpha, Alpha);                           
 
302
};
 
303
 
 
304
//---------------------------------------------------------------------------
 
305