Remove obsolete VisualAge-related files.
[wxWidgets.git] / src / osx / core / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/colour.cpp
3 // Purpose: wxColour class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/colour.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/gdicmn.h"
17 #endif
18
19 #include "wx/osx/private.h"
20
21 #if wxOSX_USE_COCOA_OR_CARBON
22 wxColour::wxColour(const RGBColor& col)
23 {
24 InitRGBColor(col);
25 }
26 #endif
27
28 wxColour::wxColour(CGColorRef col)
29 {
30 InitCGColorRef(col);
31 }
32
33 #if wxOSX_USE_COCOA_OR_CARBON
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 #endif
47
48 wxColour& wxColour::operator=(CGColorRef col)
49 {
50 InitCGColorRef(col);
51 return *this;
52 }
53
54 wxColour& wxColour::operator=(const wxColour& col)
55 {
56 m_red = col.m_red;
57 m_green = col.m_green;
58 m_blue = col.m_blue;
59 m_alpha = col.m_alpha;
60 m_cgColour = col.m_cgColour;
61 return *this;
62 }
63
64 void wxColour::InitRGBA (ChannelType r, ChannelType g, ChannelType b, ChannelType a)
65 {
66 m_red = r;
67 m_green = g;
68 m_blue = b;
69 m_alpha = a ;
70
71 CGColorRef col = 0 ;
72 #if wxOSX_USE_COCOA_OR_CARBON
73 if ( CGColorCreateGenericRGB != NULL )
74 col = CGColorCreateGenericRGB( (CGFloat)(r / 255.0), (CGFloat) (g / 255.0), (CGFloat) (b / 255.0), (CGFloat) (a / 255.0) );
75 else
76 #endif
77 {
78 CGFloat components[4] = { (CGFloat)(r / 255.0), (CGFloat) (g / 255.0), (CGFloat) (b / 255.0), (CGFloat) (a / 255.0) } ;
79 col = CGColorCreate( wxMacGetGenericRGBColorSpace() , components ) ;
80 }
81 wxASSERT_MSG(col != NULL, "Invalid CoreGraphics Color");
82 m_cgColour.reset( col );
83 }
84
85 #if wxOSX_USE_COCOA_OR_CARBON
86 void wxColour::InitRGBColor( const RGBColor& col )
87 {
88 m_red = col.red >> 8;
89 m_blue = col.blue >> 8;
90 m_green = col.green >> 8;
91 m_alpha = wxALPHA_OPAQUE;
92 CGColorRef cfcol;
93 cfcol = CGColorCreateGenericRGB((CGFloat)(col.red / 65535.0), (CGFloat)(col.green / 65535.0),
94 (CGFloat)(col.blue / 65535.0), (CGFloat) 1.0 );
95 wxASSERT_MSG(cfcol != NULL, "Invalid CoreGraphics Color");
96 m_cgColour.reset( cfcol );
97 }
98 #endif
99
100 void wxColour::InitCGColorRef( CGColorRef col )
101 {
102 wxASSERT_MSG(col != NULL, "Invalid CoreGraphics Color");
103 m_cgColour.reset( col );
104 size_t noComp = CGColorGetNumberOfComponents( col );
105
106 const CGFloat *components = NULL;
107 if ( noComp >= 1 && noComp <= 4 )
108 {
109 // TODO verify whether we really are on a RGB color space
110 m_alpha = wxALPHA_OPAQUE;
111 components = CGColorGetComponents( col );
112 }
113 InitFromComponents(components, noComp);
114 }
115
116 void wxColour::InitFromComponents(const CGFloat* components, size_t numComponents )
117 {
118 if ( numComponents < 1 || !components )
119 {
120 m_alpha = wxALPHA_OPAQUE;
121 m_red = m_green = m_blue = 0;
122 return;
123 }
124
125 if ( numComponents >= 3 )
126 {
127 m_red = (int)(components[0]*255+0.5);
128 m_green = (int)(components[1]*255+0.5);
129 m_blue = (int)(components[2]*255+0.5);
130 if ( numComponents == 4 )
131 m_alpha = (int)(components[3]*255+0.5);
132 }
133 else
134 {
135 m_red = (int)(components[0]*255+0.5);
136 m_green = (int)(components[0]*255+0.5);
137 m_blue = (int)(components[0]*255+0.5);
138 }
139 }
140
141 bool wxColour::operator == (const wxColour& colour) const
142 {
143 return ( (IsOk() == colour.IsOk()) && (!IsOk() ||
144 CGColorEqualToColor( m_cgColour, colour.m_cgColour ) ) );
145 }
146
147