]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/colour.cpp | |
3 | // Purpose: wxColour class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/colour.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/gdicmn.h" | |
18 | #endif | |
19 | ||
20 | #include "wx/mac/private.h" | |
21 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject) | |
23 | ||
24 | wxColour::wxColour(const RGBColor& col) | |
25 | { | |
26 | InitRGBColor(col); | |
27 | } | |
28 | ||
29 | wxColour::wxColour( const wxColour& col ) | |
30 | { | |
31 | m_red = col.m_red; | |
32 | m_green = col.m_green; | |
33 | m_blue = col.m_blue; | |
34 | m_alpha = col.m_alpha; | |
35 | m_cgColour = col.m_cgColour; | |
36 | } | |
37 | ||
38 | wxColour::wxColour(CGColorRef col) | |
39 | { | |
40 | InitCGColorRef(col); | |
41 | } | |
42 | ||
43 | void wxColour::GetRGBColor( RGBColor *col ) const | |
44 | { | |
45 | col->red = (m_red << 8) + m_red; | |
46 | col->blue = (m_blue << 8) + m_blue; | |
47 | col->green = (m_green << 8) + m_green; | |
48 | } | |
49 | ||
50 | wxColour::~wxColour () | |
51 | { | |
52 | } | |
53 | ||
54 | wxColour& wxColour::operator=(const RGBColor& col) | |
55 | { | |
56 | InitRGBColor(col); | |
57 | return *this; | |
58 | } | |
59 | ||
60 | wxColour& wxColour::operator=(CGColorRef col) | |
61 | { | |
62 | InitCGColorRef(col); | |
63 | return *this; | |
64 | } | |
65 | ||
66 | wxColour& wxColour::operator=(const wxColour& col) | |
67 | { | |
68 | m_red = col.m_red; | |
69 | m_green = col.m_green; | |
70 | m_blue = col.m_blue; | |
71 | m_alpha = col.m_alpha; | |
72 | m_cgColour = col.m_cgColour; | |
73 | return *this; | |
74 | } | |
75 | ||
76 | void wxColour::InitRGBA (ChannelType r, ChannelType g, ChannelType b, ChannelType a) | |
77 | { | |
78 | m_red = r; | |
79 | m_green = g; | |
80 | m_blue = b; | |
81 | m_alpha = a ; | |
82 | ||
83 | CGColorRef col = 0 ; | |
84 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
85 | if ( CGColorCreateGenericRGB ) | |
86 | col = CGColorCreateGenericRGB( r / 255.0, g / 255.0, b / 255.0, a / 255.0 ); | |
87 | else | |
88 | #endif | |
89 | { | |
90 | CGFloat components[4] = { r / 255.0, g / 255.0, b / 255.0, a / 255.0 } ; | |
91 | col = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ; | |
92 | } | |
93 | m_cgColour.reset( col ); | |
94 | } | |
95 | ||
96 | void wxColour::InitRGBColor( const RGBColor& col ) | |
97 | { | |
98 | m_red = col.red >> 8; | |
99 | m_blue = col.blue >> 8; | |
100 | m_green = col.green >> 8; | |
101 | m_alpha = wxALPHA_OPAQUE; | |
102 | CGColorRef cfcol; | |
103 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 | |
104 | if ( CGColorCreateGenericRGB ) | |
105 | cfcol = CGColorCreateGenericRGB( col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 ); | |
106 | else | |
107 | #endif | |
108 | { | |
109 | CGFloat components[4] = { col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 } ; | |
110 | cfcol = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ; | |
111 | } | |
112 | m_cgColour.reset( cfcol ); | |
113 | } | |
114 | ||
115 | void wxColour::InitCGColorRef( CGColorRef col ) | |
116 | { | |
117 | m_cgColour.reset( col ); | |
118 | size_t noComp = CGColorGetNumberOfComponents( col ); | |
119 | if ( noComp >=3 && noComp <= 4 ) | |
120 | { | |
121 | // TODO verify whether we really are on a RGB color space | |
122 | const CGFloat *components = CGColorGetComponents( col ); | |
123 | m_red = (int)(components[0]*255+0.5); | |
124 | m_green = (int)(components[1]*255+0.5); | |
125 | m_blue = (int)(components[2]*255+0.5); | |
126 | if ( noComp == 4 ) | |
127 | m_alpha = (int)(components[3]*255+0.5); | |
128 | else | |
129 | m_alpha = wxALPHA_OPAQUE; | |
130 | } | |
131 | else | |
132 | { | |
133 | m_alpha = wxALPHA_OPAQUE; | |
134 | m_red = m_green = m_blue = 0; | |
135 | } | |
136 | } | |
137 | ||
138 | bool wxColour::operator == (const wxColour& colour) const | |
139 | { | |
140 | return ( (IsOk() == colour.IsOk()) && (!IsOk() || | |
141 | CGColorEqualToColor( m_cgColour, colour.m_cgColour ) ) ); | |
142 | } | |
143 | ||
144 |