]>
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
7 // Copyright: (c) Julian Smart, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/colour.h"
18 #include "wx/gdicmn.h"
21 #include "wx/x11/private.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 class wxColourRefData
: public wxGDIRefData
40 wxColourRefData(const wxColourRefData
& data
)
42 m_color
= data
.m_color
;
43 m_colormap
= data
.m_colormap
;
44 m_hasPixel
= data
.m_hasPixel
;
47 virtual ~wxColourRefData()
52 bool operator==(const wxColourRefData
& data
) const
54 return (m_colormap
== data
.m_colormap
&&
55 m_hasPixel
== data
.m_hasPixel
&&
56 m_color
.red
== data
.m_color
.red
&&
57 m_color
.green
== data
.m_color
.green
&&
58 m_color
.blue
== data
.m_color
.blue
&&
59 m_color
.pixel
== data
.m_color
.pixel
);
63 void AllocColour( WXColormap cmap
);
66 WXColormap m_colormap
;
69 friend class wxColour
;
71 // reference counter for systems with <= 8-Bit display
72 static unsigned short colMapAllocCounter
[ 256 ];
75 unsigned short wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
92 void wxColourRefData::FreeColour()
98 (wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
||
99 wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
) )
101 int idx
= m_color
.pixel
;
102 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
104 if (colMapAllocCounter
[ idx
] == 0)
106 unsigned long pixel
= m_color
.pixel
;
107 XFreeColors( wxGlobalDisplay(), (Colormap
) m_colormap
, &pixel
, 1, 0 );
113 void wxColourRefData::AllocColour( WXColormap cmap
)
115 if (m_hasPixel
&& (m_colormap
== cmap
))
121 if ((wxTheApp
->m_visualInfo
->m_visualType
== GrayScale
) ||
122 (wxTheApp
->m_visualInfo
->m_visualType
== PseudoColor
))
124 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
125 int idx
= m_color
.pixel
;
126 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
131 m_hasPixel
= XAllocColor( wxGlobalDisplay(), (Colormap
) cmap
, &m_color
);
137 //-----------------------------------------------------------------------------
139 #define M_COLDATA ((wxColourRefData *)m_refData)
141 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
143 wxColour::~wxColour()
147 bool wxColour::operator == ( const wxColour
& col
) const
149 if (m_refData
== col
.m_refData
) return true;
151 if (!m_refData
|| !col
.m_refData
) return false;
153 XColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
154 XColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
156 return (own
->red
== other
->red
)
157 && (own
->green
== other
->green
)
158 && (own
->blue
== other
->blue
) ;
162 wxGDIRefData
*wxColour::CreateGDIRefData() const
164 return new wxColourRefData
;
167 wxGDIRefData
*wxColour::CloneGDIRefData(const wxGDIRefData
*data
) const
169 return new wxColourRefData(*(wxColourRefData
*)data
);
172 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
173 unsigned char WXUNUSED(alpha
))
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( IsOk(), 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( IsOk(), 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( IsOk(), 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( IsOk(), wxT("invalid colour") );
226 wxCHECK_RET( cmap
, wxT("invalid colormap") );
228 M_COLDATA
->AllocColour( cmap
);
231 unsigned long wxColour::GetPixel() const
233 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
235 return M_COLDATA
->m_color
.pixel
;
238 WXColor
*wxColour::GetColor() const
240 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid colour") );
242 return (WXColor
*) &M_COLDATA
->m_color
;
245 bool wxColour::FromString(const wxString
& name
)
247 Display
*dpy
= wxGlobalDisplay();
248 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
250 if ( XParseColor( dpy
, (Colormap
)colormap
, name
.mbc_str(), &xcol
) )
254 m_refData
= new wxColourRefData
;
255 M_COLDATA
->m_colormap
= colormap
;
256 M_COLDATA
->m_color
= xcol
;
260 return wxColourBase::FromString(name
);