]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
9ce03f2d5b94e88f5c6d649bcfb951a08597c61c
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxColour class
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "colour.h"
17 #include "wx/gdicmn.h"
20 #include "wx/x11/private.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxColourRefData
: public wxObjectRefData
35 m_colormap
= (WXColormap
*) NULL
;
38 wxColourRefData(const wxColourRefData
& data
):
41 m_color
= data
.m_color
;
42 m_colormap
= data
.m_colormap
;
43 m_hasPixel
= data
.m_hasPixel
;
51 bool operator == (const wxColourRefData
& data
) const
53 return (m_colormap
== data
.m_colormap
&&
54 m_hasPixel
== data
.m_hasPixel
&&
55 m_color
.red
== data
.m_color
.red
&&
56 m_color
.green
== data
.m_color
.green
&&
57 m_color
.blue
== data
.m_color
.blue
&&
58 m_color
.pixel
== data
.m_color
.pixel
);
62 void AllocColour( WXColormap cmap
);
65 WXColormap m_colormap
;
68 friend class wxColour
;
70 // reference counter for systems with <= 8-Bit display
71 static unsigned short colMapAllocCounter
[ 256 ];
74 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 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
91 void wxColourRefData::FreeColour()
97 (wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
||
98 wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
) )
100 int idx
= m_color
.pixel
;
101 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
103 if (colMapAllocCounter
[ idx
] == 0)
105 unsigned long pixel
= m_color
.pixel
;
106 XFreeColors( wxGlobalDisplay(), (Colormap
) m_colormap
, &pixel
, 1, 0 );
112 void wxColourRefData::AllocColour( WXColormap cmap
)
114 if (m_hasPixel
&& (m_colormap
== cmap
))
120 if ((wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
) ||
121 (wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
))
123 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
124 int idx
= m_color
.pixel
;
125 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
130 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
136 //-----------------------------------------------------------------------------
138 #define M_COLDATA ((wxColourRefData *)m_refData)
140 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
142 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
144 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
146 m_refData
= new wxColourRefData();
148 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
149 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
150 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
152 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
153 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
154 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
156 M_COLDATA
->m_color
.pixel
= 0;
160 wxColour
wxColour::CreateByName(const wxString
& name
)
164 Display
*dpy
= wxGlobalDisplay();
165 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
167 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mb_str(), &xcol
) )
169 wxColourRefData
*refData
= new wxColourRefData
;
170 refData
->m_colormap
= colormap
;
171 refData
->m_color
= xcol
;
172 col
.m_refData
= refData
;
178 void wxColour::InitFromName( const wxString
&colourName
)
180 // check the cache first
182 if ( wxTheColourDatabase
)
184 col
= wxTheColourDatabase
->Find(colourName
);
189 col
= CreateByName(colourName
);
198 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
202 wxColour::~wxColour()
206 bool wxColour::operator == ( const wxColour
& col
) const
208 if (m_refData
== col
.m_refData
) return true;
210 if (!m_refData
|| !col
.m_refData
) return false;
212 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
213 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
215 return (own
->red
== other
->red
)
216 && (own
->green
== other
->green
)
217 && (own
->blue
== other
->blue
) ;
221 wxObjectRefData
*wxColour::CreateRefData() const
223 return new wxColourRefData
;
226 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
228 return new wxColourRefData(*(wxColourRefData
*)data
);
231 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
235 m_refData
= new wxColourRefData();
237 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
238 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
239 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
241 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
242 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
243 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
245 M_COLDATA
->m_color
.pixel
= 0;
248 unsigned char wxColour::Red() const
250 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
253 return (unsigned char) M_COLDATA
->m_color
.red
;
255 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
259 unsigned char wxColour::Green() const
261 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
264 return (unsigned char) M_COLDATA
->m_color
.green
;
266 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
270 unsigned char wxColour::Blue() const
272 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
275 return (unsigned char) M_COLDATA
->m_color
.blue
;
277 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
281 void wxColour::CalcPixel( WXColormap cmap
)
283 wxCHECK_RET( Ok(), wxT("invalid colour") );
285 wxCHECK_RET( cmap
, wxT("invalid colormap") );
287 M_COLDATA
->AllocColour( cmap
);
290 unsigned long wxColour::GetPixel() const
292 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
294 return M_COLDATA
->m_color
.pixel
;
297 WXColor
*wxColour::GetColor() const
299 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
301 return (WXColor
*) &M_COLDATA
->m_color
;