]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/core/colour.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/osx/core/colour.cpp 
   3 // Purpose:     wxColour class 
   4 // Author:      Stefan Csomor 
   7 // Copyright:   (c) Stefan Csomor 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 #include "wx/wxprec.h" 
  13 #include "wx/colour.h" 
  16     #include "wx/gdicmn.h" 
  19 #include "wx/osx/private.h" 
  21 #if wxOSX_USE_COCOA_OR_CARBON 
  22 wxColour::wxColour(const RGBColor
& col
) 
  28 wxColour::wxColour(CGColorRef col
) 
  33 #if wxOSX_USE_COCOA_OR_CARBON 
  34 void wxColour::GetRGBColor( RGBColor 
*col 
) const 
  36     col
->red 
= (m_red 
<< 8) + m_red
; 
  37     col
->blue 
= (m_blue 
<< 8) + m_blue
; 
  38     col
->green 
= (m_green 
<< 8) + m_green
; 
  41 wxColour
& wxColour::operator=(const RGBColor
& col
) 
  48 wxColour
& wxColour::operator=(CGColorRef col
) 
  54 wxColour
& wxColour::operator=(const wxColour
& col
) 
  57     m_green 
= col
.m_green
; 
  59     m_alpha 
= col
.m_alpha
; 
  60     m_cgColour 
= col
.m_cgColour
; 
  64 void wxColour::InitRGBA (ChannelType r
, ChannelType g
, ChannelType b
, ChannelType a
) 
  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) ); 
  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 
) ; 
  81     wxASSERT_MSG(col 
!= NULL
, "Invalid CoreGraphics Color"); 
  82     m_cgColour
.reset( col 
); 
  85 #if wxOSX_USE_COCOA_OR_CARBON 
  86 void wxColour::InitRGBColor( const RGBColor
& col 
) 
  89     m_blue 
= col
.blue 
>> 8; 
  90     m_green 
= col
.green 
>> 8; 
  91     m_alpha 
= wxALPHA_OPAQUE
; 
  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 
); 
 100 void wxColour::InitCGColorRef( CGColorRef col 
) 
 102     wxASSERT_MSG(col 
!= NULL
, "Invalid CoreGraphics Color"); 
 103     m_cgColour
.reset( col 
); 
 104     size_t noComp 
= CGColorGetNumberOfComponents( col 
); 
 106     const CGFloat 
*components 
= NULL
; 
 107     if ( noComp 
>= 1 && noComp 
<= 4 ) 
 109         // TODO verify whether we really are on a RGB color space 
 110         m_alpha 
= wxALPHA_OPAQUE
; 
 111         components 
= CGColorGetComponents( col 
); 
 113     InitFromComponents(components
, noComp
); 
 116 void wxColour::InitFromComponents(const CGFloat
* components
, size_t numComponents 
) 
 118     if ( numComponents 
< 1 || !components 
) 
 120         m_alpha 
= wxALPHA_OPAQUE
; 
 121         m_red 
= m_green 
= m_blue 
= 0; 
 125     if ( numComponents 
>= 3 ) 
 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); 
 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); 
 141 bool wxColour::operator == (const wxColour
& colour
) const 
 143     return ( (IsOk() == colour
.IsOk()) && (!IsOk() || 
 144                                            CGColorEqualToColor( m_cgColour
, colour
.m_cgColour 
) ) );