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