]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/colour.cpp
cleanup
[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
e9576ca5
SC
41wxColour::~wxColour ()
42{
43}
44
276ee533
SC
45wxColour& wxColour::operator=(const RGBColor& col)
46{
47 InitRGBColor(col);
48 return *this;
49}
50
51wxColour& wxColour::operator=(CGColorRef col)
52{
53 InitCGColorRef(col);
54 return *this;
55}
56
57bool wxColour::IsOk() const
58{
59 return m_cgColour.get() != NULL;
60}
61
62void wxColour::InitRGBA (ChannelType r, ChannelType g, ChannelType b, ChannelType a)
e9576ca5
SC
63{
64 m_red = r;
65 m_green = g;
66 m_blue = b;
fa8bc37b 67 m_alpha = a ;
519cb848 68
276ee533 69 CGColorRef col = 0 ;
50b8a336 70#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
276ee533
SC
71 if ( CGColorCreateGenericRGB )
72 col = CGColorCreateGenericRGB( r / 255.0, g / 255.0, b / 255.0, a / 255.0 );
73 else
74#endif
75 {
76 CGFloat components[4] = { r / 255.0, g / 255.0, b / 255.0, a / 255.0 } ;
77 col = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
78 }
79 m_cgColour.reset( col );
e9576ca5 80}
76a5e5d2 81
276ee533 82void wxColour::InitRGBColor( const RGBColor& col )
aad6765c 83{
276ee533
SC
84 m_red = col.red >> 8;
85 m_blue = col.blue >> 8;
86 m_green = col.green >> 8;
87 m_alpha = wxALPHA_OPAQUE;
88 CGColorRef cfcol;
50b8a336 89#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
276ee533
SC
90 if ( CGColorCreateGenericRGB )
91 cfcol = CGColorCreateGenericRGB( col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 );
92 else
93#endif
94 {
95 CGFloat components[4] = { col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 } ;
96 cfcol = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
97 }
98 m_cgColour.reset( cfcol );
d84afea9 99}
f84a986c 100
276ee533 101void wxColour::InitCGColorRef( CGColorRef col )
055de350 102{
276ee533
SC
103 m_cgColour.reset( col );
104 size_t noComp = CGColorGetNumberOfComponents( col );
105 if ( noComp >=3 && noComp <= 4 )
106 {
107 // TODO verify whether we really are on a RGB color space
108 const CGFloat *components = CGColorGetComponents( col );
109 m_red = (int)(components[0]*255+0.5);
110 m_green = (int)(components[1]*255+0.5);
111 m_blue = (int)(components[2]*255+0.5);
112 if ( noComp == 4 )
113 m_alpha = (int)(components[3]*255+0.5);
114 else
115 m_alpha = wxALPHA_OPAQUE;
116 }
117 else
118 {
119 m_alpha = wxALPHA_OPAQUE;
120 m_red = m_green = m_blue = 0;
121 }
055de350
VZ
122}
123
276ee533 124bool wxColour::operator == (const wxColour& colour) const
f84a986c 125{
276ee533
SC
126 return ( (IsOk() == colour.IsOk()) && (!IsOk() ||
127 CGColorEqualToColor( m_cgColour, colour.m_cgColour ) ) );
f84a986c
SC
128}
129
276ee533 130