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