]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
af912732579e32b16a574aba12d0ecba3bd349ed
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colour.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/colour.h"
15 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 class wxColourRefData
: public wxObjectRefData
26 wxColourRefData(guint16 red
, guint16 green
, guint16 blue
)
38 virtual ~wxColourRefData()
44 void AllocColour( GdkColormap
* cmap
);
47 GdkColormap
*m_colormap
;
48 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
53 DECLARE_NO_COPY_CLASS(wxColourRefData
)
56 void wxColourRefData::FreeColour()
60 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
66 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
68 if (m_colormap
!= cmap
)
73 m_color
.green
= m_green
;
74 m_color
.blue
= m_blue
;
75 if (gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
))
82 //-----------------------------------------------------------------------------
84 #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
86 // GDK's values are in 0..65535 range, ours are in 0..255
89 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
91 wxColour::wxColour(const GdkColor
& gdkColor
)
93 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
);
100 bool wxColour::operator == ( const wxColour
& col
) const
102 if (m_refData
== col
.m_refData
)
105 if (!m_refData
|| !col
.m_refData
)
108 wxColourRefData
* refData
= M_COLDATA
;
109 wxColourRefData
* that_refData
= wx_static_cast(wxColourRefData
*, col
.m_refData
);
110 return refData
->m_red
== that_refData
->m_red
&&
111 refData
->m_green
== that_refData
->m_green
&&
112 refData
->m_blue
== that_refData
->m_blue
;
115 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
119 m_refData
= new wxColourRefData(
120 (guint16(red
) << SHIFT
) + red
,
121 (guint16(green
) << SHIFT
) + green
,
122 (guint16(blue
) << SHIFT
) + blue
);
125 unsigned char wxColour::Red() const
127 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
129 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
132 unsigned char wxColour::Green() const
134 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
136 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
139 unsigned char wxColour::Blue() const
141 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
143 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
146 void wxColour::CalcPixel( GdkColormap
*cmap
)
150 M_COLDATA
->AllocColour( cmap
);
153 int wxColour::GetPixel() const
155 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
157 return M_COLDATA
->m_color
.pixel
;
161 const GdkColor
*wxColour::GetColor() const
163 GdkColor
*wxColour::GetColor() const
166 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
168 return &M_COLDATA
->m_color
;
171 bool wxColour::FromString(const wxChar
*str
)
174 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
176 *this = wxColour(colGDK
);
180 return wxColourBase::FromString(str
);