]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/core/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/colour.cpp
3 // Purpose: wxColour class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/colour.h"
17 #include "wx/gdicmn.h"
20 #include "wx/osx/private.h"
22 #if wxOSX_USE_COCOA_OR_CARBON
23 wxColour::wxColour(const RGBColor
& col
)
29 wxColour::wxColour(CGColorRef col
)
34 #if wxOSX_USE_COCOA_OR_CARBON
35 void wxColour::GetRGBColor( RGBColor
*col
) const
37 col
->red
= (m_red
<< 8) + m_red
;
38 col
->blue
= (m_blue
<< 8) + m_blue
;
39 col
->green
= (m_green
<< 8) + m_green
;
42 wxColour
& wxColour::operator=(const RGBColor
& col
)
49 wxColour
& wxColour::operator=(CGColorRef col
)
55 wxColour
& wxColour::operator=(const wxColour
& col
)
58 m_green
= col
.m_green
;
60 m_alpha
= col
.m_alpha
;
61 m_cgColour
= col
.m_cgColour
;
65 void wxColour::InitRGBA (ChannelType r
, ChannelType g
, ChannelType b
, ChannelType a
)
73 #if wxOSX_USE_COCOA_OR_CARBON && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
74 if ( CGColorCreateGenericRGB
!= NULL
)
75 col
= CGColorCreateGenericRGB( (CGFloat
)(r
/ 255.0), (CGFloat
) (g
/ 255.0), (CGFloat
) (b
/ 255.0), (CGFloat
) (a
/ 255.0) );
79 CGFloat components
[4] = { (CGFloat
)(r
/ 255.0), (CGFloat
) (g
/ 255.0), (CGFloat
) (b
/ 255.0), (CGFloat
) (a
/ 255.0) } ;
80 col
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
82 wxASSERT_MSG(col
!= NULL
, "Invalid CoreGraphics Color");
83 m_cgColour
.reset( col
);
86 #if wxOSX_USE_COCOA_OR_CARBON
87 void wxColour::InitRGBColor( const RGBColor
& col
)
90 m_blue
= col
.blue
>> 8;
91 m_green
= col
.green
>> 8;
92 m_alpha
= wxALPHA_OPAQUE
;
94 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
95 if ( CGColorCreateGenericRGB
!= NULL
)
96 cfcol
= CGColorCreateGenericRGB((CGFloat
)(col
.red
/ 65535.0), (CGFloat
)(col
.green
/ 65535.0),
97 (CGFloat
)(col
.blue
/ 65535.0), (CGFloat
) 1.0 );
101 CGFloat components
[4] = { (CGFloat
)(col
.red
/ 65535.0), (CGFloat
)(col
.green
/ 65535.0),
102 (CGFloat
)(col
.blue
/ 65535.0), (CGFloat
) 1.0 } ;
103 cfcol
= CGColorCreate( wxMacGetGenericRGBColorSpace() , components
) ;
105 wxASSERT_MSG(cfcol
!= NULL
, "Invalid CoreGraphics Color");
106 m_cgColour
.reset( cfcol
);
110 void wxColour::InitCGColorRef( CGColorRef col
)
112 wxASSERT_MSG(col
!= NULL
, "Invalid CoreGraphics Color");
113 m_cgColour
.reset( col
);
114 size_t noComp
= CGColorGetNumberOfComponents( col
);
116 const CGFloat
*components
= NULL
;
117 if ( noComp
>= 1 && noComp
<= 4 )
119 // TODO verify whether we really are on a RGB color space
120 m_alpha
= wxALPHA_OPAQUE
;
121 components
= CGColorGetComponents( col
);
123 InitFromComponents(components
, noComp
);
126 void wxColour::InitFromComponents(const CGFloat
* components
, size_t numComponents
)
128 if ( numComponents
< 1 || !components
)
130 m_alpha
= wxALPHA_OPAQUE
;
131 m_red
= m_green
= m_blue
= 0;
135 if ( numComponents
>= 3 )
137 m_red
= (int)(components
[0]*255+0.5);
138 m_green
= (int)(components
[1]*255+0.5);
139 m_blue
= (int)(components
[2]*255+0.5);
140 if ( numComponents
== 4 )
141 m_alpha
= (int)(components
[3]*255+0.5);
145 m_red
= (int)(components
[0]*255+0.5);
146 m_green
= (int)(components
[0]*255+0.5);
147 m_blue
= (int)(components
[0]*255+0.5);
151 bool wxColour::operator == (const wxColour
& colour
) const
153 return ( (IsOk() == colour
.IsOk()) && (!IsOk() ||
154 CGColorEqualToColor( m_cgColour
, colour
.m_cgColour
) ) );