]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/colour.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
12 #include "wx/colour.h"
15 #include "wx/gtk/private.h"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 class wxColourRefData
: public wxGDIRefData
25 wxColourRefData(const GdkRGBA
& gdkRGBA
)
28 m_gdkColor
.red
= guint16(gdkRGBA
.red
* 65535);
29 m_gdkColor
.green
= guint16(gdkRGBA
.green
* 65535);
30 m_gdkColor
.blue
= guint16(gdkRGBA
.blue
* 65535);
31 m_alpha
= wxByte(gdkRGBA
.alpha
* 255 + 0.5);
33 wxColourRefData(const GdkColor
& gdkColor
)
34 : m_gdkColor(gdkColor
)
36 m_gdkRGBA
.red
= gdkColor
.red
/ 65535.0;
37 m_gdkRGBA
.green
= gdkColor
.green
/ 65535.0;
38 m_gdkRGBA
.blue
= gdkColor
.blue
/ 65535.0;
42 wxColourRefData(guchar red
, guchar green
, guchar blue
, guchar alpha
)
44 m_gdkRGBA
.red
= red
/ 255.0;
45 m_gdkRGBA
.green
= green
/ 255.0;
46 m_gdkRGBA
.blue
= blue
/ 255.0;
47 m_gdkRGBA
.alpha
= alpha
/ 255.0;
48 m_gdkColor
.red
= (guint16(red
) << 8) + red
;
49 m_gdkColor
.green
= (guint16(green
) << 8) + green
;
50 m_gdkColor
.blue
= (guint16(blue
) << 8) + blue
;
56 wxColourRefData(guint16 red
, guint16 green
, guint16 blue
, wxByte alpha
= 0xff)
69 virtual ~wxColourRefData()
75 void AllocColour( GdkColormap
* cmap
);
78 GdkColormap
*m_colormap
;
79 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
86 wxDECLARE_NO_COPY_CLASS(wxColourRefData
);
90 void wxColourRefData::FreeColour()
94 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
100 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
102 if (m_colormap
!= cmap
)
107 m_color
.green
= m_green
;
108 m_color
.blue
= m_blue
;
109 if (gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
))
117 //-----------------------------------------------------------------------------
119 #define M_COLDATA static_cast<wxColourRefData*>(m_refData)
121 // GDK's values are in 0..65535 range, ours are in 0..255
125 wxColour::wxColour(const GdkRGBA
& gdkRGBA
)
127 m_refData
= new wxColourRefData(gdkRGBA
);
130 wxColour::wxColour(const GdkColor
& gdkColor
)
132 m_refData
= new wxColourRefData(gdkColor
);
135 wxColour::wxColour(const GdkColor
& gdkColor
)
137 m_refData
= new wxColourRefData(gdkColor
.red
, gdkColor
.green
, gdkColor
.blue
);
141 wxColour::~wxColour()
145 bool wxColour::operator == ( const wxColour
& col
) const
147 if (m_refData
== col
.m_refData
)
150 if (!m_refData
|| !col
.m_refData
)
153 wxColourRefData
* refData
= M_COLDATA
;
154 wxColourRefData
* that_refData
= static_cast<wxColourRefData
*>(col
.m_refData
);
156 return refData
->m_gdkColor
.red
== that_refData
->m_gdkColor
.red
&&
157 refData
->m_gdkColor
.green
== that_refData
->m_gdkColor
.green
&&
158 refData
->m_gdkColor
.blue
== that_refData
->m_gdkColor
.blue
&&
160 return refData
->m_red
== that_refData
->m_red
&&
161 refData
->m_green
== that_refData
->m_green
&&
162 refData
->m_blue
== that_refData
->m_blue
&&
164 refData
->m_alpha
== that_refData
->m_alpha
;
167 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
173 m_refData
= new wxColourRefData(red
, green
, blue
, alpha
);
175 m_refData
= new wxColourRefData(
176 (guint16(red
) << SHIFT
) + red
,
177 (guint16(green
) << SHIFT
) + green
,
178 (guint16(blue
) << SHIFT
) + blue
,
183 unsigned char wxColour::Red() const
185 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
188 return wxByte(M_COLDATA
->m_gdkColor
.red
>> 8);
190 return wxByte(M_COLDATA
->m_red
>> SHIFT
);
194 unsigned char wxColour::Green() const
196 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
199 return wxByte(M_COLDATA
->m_gdkColor
.green
>> 8);
201 return wxByte(M_COLDATA
->m_green
>> SHIFT
);
205 unsigned char wxColour::Blue() const
207 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
210 return wxByte(M_COLDATA
->m_gdkColor
.blue
>> 8);
212 return wxByte(M_COLDATA
->m_blue
>> SHIFT
);
216 unsigned char wxColour::Alpha() const
218 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
220 return M_COLDATA
->m_alpha
;
224 void wxColour::CalcPixel( GdkColormap
*cmap
)
228 M_COLDATA
->AllocColour( cmap
);
231 int wxColour::GetPixel() const
233 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
235 return M_COLDATA
->m_color
.pixel
;
239 const GdkColor
*wxColour::GetColor() const
241 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid colour") );
244 return &M_COLDATA
->m_gdkColor
;
246 return &M_COLDATA
->m_color
;
251 wxColour::operator const GdkRGBA
*() const
253 const GdkRGBA
* c
= NULL
;
255 c
= &M_COLDATA
->m_gdkRGBA
;
260 bool wxColour::FromString(const wxString
& str
)
264 if (gdk_rgba_parse(&gdkRGBA
, wxGTK_CONV_SYS(str
)))
266 *this = wxColour(gdkRGBA
);
271 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
273 *this = wxColour(colGDK
);
278 return wxColourBase::FromString(str
);