]>
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"
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()
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 wxObjectRefData
*wxColour::CreateRefData() const
165 return new wxColourRefData
;
168 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
170 return new wxColourRefData(*(wxColourRefData
*)data
);
173 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
178 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
179 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
180 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
182 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
183 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
184 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
186 M_COLDATA
->m_color
.pixel
= 0;
189 unsigned char wxColour::Red() const
191 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
194 return (unsigned char) M_COLDATA
->m_color
.red
;
196 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
200 unsigned char wxColour::Green() const
202 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
205 return (unsigned char) M_COLDATA
->m_color
.green
;
207 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
211 unsigned char wxColour::Blue() const
213 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
216 return (unsigned char) M_COLDATA
->m_color
.blue
;
218 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
222 void wxColour::CalcPixel( WXColormap cmap
)
224 wxCHECK_RET( Ok(), wxT("invalid colour") );
226 wxCHECK_RET( cmap
, wxT("invalid colormap") );
228 M_COLDATA
->AllocColour( cmap
);
231 unsigned long wxColour::GetPixel() const
233 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
235 return M_COLDATA
->m_color
.pixel
;
238 WXColor
*wxColour::GetColor() const
240 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
242 return (WXColor
*) &M_COLDATA
->m_color
;
245 bool wxColour::FromString(const wxChar
*name
)
247 Display
*dpy
= wxGlobalDisplay();
248 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
250 if ( XParseColor( dpy
, (Colormap
)colormap
, name
, &xcol
) )
254 m_refData
= new wxColourRefData
;
255 M_COLDATA
->m_colormap
= colormap
;
256 M_COLDATA
->m_color
= xcol
;
260 return wxColourBase::FromString(name
);