]>
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"
21 #include "wx/gdicmn.h"
23 #include "wx/x11/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxColourRefData
: public wxObjectRefData
38 m_colormap
= (WXColormap
*) NULL
;
41 wxColourRefData(const wxColourRefData
& data
):
44 m_color
= data
.m_color
;
45 m_colormap
= data
.m_colormap
;
46 m_hasPixel
= data
.m_hasPixel
;
54 bool operator == (const wxColourRefData
& data
) const
56 return (m_colormap
== data
.m_colormap
&&
57 m_hasPixel
== data
.m_hasPixel
&&
58 m_color
.red
== data
.m_color
.red
&&
59 m_color
.green
== data
.m_color
.green
&&
60 m_color
.blue
== data
.m_color
.blue
&&
61 m_color
.pixel
== data
.m_color
.pixel
);
65 void AllocColour( WXColormap cmap
);
68 WXColormap m_colormap
;
71 friend class wxColour
;
73 // reference counter for systems with <= 8-Bit display
74 static unsigned short colMapAllocCounter
[ 256 ];
77 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94 void wxColourRefData::FreeColour()
100 (wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
||
101 wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
) )
103 int idx
= m_color
.pixel
;
104 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
106 if (colMapAllocCounter
[ idx
] == 0)
108 unsigned long pixel
= m_color
.pixel
;
109 XFreeColors( wxGlobalDisplay(), (Colormap
) m_colormap
, &pixel
, 1, 0 );
115 void wxColourRefData::AllocColour( WXColormap cmap
)
117 if (m_hasPixel
&& (m_colormap
== cmap
))
123 if ((wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
) ||
124 (wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
))
126 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
127 int idx
= m_color
.pixel
;
128 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
133 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
139 //-----------------------------------------------------------------------------
141 #define M_COLDATA ((wxColourRefData *)m_refData)
143 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
145 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
147 wxColour::~wxColour()
151 bool wxColour::operator == ( const wxColour
& col
) const
153 if (m_refData
== col
.m_refData
) return true;
155 if (!m_refData
|| !col
.m_refData
) return false;
157 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
158 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
160 return (own
->red
== other
->red
)
161 && (own
->green
== other
->green
)
162 && (own
->blue
== other
->blue
) ;
166 wxObjectRefData
*wxColour::CreateRefData() const
168 return new wxColourRefData
;
171 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
173 return new wxColourRefData(*(wxColourRefData
*)data
);
176 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
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(), (WXColor
*) NULL
, wxT("invalid colour") );
245 return (WXColor
*) &M_COLDATA
->m_color
;
248 bool wxColour::FromString(const wxChar
*name
)
250 Display
*dpy
= wxGlobalDisplay();
251 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
253 if ( XParseColor( dpy
, (Colormap
)colormap
, name
, &xcol
) )
257 m_refData
= new wxColourRefData
;
258 M_COLDATA
->m_colormap
= colormap
;
259 M_COLDATA
->m_color
= xcol
;
263 return wxColourBase::FromString(name
);