]>
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 wxObjectRefData
37 m_colormap
= (WXColormap
*) NULL
;
40 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 wxObjectRefData
*wxColour::CreateRefData() const
167 return new wxColourRefData
;
170 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
172 return new wxColourRefData(*(wxColourRefData
*)data
);
175 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
180 M_COLDATA
->m_color
.red
= ((unsigned short)red
) ;
181 M_COLDATA
->m_color
.green
= ((unsigned short)green
) ;
182 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) ;
184 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
185 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
186 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
188 M_COLDATA
->m_color
.pixel
= 0;
191 unsigned char wxColour::Red() const
193 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
196 return (unsigned char) M_COLDATA
->m_color
.red
;
198 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
202 unsigned char wxColour::Green() const
204 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
207 return (unsigned char) M_COLDATA
->m_color
.green
;
209 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
213 unsigned char wxColour::Blue() const
215 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
218 return (unsigned char) M_COLDATA
->m_color
.blue
;
220 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
224 void wxColour::CalcPixel( WXColormap cmap
)
226 wxCHECK_RET( Ok(), wxT("invalid colour") );
228 wxCHECK_RET( cmap
, wxT("invalid colormap") );
230 M_COLDATA
->AllocColour( cmap
);
233 unsigned long wxColour::GetPixel() const
235 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
237 return M_COLDATA
->m_color
.pixel
;
240 WXColor
*wxColour::GetColor() const
242 wxCHECK_MSG( Ok(), (WXColor
*) NULL
, wxT("invalid colour") );
244 return (WXColor
*) &M_COLDATA
->m_color
;
247 bool wxColour::FromString(const wxChar
*name
)
249 Display
*dpy
= wxGlobalDisplay();
250 WXColormap colormap
= wxTheApp
->GetMainColormap( dpy
);
252 if ( XParseColor( dpy
, (Colormap
)colormap
, name
, &xcol
) )
256 m_refData
= new wxColourRefData
;
257 M_COLDATA
->m_colormap
= colormap
;
258 M_COLDATA
->m_color
= xcol
;
262 return wxColourBase::FromString(name
);