]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/colour.cpp
remove leading dots from .ini/.conf in AddConfFileExtIfNeeded() as this results in...
[wxWidgets.git] / src / mac / carbon / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/colour.cpp
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/mac/private.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject)
23
24 wxColour::wxColour(const RGBColor& col)
25 {
26 InitRGBColor(col);
27 }
28
29 wxColour::wxColour(CGColorRef col)
30 {
31 InitCGColorRef(col);
32 }
33
34 void wxColour::GetRGBColor( RGBColor *col ) const
35 {
36 col->red = (m_red << 8) + m_red;
37 col->blue = (m_blue << 8) + m_blue;
38 col->green = (m_green << 8) + m_green;
39 }
40
41 wxColour& wxColour::operator=(const RGBColor& col)
42 {
43 InitRGBColor(col);
44 return *this;
45 }
46
47 wxColour& wxColour::operator=(CGColorRef col)
48 {
49 InitCGColorRef(col);
50 return *this;
51 }
52
53 wxColour& 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
63 void wxColour::InitRGBA (ChannelType r, ChannelType g, ChannelType b, ChannelType a)
64 {
65 m_red = r;
66 m_green = g;
67 m_blue = b;
68 m_alpha = a ;
69
70 CGColorRef col = 0 ;
71 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
72 if ( CGColorCreateGenericRGB )
73 col = CGColorCreateGenericRGB( r / 255.0, g / 255.0, b / 255.0, a / 255.0 );
74 else
75 #endif
76 {
77 CGFloat components[4] = { r / 255.0, g / 255.0, b / 255.0, a / 255.0 } ;
78 col = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
79 }
80 m_cgColour.reset( col );
81 }
82
83 void wxColour::InitRGBColor( const RGBColor& col )
84 {
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;
90 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
91 if ( CGColorCreateGenericRGB )
92 cfcol = CGColorCreateGenericRGB( col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 );
93 else
94 #endif
95 {
96 CGFloat components[4] = { col.red / 65535.0, col.green / 65535.0, col.blue / 65535.0, 1.0 } ;
97 cfcol = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
98 }
99 m_cgColour.reset( cfcol );
100 }
101
102 void wxColour::InitCGColorRef( CGColorRef col )
103 {
104 m_cgColour.reset( col );
105 size_t noComp = CGColorGetNumberOfComponents( col );
106 if ( noComp >= 1 && noComp <= 4 )
107 {
108 // TODO verify whether we really are on a RGB color space
109 m_alpha = wxALPHA_OPAQUE;
110 const CGFloat *components = CGColorGetComponents( col );
111 if ( noComp >= 3 )
112 {
113 m_red = (int)(components[0]*255+0.5);
114 m_green = (int)(components[1]*255+0.5);
115 m_blue = (int)(components[2]*255+0.5);
116 if ( noComp == 4 )
117 m_alpha = (int)(components[3]*255+0.5);
118 }
119 else
120 {
121 m_red = (int)(components[0]*255+0.5);
122 m_green = (int)(components[0]*255+0.5);
123 m_blue = (int)(components[0]*255+0.5);
124 }
125 }
126 else
127 {
128 m_alpha = wxALPHA_OPAQUE;
129 m_red = m_green = m_blue = 0;
130 }
131 }
132
133 bool wxColour::operator == (const wxColour& colour) const
134 {
135 return ( (IsOk() == colour.IsOk()) && (!IsOk() ||
136 CGColorEqualToColor( m_cgColour, colour.m_cgColour ) ) );
137 }
138
139