]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxColour class
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/gdicmn.h"
15 #include "wx/x11/private.h"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 class wxColourRefData
: public wxObjectRefData
30 m_colormap
= (WXColormap
*) NULL
;
33 wxColourRefData(const wxColourRefData
& data
):
36 m_color
= data
.m_color
;
37 m_colormap
= data
.m_colormap
;
38 m_hasPixel
= data
.m_hasPixel
;
46 bool operator == (const wxColourRefData
& data
) const
48 return (m_colormap
== data
.m_colormap
&&
49 m_hasPixel
== data
.m_hasPixel
&&
50 m_color
.red
== data
.m_color
.red
&&
51 m_color
.green
== data
.m_color
.green
&&
52 m_color
.blue
== data
.m_color
.blue
&&
53 m_color
.pixel
== data
.m_color
.pixel
);
57 void AllocColour( WXColormap cmap
);
60 WXColormap m_colormap
;
63 friend class wxColour
;
65 // reference counter for systems with <= 8-Bit display
66 static unsigned short colMapAllocCounter
[ 256 ];
69 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
86 void wxColourRefData::FreeColour()
92 (wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
||
93 wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
) )
95 int idx
= m_color
.pixel
;
96 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
98 if (colMapAllocCounter
[ idx
] == 0)
100 unsigned long pixel
= m_color
.pixel
;
101 XFreeColors( wxGlobalDisplay(), (Colormap
) m_colormap
, &pixel
, 1, 0 );
107 void wxColourRefData::AllocColour( WXColormap cmap
)
109 if (m_hasPixel
&& (m_colormap
== cmap
))
115 if ((wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
) ||
116 (wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
))
118 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
119 int idx
= m_color
.pixel
;
120 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
125 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
131 //-----------------------------------------------------------------------------
133 #define M_COLDATA ((wxColourRefData *)m_refData)
135 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
137 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
139 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
141 m_refData
= new wxColourRefData();
143 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
144 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
145 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
147 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
148 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
149 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
151 M_COLDATA
->m_color
.pixel
= 0;
155 wxColour
wxColour::CreateByName(const wxString
& name
)
159 Display
*dpy
= wxGlobalDisplay();
160 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
162 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mb_str(), &xcol
) )
164 wxColourRefData
*refData
= new wxColourRefData
;
165 refData
->m_colormap
= colormap
;
166 refData
->m_color
= xcol
;
167 col
.m_refData
= refData
;
173 void wxColour::InitFromName( const wxString
&colourName
)
175 // check the cache first
177 if ( wxTheColourDatabase
)
179 col
= wxTheColourDatabase
->Find(colourName
);
184 col
= CreateByName(colourName
);
193 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
197 wxColour::~wxColour()
201 bool wxColour::operator == ( const wxColour
& col
) const
203 if (m_refData
== col
.m_refData
) return true;
205 if (!m_refData
|| !col
.m_refData
) return false;
207 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
208 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
210 return (own
->red
== other
->red
)
211 && (own
->green
== other
->green
)
212 && (own
->blue
== other
->blue
) ;
216 wxObjectRefData
*wxColour::CreateRefData() const
218 return new wxColourRefData
;
221 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
223 return new wxColourRefData(*(wxColourRefData
*)data
);
226 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
231 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
232 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
233 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
235 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
236 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
237 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
239 M_COLDATA
->m_color
.pixel
= 0;
242 unsigned char wxColour::Red() const
244 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
247 return (unsigned char) M_COLDATA
->m_color
.red
;
249 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
253 unsigned char wxColour::Green() const
255 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
258 return (unsigned char) M_COLDATA
->m_color
.green
;
260 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
264 unsigned char wxColour::Blue() const
266 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
269 return (unsigned char) M_COLDATA
->m_color
.blue
;
271 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
275 void wxColour::CalcPixel( WXColormap cmap
)
277 wxCHECK_RET( Ok(), wxT("invalid colour") );
279 wxCHECK_RET( cmap
, wxT("invalid colormap") );
281 M_COLDATA
->AllocColour( cmap
);
284 unsigned long wxColour::GetPixel() const
286 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
288 return M_COLDATA
->m_color
.pixel
;
291 WXColor
*wxColour::GetColor() const
293 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
295 return (WXColor
*) &M_COLDATA
->m_color
;