]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/colour.cpp
Fix crash in wxColour ctor from NSColor in wxOSX/Cocoa.
[wxWidgets.git] / src / osx / core / colour.cpp
CommitLineData
c40627e9 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/colour.cpp
c40627e9
SC
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/osx/private.h"
21
22IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject)
23
e630b0f0 24#if wxOSX_USE_COCOA_OR_CARBON
c40627e9
SC
25wxColour::wxColour(const RGBColor& col)
26{
27 InitRGBColor(col);
28}
29#endif
30
31wxColour::wxColour(CGColorRef col)
32{
33 InitCGColorRef(col);
34}
35
e630b0f0 36#if wxOSX_USE_COCOA_OR_CARBON
c40627e9
SC
37void wxColour::GetRGBColor( RGBColor *col ) const
38{
39 col->red = (m_red << 8) + m_red;
40 col->blue = (m_blue << 8) + m_blue;
41 col->green = (m_green << 8) + m_green;
42}
43
44wxColour& wxColour::operator=(const RGBColor& col)
45{
46 InitRGBColor(col);
47 return *this;
48}
49#endif
50
51wxColour& wxColour::operator=(CGColorRef col)
52{
53 InitCGColorRef(col);
54 return *this;
55}
56
57wxColour& wxColour::operator=(const wxColour& col)
58{
59 m_red = col.m_red;
60 m_green = col.m_green;
61 m_blue = col.m_blue;
62 m_alpha = col.m_alpha;
63 m_cgColour = col.m_cgColour;
64 return *this;
65}
66
67void wxColour::InitRGBA (ChannelType r, ChannelType g, ChannelType b, ChannelType a)
68{
69 m_red = r;
70 m_green = g;
71 m_blue = b;
72 m_alpha = a ;
73
74 CGColorRef col = 0 ;
f7d32bb7 75#if wxOSX_USE_COCOA_OR_CARBON && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
728447a0 76 if ( CGColorCreateGenericRGB != NULL )
c40627e9
SC
77 col = CGColorCreateGenericRGB( (CGFloat)(r / 255.0), (CGFloat) (g / 255.0), (CGFloat) (b / 255.0), (CGFloat) (a / 255.0) );
78 else
79#endif
80 {
03647350 81 CGFloat components[4] = { (CGFloat)(r / 255.0), (CGFloat) (g / 255.0), (CGFloat) (b / 255.0), (CGFloat) (a / 255.0) } ;
c40627e9
SC
82 col = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
83 }
84 m_cgColour.reset( col );
85}
86
e630b0f0 87#if wxOSX_USE_COCOA_OR_CARBON
c40627e9
SC
88void wxColour::InitRGBColor( const RGBColor& col )
89{
90 m_red = col.red >> 8;
91 m_blue = col.blue >> 8;
92 m_green = col.green >> 8;
93 m_alpha = wxALPHA_OPAQUE;
94 CGColorRef cfcol;
95#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
728447a0 96 if ( CGColorCreateGenericRGB != NULL )
03647350 97 cfcol = CGColorCreateGenericRGB((CGFloat)(col.red / 65535.0), (CGFloat)(col.green / 65535.0),
c40627e9
SC
98 (CGFloat)(col.blue / 65535.0), (CGFloat) 1.0 );
99 else
100#endif
101 {
03647350
VZ
102 CGFloat components[4] = { (CGFloat)(col.red / 65535.0), (CGFloat)(col.green / 65535.0),
103 (CGFloat)(col.blue / 65535.0), (CGFloat) 1.0 } ;
c40627e9
SC
104 cfcol = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
105 }
106 m_cgColour.reset( cfcol );
107}
108#endif
109
110void wxColour::InitCGColorRef( CGColorRef col )
111{
112 m_cgColour.reset( col );
113 size_t noComp = CGColorGetNumberOfComponents( col );
ce00f59b 114
b478f242 115 const CGFloat *components = NULL;
c40627e9
SC
116 if ( noComp >= 1 && noComp <= 4 )
117 {
118 // TODO verify whether we really are on a RGB color space
119 m_alpha = wxALPHA_OPAQUE;
b478f242 120 components = CGColorGetComponents( col );
c40627e9 121 }
b478f242
KO
122 InitFromComponents(components, noComp);
123}
124
125void wxColour::InitFromComponents(const CGFloat* components, size_t numComponents )
126{
127 if ( numComponents < 1 || !components )
c40627e9
SC
128 {
129 m_alpha = wxALPHA_OPAQUE;
130 m_red = m_green = m_blue = 0;
b478f242
KO
131 return;
132 }
ce00f59b 133
b478f242
KO
134 if ( numComponents >= 3 )
135 {
136 m_red = (int)(components[0]*255+0.5);
137 m_green = (int)(components[1]*255+0.5);
138 m_blue = (int)(components[2]*255+0.5);
139 if ( numComponents == 4 )
140 m_alpha = (int)(components[3]*255+0.5);
141 }
142 else
143 {
144 m_red = (int)(components[0]*255+0.5);
145 m_green = (int)(components[0]*255+0.5);
146 m_blue = (int)(components[0]*255+0.5);
c40627e9
SC
147 }
148}
149
150bool wxColour::operator == (const wxColour& colour) const
151{
152 return ( (IsOk() == colour.IsOk()) && (!IsOk() ||
153 CGColorEqualToColor( m_cgColour, colour.m_cgColour ) ) );
154}
155
156