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