1
/////////////////////////////////////////////////////////////////////////////
2
// Name: src/x11/colour.cpp
3
// Purpose: wxColour class
4
// Author: Julian Smart, Robert Roebling
7
// RCS-ID: $Id: colour.cpp 67681 2011-05-03 16:29:04Z DS $
8
// Copyright: (c) Julian Smart, Robert Roebling
9
// Licence: wxWindows licence
10
/////////////////////////////////////////////////////////////////////////////
12
// For compilers that support precompilation, includes "wx.h".
13
#include "wx/wxprec.h"
15
#include "wx/colour.h"
19
#include "wx/gdicmn.h"
22
#include "wx/x11/private.h"
24
//-----------------------------------------------------------------------------
26
//-----------------------------------------------------------------------------
28
class wxColourRefData : public wxGDIRefData
41
wxColourRefData(const wxColourRefData& data)
43
m_color = data.m_color;
44
m_colormap = data.m_colormap;
45
m_hasPixel = data.m_hasPixel;
48
virtual ~wxColourRefData()
53
bool operator==(const wxColourRefData& data) const
55
return (m_colormap == data.m_colormap &&
56
m_hasPixel == data.m_hasPixel &&
57
m_color.red == data.m_color.red &&
58
m_color.green == data.m_color.green &&
59
m_color.blue == data.m_color.blue &&
60
m_color.pixel == data.m_color.pixel);
64
void AllocColour( WXColormap cmap );
67
WXColormap m_colormap;
70
friend class wxColour;
72
// reference counter for systems with <= 8-Bit display
73
static unsigned short colMapAllocCounter[ 256 ];
76
unsigned short wxColourRefData::colMapAllocCounter[ 256 ] =
78
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
93
void wxColourRefData::FreeColour()
99
(wxTheApp->m_visualInfo->m_visualType == GrayScale ||
100
wxTheApp->m_visualInfo->m_visualType == PseudoColor) )
102
int idx = m_color.pixel;
103
colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
105
if (colMapAllocCounter[ idx ] == 0)
107
unsigned long pixel = m_color.pixel;
108
XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 );
114
void wxColourRefData::AllocColour( WXColormap cmap )
116
if (m_hasPixel && (m_colormap == cmap))
122
if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
123
(wxTheApp->m_visualInfo->m_visualType == PseudoColor))
125
m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
126
int idx = m_color.pixel;
127
colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
132
m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
138
//-----------------------------------------------------------------------------
140
#define M_COLDATA ((wxColourRefData *)m_refData)
142
#define SHIFT (8*(sizeof(short int)-sizeof(char)))
144
wxColour::~wxColour()
148
bool wxColour::operator == ( const wxColour& col ) const
150
if (m_refData == col.m_refData) return true;
152
if (!m_refData || !col.m_refData) return false;
154
XColor *own = &(((wxColourRefData*)m_refData)->m_color);
155
XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
157
return (own->red == other->red)
158
&& (own->green == other->green)
159
&& (own->blue == other->blue) ;
163
wxGDIRefData *wxColour::CreateGDIRefData() const
165
return new wxColourRefData;
168
wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
170
return new wxColourRefData(*(wxColourRefData *)data);
173
void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
174
unsigned char WXUNUSED(alpha))
179
M_COLDATA->m_color.red = ((unsigned short)red) ;
180
M_COLDATA->m_color.green = ((unsigned short)green) ;
181
M_COLDATA->m_color.blue = ((unsigned short)blue) ;
183
M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
184
M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
185
M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
187
M_COLDATA->m_color.pixel = 0;
190
unsigned char wxColour::Red() const
192
wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
195
return (unsigned char) M_COLDATA->m_color.red ;
197
return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
201
unsigned char wxColour::Green() const
203
wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
206
return (unsigned char) M_COLDATA->m_color.green ;
208
return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
212
unsigned char wxColour::Blue() const
214
wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
217
return (unsigned char) M_COLDATA->m_color.blue ;
219
return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
223
void wxColour::CalcPixel( WXColormap cmap )
225
wxCHECK_RET( IsOk(), wxT("invalid colour") );
227
wxCHECK_RET( cmap, wxT("invalid colormap") );
229
M_COLDATA->AllocColour( cmap );
232
unsigned long wxColour::GetPixel() const
234
wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
236
return M_COLDATA->m_color.pixel;
239
WXColor *wxColour::GetColor() const
241
wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") );
243
return (WXColor*) &M_COLDATA->m_color;
246
bool wxColour::FromString(const wxString& name)
248
Display *dpy = wxGlobalDisplay();
249
WXColormap colormap = wxTheApp->GetMainColormap( dpy );
251
if ( XParseColor( dpy, (Colormap)colormap, name.mbc_str(), &xcol ) )
255
m_refData = new wxColourRefData;
256
M_COLDATA->m_colormap = colormap;
257
M_COLDATA->m_color = xcol;
261
return wxColourBase::FromString(name);