]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/colour.cpp
3 // Purpose: wxColour class
4 // Author: Julian Smart, Robert Roebling
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 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
146 wxColour::~wxColour()
150 bool wxColour::operator == ( const wxColour
& col
) const
152 if (m_refData
== col
.m_refData
) return true;
154 if (!m_refData
|| !col
.m_refData
) return false;
156 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
157 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
159 return (own
->red
== other
->red
)
160 && (own
->green
== other
->green
)
161 && (own
->blue
== other
->blue
) ;
165 wxGDIRefData
*wxColour::CreateGDIRefData() const
167 return new wxColourRefData
;
170 wxGDIRefData
*wxColour::CloneGDIRefData(const wxGDIRefData
*data
) const
172 return new wxColourRefData(*(wxColourRefData
*)data
);
175 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
176 unsigned char WXUNUSED(alpha
))
181 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
182 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
183 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
185 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
186 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
187 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
189 M_COLDATA
->m_color
.pixel
= 0;
192 unsigned char wxColour::Red() const
194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
197 return (unsigned char) M_COLDATA
->m_color
.red
;
199 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
203 unsigned char wxColour::Green() const
205 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
208 return (unsigned char) M_COLDATA
->m_color
.green
;
210 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
214 unsigned char wxColour::Blue() const
216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
219 return (unsigned char) M_COLDATA
->m_color
.blue
;
221 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
225 void wxColour::CalcPixel( WXColormap cmap
)
227 wxCHECK_RET( Ok(), wxT("invalid colour") );
229 wxCHECK_RET( cmap
, wxT("invalid colormap") );
231 M_COLDATA
->AllocColour( cmap
);
234 unsigned long wxColour::GetPixel() const
236 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
238 return M_COLDATA
->m_color
.pixel
;
241 WXColor
*wxColour::GetColor() const
243 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
245 return (WXColor
*) &M_COLDATA
->m_color
;
248 bool wxColour::FromString(const wxString
& name
)
250 Display
*dpy
= wxGlobalDisplay();
251 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
253 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mbc_str(), &xcol
) )
257 m_refData
= new wxColourRefData
;
258 M_COLDATA
->m_colormap
= colormap
;
259 M_COLDATA
->m_color
= xcol
;
263 return wxColourBase::FromString(name
);