]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/colour.mm
Metrowerks Objective-C++ is more strict about types than Apple's.
[wxWidgets.git] / src / cocoa / colour.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colour.mm
3 // Purpose: wxColour class
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/06/17
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #endif //WX_PRECOMP
15
16 #include "wx/gdicmn.h"
17 #include "wx/colour.h"
18
19 #include "wx/cocoa/autorelease.h"
20
21 #import <AppKit/NSColor.h>
22
23 IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject)
24
25 void wxColour::Init()
26 {
27 m_cocoaNSColor = NULL;
28 m_red =
29 m_blue =
30 m_green = 0;
31 }
32
33 wxColour::wxColour (const wxColour& col)
34 : m_cocoaNSColor(col.m_cocoaNSColor)
35 , m_red(col.m_red)
36 , m_green(col.m_green)
37 , m_blue(col.m_blue)
38 {
39 [m_cocoaNSColor retain];
40 }
41
42 wxColour::wxColour( WX_NSColor aColor )
43 : m_cocoaNSColor(nil)
44 {
45 Set(aColor);
46 }
47
48 wxColour& wxColour::operator =(const wxColour& col)
49 {
50 m_cocoaNSColor = col.m_cocoaNSColor;
51 m_red = col.m_red;
52 m_green = col.m_green;
53 m_blue = col.m_blue;
54 [m_cocoaNSColor retain];
55 return *this;
56 }
57
58 void wxColour::InitFromName(const wxString& name)
59 {
60 if ( wxTheColourDatabase )
61 {
62 wxColour col = wxTheColourDatabase->Find(name);
63 if ( col.Ok() )
64 {
65 *this = col;
66 return;
67 }
68 }
69
70 // leave invalid
71 Init();
72 }
73
74 wxColour::~wxColour ()
75 {
76 [m_cocoaNSColor release];
77 }
78
79 void wxColour::Set (unsigned char r, unsigned char g, unsigned char b)
80 {
81 wxAutoNSAutoreleasePool pool;
82 [m_cocoaNSColor release];
83 m_cocoaNSColor = [[NSColor colorWithCalibratedRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0] retain];
84 m_red = r;
85 m_green = g;
86 m_blue = b;
87 }
88
89 void wxColour::Set( WX_NSColor aColor )
90 {
91 [aColor retain];
92 [m_cocoaNSColor release];
93 m_cocoaNSColor = aColor;
94
95 /* Make a temporary color in RGB format and get the values. Note that
96 unless the color was actually RGB to begin with it's likely that
97 these will be fairly bogus. Particulary if the color is a pattern. */
98 NSColor *rgbColor = [m_cocoaNSColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
99 m_red = (wxUint8) ([rgbColor redComponent] * 255.0);
100 m_green = (wxUint8) ([rgbColor greenComponent] * 255.0);
101 m_blue = (wxUint8) ([rgbColor blueComponent] * 255.0);
102 }
103